20
\$\begingroup\$

Your task is to take an encrypted string as input, and output the string decrypted, to reveal its hidden message.

The strings, both the input and output, will contain characters from this list of 64 ASCII-characters (note the leading space):

 !"#$%&'()*+,-./0123456789:;=?@[\]^_abcdefghijklmnopqrstuvwxyz|~

These characters are assigned numbers, in the order they are listed above:

  ! " # $ % &   ...
0 1 2 3 4 5 6   ...

So, space is number 0, ! is number 1 and ~ is number 63. These numbers can be represented in 6-bit binary code:

 :  0:  000000
!:  1:  000001
":  2:  000010
#:  3:  000011 
.. ...  ......
z: 61:  111101
|: 62:  111110
~: 63:  111111

The encryption is very simple:

I'll use eC for encrypted characters, and C for characters of the original string. C(n) is the n'th character of the original string, while eC(n) is the n'th character of the encrypted string.

You'll use the 6-bit binary representation of the characters. The first character will be eC(0) = not(C(0)). From there, all characters will be eC(n) = xor(C(n),C(n-1)).

Example:

Let's assume the input string is code.

  • c is the 38th character (zero indexed), or 100110 in binary. The encrypted version has all bits flipped, so 011001 -> 25 -> '9' (again, zero indexed).
  • o is the 50th character, or 110010 in binary. xor(100110, 110010) = 010100 = 20 = '4'.
  • d is the 39th character, or 100111 in binary. xor(100111, 110010) = 010101 = 21 = '5'.
  • e is the 40th character, or 101000 in binary. xor(101000, 100111) = 001111 = 15 = '/'.

So, if the original string is code, the encrypted string will become 945/.


Test cases:

945/
code

,&'8[14 =?;gp+% 2'@s&&c45/eg8?&
programming puzzles & code golf

;a$5$%0r?2@12dw6# lb-eg&519nt%ot=9$@es@96+?;ga" 4*)&ta56dp[?o#t%oh/"(&?#ee![,+,/+fe4"
a $150 reward will be given to those sending account and pin# to hackers@steal_id.com

~!#!'!#!/!#!'!#![!#!'!#!/!#!'!#!~!#!'!#!/!#!'!#![!#!'!#!/!#!'!#!
 !"#$%&'()*+,-./0123456789:;=?@[\]^_abcdefghijklmnopqrstuvwxyz|~
\$\endgroup\$
  • 4
    \$\begingroup\$ Married life and already with the decoding of messages? :p \$\endgroup\$ – Jonathan Allan Jun 2 '17 at 17:50
  • 1
    \$\begingroup\$ @JonathanAllan I've been decoding messages for years... :P \$\endgroup\$ – Stewie Griffin Jun 2 '17 at 17:55

16 Answers 16

9
\$\begingroup\$

Jelly, 27 26 bytes

ØJḟ“<>`{}”ḟØAɓi@€_33^\96_ị

Try it online!

Alternate version, 22 bytes (non-competing)

Jelly finally caught up with other golfing langs and got a printable ASCII atom, so this works now.

ØṖḟ“<>`{}”ḟØAɓi@€’^\Nị

Try it online!

How it works

ØJḟ“<>`{}”ḟØAɓi@€_33^\96_ị  Main link. Argument: s (string)

ØJ                          Yield Jelly's code page, i.e., 32 non-ASCII characters,
                            followed by all printable ASCII characters, followed by
                            129 non-ASCII characters.
  ḟ“<>`{}”                  Filterfalse; remove the characters "<>`{}".
          ḟØA               Filterfalse; remove all uppercase ASCII letters.
                            Let's call the resulting alphabet a.
             ɓ              Begin a new, dyadic chain.
                            Left argument: s. Right argument: a
              i@€           Find the (1-based) index of all characters of s in a.
                 _33        Subtract 33, so ' ' maps to 0, '~' maps to 63.
                    ^\      Compute the cumulative bitwise XOR.
                            We didn't take the bitwise NOT of the first index,
                            which can be rectified by subtracting all results from
                            63. However, we must also add 33 to account for the
                            fact that the index of ' ' in a is 33.
                      96_   Subtract the results from 96.
                         ị  Index into a.
\$\endgroup\$
  • \$\begingroup\$ Ugh I thought of this within the last couple of minutes! Shame on me for posting an explanation first :p \$\endgroup\$ – Jonathan Allan Jun 2 '17 at 19:34
6
\$\begingroup\$

JavaScript (ES6), 115 bytes

s=>s.replace(/./g,s=>d[x^=d.indexOf(s)],x=63,d=` !"#$%&'()*+,-./0123456789:;=?@[\\]^_abcdefghijklmnopqrstuvwxyz|~`)

Test cases

let f =

s=>s.replace(/./g,s=>d[x^=d.indexOf(s)],x=63,d=` !"#$%&'()*+,-./0123456789:;=?@[\\]^_abcdefghijklmnopqrstuvwxyz|~`)

console.log(f("945/"))
console.log(f(",&'8[14 =?;gp+% 2'@s&&c45/eg8?&"))
console.log(f(';a$5$%0r?2@12dw6# lb-eg&519nt%ot=9$@es@96+?;ga" 4*)&ta56dp[?o#t%oh/"(&?#ee![,+,/+fe4"'))
console.log(f("~!#!'!#!/!#!'!#![!#!'!#!/!#!'!#!~!#!'!#!/!#!'!#![!#!'!#!/!#!'!#!"))

\$\endgroup\$
  • \$\begingroup\$ I think the ^ goes to the right of the ]. The answer still works with that change for the test cases I believe. \$\endgroup\$ – Jonathan Allan Jun 2 '17 at 18:39
  • \$\begingroup\$ Might it be shorter to construct d from what is missing? \$\endgroup\$ – Jonathan Allan Jun 2 '17 at 18:40
  • 1
    \$\begingroup\$ @JonathanAllan Maybe, but JS has pretty long character manipulation methods. I failed to have a dynamic version shorter than a simple hardcoded string in previous similar challenges and also had no success this time so far. \$\endgroup\$ – Arnauld Jun 2 '17 at 18:45
  • \$\begingroup\$ I know this is javascript but it doesn't look like it at all :S \$\endgroup\$ – Slava Knyazev Jun 3 '17 at 0:42
5
\$\begingroup\$

Jelly,  34  31 bytes

-3 bytes thanks to Dennis (use twice rather than œ-, ; and ¤; use ”~ rather than 63 )

32r126Ọḟ“<>`{}”ḟØAṙ1ɓ”~;i@€^\Ḋị

A monadic link taking and returning lists of characters.
*Note: the input arguments to a Jelly program utilise Python string formatting, so quoting with ", ', ''' (or if unambiguous no quoting) are all options.

Try it online!

How?

Bitwise-xor is invertible (given "leading zeros").

Bitwise-Not is an xor with "all ones" - in this case only 6 ones are ever required, so 27-1 = 63.

Once we have created the array or characters and looked up the indexes of the input characters, the decode itself is then simply a cumulative reduction by bitwise-xor, after which we may index back into the same array.

32r126Ọḟ“<>`{}”ḟØAṙ1ɓ”~;i@€^\Ḋị - Main link: string, s
32r126                          - inclusive range -> [32,33,...,125,126]
      Ọ                         - cast to ordinals -> " !...}~"
        “<>`{}”                 - literal ['<','>','`','{','}']
       ḟ                        - filter discard
                ØA              - yield "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
               ḟ                - filter discard
                  ṙ1            - rotate left by one (Jelly indexing is one based)
                    ɓ           - dyadic chain separation, swapping arguments (call that p)
                     ”~         - literal '~'
                       ;        - concatenate with s (`~` has value 63 for the bitwise-not)
                        i@€     - first index* of €ach character of s in p
                            \   - cumulative reduce by:
                           ^    -   bitwise-xor
                             Ḋ  - dequeue (remove the 63 from '~')
                              ị - index into p

* Note: looking up a space in p will yield a 64, but that's OK since indexing back into p is modular so adding a leading 1 is like adding 64, taking the index right back around to where it needs to be).

\$\endgroup\$
3
\$\begingroup\$

Java, 225 bytes

String D(String E){String A="",C=" !\"#$%&'()*+,-./0123456789:;=?@[\\]^_abcdefghijklmnopqrstuvwxyz|~";for(int i=-1,J=0;++i<E.length();J=C.indexOf(E.charAt(i)),A+=C.charAt(i<1?(1<<6)-1-J:C.indexOf(A.charAt(i-1))^J));return A;}

I have not golfed in Java in a very long time, so any golfing tips are appreciated.

Try It Online!

\$\endgroup\$
  • \$\begingroup\$ I know it's been a while, but some things to golf: using a Java 8+ lambda, so String D(String E){ becomes E->{ (-15 bytes); -1-J can be +~J (-1 byte); and i=-1 can be i=0, the ++ can be moved to i++<1?, and then the i-1 becomes i-2 (-1 byte). Try it online: 208 bytes \$\endgroup\$ – Kevin Cruijssen Feb 7 at 13:44
2
\$\begingroup\$

05AB1E, 40 bytes

'~«vžQAu"<>{}`"«SK©yk64+b¦S}r.gG^DJC®sè?

Try it online!

Explanation

'~«                                       # append input to "~"
   v                                      # for each char y in the resulting string
    žQ                                    # push printable ascii chars
      Au                                  # push upper case alphabet
        "<>{}`"«                          # append "<>{}`"
                SK                        # remove those chars from printable ascii
                  ©                       # store a copy in register
                   yk                     # get the index of y in that string
                     64+                  # add 64
                        b                 # convert to binary
                         ¦S               # remove leading 1 and convert to list of bits
                           }              # end loop
                            r             # reverse stack
                             .gG          # len(stack)-1 times do
                                ^         # xor top 2 lists of bits on the stack
                                 DJC      # convert a copy to decimal
                                    ®sè   # index into the char string with this
                                       ?  # print
\$\endgroup\$
2
\$\begingroup\$

CPU x86 instruction set, 235 bytes

00000750  50                push eax
00000751  8A10              mov dl,[eax]
00000753  80FA00            cmp dl,0x0
00000756  7418              jz 0x770
00000758  31DB              xor ebx,ebx
0000075A  EB03              jmp short 0x75f
0000075C  F9                stc
0000075D  EB13              jmp short 0x772
0000075F  8A83C1A14000      mov al,[ebx+0x40a1c1]
00000765  3C00              cmp al,0x0
00000767  74F3              jz 0x75c
00000769  38C2              cmp dl,al
0000076B  7404              jz 0x771
0000076D  43                inc ebx
0000076E  EBEF              jmp short 0x75f
00000770  42                inc edx
00000771  F8                clc
00000772  58                pop eax
00000773  C3                ret
00000774  53                push ebx
00000775  8B442408          mov eax,[esp+0x8]
00000779  31C9              xor ecx,ecx
0000077B  09C0              or eax,eax
0000077D  7505              jnz 0x784
0000077F  31C0              xor eax,eax
00000781  48                dec eax
00000782  EB2F              jmp short 0x7b3
00000784  E8C7FFFFFF        call 0x750
00000789  72F4              jc 0x77f
0000078B  7510              jnz 0x79d
0000078D  F6D3              not bl
0000078F  80E33F            and bl,0x3f
00000792  88D9              mov cl,bl
00000794  8AB3C1A14000      mov dh,[ebx+0x40a1c1]
0000079A  8830              mov [eax],dh
0000079C  40                inc eax
0000079D  E8AEFFFFFF        call 0x750
000007A2  72DB              jc 0x77f
000007A4  750D              jnz 0x7b3
000007A6  30D9              xor cl,bl
000007A8  8AB1C1A14000      mov dh,[ecx+0x40a1c1]
000007AE  8830              mov [eax],dh
000007B0  40                inc eax
000007B1  EBEA              jmp short 0x79d
000007B3  5B                pop ebx
000007B4  C20400            ret 0x4
000007B7  53                push ebx
000007B8  8B442408          mov eax,[esp+0x8]
000007BC  31C9              xor ecx,ecx
000007BE  09C0              or eax,eax
000007C0  7505              jnz 0x7c7
000007C2  31C0              xor eax,eax
000007C4  48                dec eax
000007C5  EB32              jmp short 0x7f9
000007C7  E884FFFFFF        call 0x750
000007CC  72F4              jc 0x7c2
000007CE  750F              jnz 0x7df
000007D0  30D9              xor cl,bl
000007D2  8AB1C1A14000      mov dh,[ecx+0x40a1c1]
000007D8  8830              mov [eax],dh
000007DA  88D9              mov cl,bl
000007DC  40                inc eax
000007DD  EBE8              jmp short 0x7c7
000007DF  8B442408          mov eax,[esp+0x8]
000007E3  E868FFFFFF        call 0x750
000007E8  72D8              jc 0x7c2
000007EA  750D              jnz 0x7f9
000007EC  F6D3              not bl
000007EE  80E33F            and bl,0x3f
000007F1  8AB3C1A14000      mov dh,[ebx+0x40a1c1]
000007F7  8830              mov [eax],dh
000007F9  5B                pop ebx
000007FA  C20400            ret 0x4

The function find() and deCript() + the string abc: 171 bytes + 64 bytes=235 assembly with nasmw and compiler/library with Borland C compiler:

; nasmw -fobj  this.asm
; bcc32 -v  this.obj
section _DATA use32 public class=DATA
global _main
extern _printf

fmt1 db "result=%s" , 13, 10, 0, 0
fmt2 db "abc[63]=%c" , 13, 10, 0, 0
code1 db "code" , 0, 0
code2 db ",&'8[14 =?;gp+% 2'@s&&c45/eg8?&" , 0, 0
code3 db ';a$5$%0r?2@12dw6# lb-eg&519nt%ot=9$@es@96+?;ga" 4*)&ta56dp[?o#t%oh/"(&?#ee![,+,/+fe4"' , 0, 0
abc db ' !"#$%' , "&'()*+,-./0123456789:;=?@[\]^_abcdefghijklmnopqrstuvwxyz|~" , 0, 0

section _TEXT use32 public class=CODE

find:     
      push    eax
      mov     dl,  [eax]
      cmp     dl,  0
      je      .2
      xor     ebx,  ebx
      jmp     short  .1
.e:   stc
      jmp     short  .z
.1:   mov     al,  [abc+ebx]
      cmp     al,  0
      je      .e
      cmp     dl,  al
      je      .3
      inc     ebx
      jmp     short  .1
.2:   inc     edx           ; set zf=0
.3:   clc
.z:   pop     eax
      ret

deCript:  
      push    ebx
      mov     eax,  dword[esp+8]
      xor     ecx,  ecx
      or      eax,  eax
      jnz     .1
.e:   xor     eax,  eax
      dec     eax
      jmp     short  .z
.1:   call    find
      jc      .e
      jnz     .2
      not     bl
      and     bl,  03Fh
      mov     cl,  bl
      mov     dh,  [abc+ebx]
      mov     [eax],  dh
      inc     eax
.2:   call    find
      jc      .e
      jnz     .z
      xor     cl,  bl
      mov     dh,  [abc+ecx]
      mov     [eax],  dh
      inc     eax
      jmp     short  .2
.z:       
      pop     ebx
      ret     4

cript:    
      push    ebx
      mov     eax,  dword[esp+8]
      xor     ecx,  ecx
      or      eax,  eax
      jnz     .1
.e:   xor     eax,  eax
      dec     eax
      jmp     short  .z
.1:   call    find
      jc      .e
      jnz     .2
      xor     cl,  bl
      mov     dh,  [abc+ecx]
      mov     [eax],  dh
      mov     cl,  bl
      inc     eax
      jmp     short  .1
.2:   mov     eax,  dword[esp+8]
      call    find
      jc      .e
      jnz     .z
      not     bl
      and     bl,  03Fh
      mov     dh,  [abc+ebx]
      mov     [eax],  dh
.z:       
      pop     ebx
      ret     4

_main:    
      pushad

      push    code1
      call    cript
      push    code1
      push    fmt1
      call    _printf
      add     esp,  8

      xor     eax,  eax
      mov     al,  [abc+63]
      push    eax
      push    fmt2
      call    _printf
      add     esp,  8

      push    code1
      call    deCript
      push    code1
      push    fmt1
      call    _printf
      add     esp,  8
      push    code2
      call    deCript
      push    code2
      push    fmt1
      call    _printf
      add     esp,  8
      push    code3
      call    deCript
      push    code3
      push    fmt1
      call    _printf
      add     esp,  8

      popad
      mov     eax,  0
      ret

results:

result=945/
abc[63]=~
result=code
result=programming puzzles & code golf
result=a $150 reward will be given to those sending account and pin# to hackers@steal_id.com

Assembly is better (for say the true I use a macro system, yes I know it is too long but as the C one +- with macro system , for say the true because the instructions are simpler it is easy write code in assembly even without make corrections as one write in English (not I) )

\$\endgroup\$
2
\$\begingroup\$

C (gcc), 153 bytes

char*i=" !\"#$%&'()*+,-./0123456789:;=?@[\\]^_abcdefghijklmnopqrstuvwxyz|~";a(b){b=index(i,b)-i;}f(char*x){for(*x=i[a(*x)^63];x[1];)*x=i[a(*x)^a(*++x)];}

Try it online!

Slightly golfed less

char*i=" !\"#$%&'()*+,-./0123456789:;=?@[\\]^_abcdefghijklmnopqrstuvwxyz|~";
a(b){
  b=index(i,b)-i;
}
f(char*x){
  for(*x=i[a(*x)^63];x[1];)
    *x=i[a(*x)^a(*++x)];
}
\$\endgroup\$
2
\$\begingroup\$

APL (Dyalog Unicode), 52 bytesSBCS

Requires ⎕IO←0

{C[2∘⊥¨≠\~@0⊢(6/2)∘⊤¨⍵⍳⍨C←(32↓⎕UCS⍳127)~⎕A,'<>{}`']}

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Röda, 120 100 bytes

f a{A=[]seq 32,126|chr _|{|v|A+=v if[v=~"[^<>`{}A-Z]"]}_;l=63;a|{|c|l=indexOf(c,A) b_xor l;[A[l]]}_}

Try it online!

I used the l=63 trick from the JavaScript answer. Right now I am working on shortening A so golfing in progress...

\$\endgroup\$
1
\$\begingroup\$

Python 2, 155 bytes

lambda s,m=' !"#$%&\'()*+,-./0123456789:;=?@[\\]^_abcdefghijklmnopqrstuvwxyz|~':''.join(m[i]for i in reduce(lambda a,b:a+[a[-1]^b],map(m.find,s),[63])[1:])

Try it online!

\$\endgroup\$
1
\$\begingroup\$

PHP, 103 Bytes

for($x=63;~$c=$argn[$i++];)echo($a=join(preg_grep("#[^A-Z<>`{}]#",range(" ","~"))))[$x^=strpos($a,$c)];

Try it online!

PHP, 107 Bytes

for($x=63;~$c=$argn[$i++];)echo($a=preg_filter("#[A-Z<>`{}]#","",join(range(" ","~"))))[$x^=strpos($a,$c)];

Try it online!

PHP, 118 Bytes

for($x=63;~$c=$argn[$i++];)echo($a=join(array_diff(range(" ","~"),range(A,Z),str_split("<>`{}"))))[$x^=strpos($a,$c)];

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Python + Numpy, 214 bytes

Can't compete with other Python solution, though uses different, pure numeric approach:

from numpy import *
def f(s):
    r=range
    A=array
    S=A(r(32,60)+[61,63,64]+r(91,96)+r(97,123)+[124,126])
    T=A(r(128))
    T[S]=A(r(64))
    W=T[fromstring(s,"b")]
    W[0]=~W[0]
    W=S[bitwise_xor.accumulate(W)&63]
    print W.tobytes()[::4]

A bit of explanation:

  • S=A(r(32,60)+...) - define alphabet as code ranges
  • T=A(r(128)) - init hash table of size 128 (largest codepoint)
  • T[S]=A(r(64)) - fill the hash table, i.e. write indices 0-63 to the elements with ASCII indices
  • W=T[fromstring(s,"b")] - convert the input to array & translate it into new codes
  • W[0]=~W[0] - invert 1st value
  • W=S[bitwise_xor.accumulate(W)&63] - use Numpy's accumulate method with xor to avoid looping, reset 2 left bits and translate back to ascii
\$\endgroup\$
1
\$\begingroup\$

Alice, 46 bytes

/" >"{""ZNr\'?wi.h%)qXq&[.&]?oe(K
\"<~`r}A*"!/

Try it online!

Explanation

The first half of the program runs in ordinal mode and sets up the mapping from numbers to characters. The second half runs in cardinal mode and uses this mapping to decode the input.

" ~"                    Push this string
    r                   Convert to entire range (all printable ASCII)
     "AZ"               Push this string
         r              Convert to entire range
          "<>`{}"       Push this string
                 *      Append last two strings
                  N     Remove characters from full string
                   !    Copy entire string to tape

'?                      Push 63 (code point of ?) onto the stack
  w                     Store return address (start main loop)
   i                    Take byte of input
    .h%                 Calculate n mod (n+1): this crashes on EOF (-1)
       )                Find byte on tape
        q               Get position on tape
         X              Bitwise XOR with current value
          q&[           Return to tape position 0
             .&]        Move to tape position corresponding to result of earlier XOR
                ?o      Get and output byte at current tape position
                  e(    Search for -1 to left of current tape position (which will put us at position -1)
                    K   Jump to previously pushed return address.
\$\endgroup\$
1
\$\begingroup\$

Japt -P, 33 bytes

;
EkB+"<>}\{`"1
¬i63 åÈ^VaYÃÅm!gV

Try it online!

For some reason, the test cases object to running as a suite, so here's the second, third, and fourth individually.

Explanation:

;                    #Set E to a string of all printable ASCII characters

Ek          1        #Set V to E with these characters removed:
  B                  # All uppercase letters
   +"<>}\{`"         # and the characters "<>}{`"

¬                    #Turn the input into an array
 i63                 #Add the number 63 to the front of that array
     å     Ã         #Replace each character with:
      È              # The index of the previous decoded character in V
       ^             # Xor with...
        VaY          # The index of the current character in V
            Å        #Remove the extra character
             m!gV    #Map the indexes to the character in V
                     #Join to a string due to the flag
\$\endgroup\$
  • \$\begingroup\$ I think your code does exactly the opposite of what the task is..? :-) \$\endgroup\$ – Stewie Griffin Feb 7 at 18:54
  • \$\begingroup\$ @StewieGriffin, oh, my bad. I followed the process listed in the question, but I didn't realize the challenge was to reverse the described process. I'll try to update. \$\endgroup\$ – Kamil Drakari Feb 7 at 19:09
  • \$\begingroup\$ @StewieGriffin Decoding turned out to be shorter, actually. \$\endgroup\$ – Kamil Drakari Feb 7 at 19:36
1
\$\begingroup\$

APL(NARS), 72 chars, 144 bytes

{r←6⍴2⋄y←{r⊤⍵}¨¯1+⍵⍳⍨s←⎕AV[33..127]∼⎕A,'{<>}`'⋄y[1]←⊂∼↑y⋄s[1+{r⊥⍵}¨≠\y]}

This suppose the input is always in the array 's'... For understand how to do decipher i had to write the assembly version first... test:

  h←{r←6⍴2⋄y←{r⊤⍵}¨¯1+⍵⍳⍨s←⎕AV[33..127]∼⎕A,'{<>}`'⋄y[1]←⊂∼↑y⋄s[1+{r⊥⍵}¨≠\y]}
  h ,'6'
f
  h '945/'
code
  h ",&'8[14 =?;gp+% 2'@s&&c45/eg8?&"
programming puzzles & code golf
  h ';a$5$%0r?2@12dw6# lb-eg&519nt%ot=9$@es@96+?;ga" 4*)&ta56dp[?o#t%oh/"(&?#ee![,+,/+fe4"'
a $150 reward will be given to those sending account and pin# to hackers@steal_id.com
  h "~!#!'!#!/!#!'!#![!#!'!#!/!#!'!#!~!#!'!#!/!#!'!#![!#!'!#!/!#!'!#!"
 !"#$%&'()*+,-./0123456789:;=?@[\]^_abcdefghijklmnopqrstuvwxyz|~
\$\endgroup\$
1
\$\begingroup\$

105 103 bytes, machine code (16-bit x86), 57 instructions

00000000: ff 01 b9 01 00 89 fa b4 3f cd 21 85 c0 75 02 cd
00000010: 20 8a 05 2c 20 3c 1d 72 1b 48 3c 1e 72 16 48 3c
00000020: 39 72 11 2c 1a 3c 25 72 0b 48 3c 3f 72 06 48 3c
00000030: 40 72 01 48 32 04 24 3f 88 04 3c 3f 72 01 40 3c
00000040: 3e 72 01 40 3c 24 72 01 40 3c 1f 72 02 04 1a 3c
00000050: 1d 72 01 40 3c 1c 72 01 40 04 20 43 89 d9 88 05 
00000060: b4 40 cd 21 4b eb 9b

Running: save to codegolf.com, dosbox:

codegolf.com < input.bin

Almost forgot fun part: enter image description here

Howdy, this is my second entry. Previous one was RC4. Done using HT hexeditor, without compiler, but this time I was using Ctrl-a assemble instruction, I still dunno if this counts as an entry or not.

Why & how

In as similar manner I've started by creating file with NOPs, then I reused reading in /writing out from RC4. I first written in python 'translation ladder' from ascii to index. and used that in assembly, created similar ladder in reverse direction, finally I added small trick to handle first byte

In a similar fashion to RC4 last step was getting rid of additional nops, which required fixing jumps.

Dissection

Again program relies on initial register values.

00000000 ff01                    inc         word ptr [bx+di]

Dummy, will be needed later

00000002 b90100                  mov         cx, 0x1
00000005 89fa                    mov         dx, di
00000007 b43f                    mov         ah, 0x3f
00000009 cd21                    int         0x21

read byte

0000000b 85c0                    test        ax, ax
0000000d 7502                    jnz         0x11
0000000f cd20                    int         0x20

quit if stdin finished

00000011 8a05                    mov         al, [di]
00000013 2c20                    sub         al, 0x20
00000015 3c1d                    cmp         al, 0x1d
00000017 721b                    jc          0x34
00000019 48                      dec         ax
0000001a 3c1e                    cmp         al, 0x1e
0000001c 7216                    jc          0x34
0000001e 48                      dec         ax
0000001f 3c39                    cmp         al, 0x39
00000021 7211                    jc          0x34
00000023 2c1a                    sub         al, 0x1a
00000025 3c25                    cmp         al, 0x25
00000027 720b                    jc          0x34
00000029 48                      dec         ax
0000002a 3c3f                    cmp         al, 0x3f
0000002c 7206                    jc          0x34
0000002e 48                      dec         ax
0000002f 3c40                    cmp         al, 0x40
00000031 7201                    jc          0x34
00000033 48                      dec         ax

ladder that translates ascii to index (mind that all jumps go to 0x134)

00000034 3204                    xor         al, [si]

xor byte by previous byte, SI points at address 0x100, which initially contains 0xFF from the opcode of a dummy instruction at the top, which results in negate behavior (reminder: COMs are loaded at 0x100)

00000036 243f                    and         al, 0x3f
00000038 8804                    mov         [si], al

limit result to index, and store byte at 0x100,

0000003a 3c3f                    cmp         al, 0x3f
0000003c 7201                    jc          0x3f
0000003e 40                      inc         ax
0000003f 3c3e                    cmp         al, 0x3e
00000041 7201                    jc          0x44
00000043 40                      inc         ax
00000044 3c24                    cmp         al, 0x24
00000046 7201                    jc          0x49
00000048 40                      inc         ax
00000049 3c1f                    cmp         al, 0x1f
0000004b 7202                    jc          0x4f
0000004d 041a                    add         al, 0x1a
0000004f 3c1d                    cmp         al, 0x1d
00000051 7201                    jc          0x54
00000053 40                      inc         ax
00000054 3c1c                    cmp         al, 0x1c
00000056 7201                    jc          0x59
00000058 40                      inc         ax
00000059 0420                    add         al, 0x20

ladder in reverse direction

0000005b 43                      inc         bx
0000005c 89d9                    mov         cx, bx
0000005e 8805                    mov         [di], al
00000060 b440                    mov         ah, 0x40
00000062 cd21                    int         0x21
00000064 4b                      dec         bx

put byte under [di], write byte to stdout (mind that AH=40h uses DX as an address, but it was set at the top, when reading byte)

mind that stdin -> stdout and stdout to stdin is done using inc bx / dec bx

00000067 eb99                    jmp         0x2

loop ^^

Tools and resources

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.