XOR decryption


Fork me on GitHub
2015-05-17

Problem 059: XOR decryption

Description:

Each character on a computer is assigned a unique code and the preferred standard is ASCII (American Standard Code for Information Interchange). For example, uppercase A = 65, asterisk (*) = 42, and lowercase k = 107.

A modern encryption method is to take a text file, convert the bytes to ASCII, then XOR each byte with a given value, taken from a secret key. The advantage with the XOR function is that using the same encryption key on the cipher text, restores the plain text; for example, 65 XOR 42 = 107, then 107 XOR 42 = 65.

For unbreakable encryption, the key is the same length as the plain text message, and the key is made up of random bytes. The user would keep the encrypted message and the encryption key in different locations, and without both "halves", it is impossible to decrypt the message.

Unfortunately, this method is impractical for most users, so the modified method is to use a password as a key. If the password is shorter than the message, which is likely, the key is repeated cyclically throughout the message. The balance for this method is using a sufficiently long password key for security, but short enough to be memorable.

Your task has been made easy, as the encryption key consists of three lower case characters. Using cipher.txt (right click and 'Save Link/Target As...'), a file containing the encrypted ASCII codes, and the knowledge that the plain text must contain common English words, decrypt the message and find the sum of the ASCII values in the original text.


Solution:
v###    0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 # ...
 ### ## 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 # ...
 ###    0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 # ...
 ## ### 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 # ...
 ###    0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 # ...
 ##     0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 # ...
        0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 # ...
        0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 # ...
        0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 # ...
        0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 # ...
        0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 # ...
        0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 # ...
        0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 # ...
                                                                                                             # ...
                                                                                                             # ...
> 89+10p 020p 030p 023p              >20g3*10gg"0"-55+*20          v                                         # ...
                                      v"d"\+8%"d":g03+-"0"gg01+1*3g<                                         # ...
79 59 12 02 79 35 08 28 20 02 03 68   >/p30g1+30p20g1+:93+-#v_$10g1v                                         # ...
08 09 68 45 00 12 09 67 68 04 07 05  |-2+!-1g02!-"u"  g01p02<0p01+ <                                         # ...
23 27 01 21 79 85 78 79 85 71 38 10                                                                          # ...
71 27 12 02 79 06 02 08 13 09 01 13                        v1p12<vp12/2g12p11/2g11+*p13*2:g <                # ...
09 08 68 19 07 01 71 56 11 21 11 68  >051p > 061p > 51g61g   11p^>11g21g+!#v_11g2%21g2%+2%31^                # ...
06 03 22 02 14 00 30 79 01 31 06 23        |- **88 2p15:<  > 31p0^         >51g"m"+61gp     v                # ...
19 10 00 73 79 44 02 79 19 06 28 68  v"+~h"<      |                         -**882 p16:+1g16<                # ...
16 06 16 15 79 35 08 11 72 71 14 10  +            >51g1+^                                                    # ...
03 79 12 02 79 19 06 28 68 32 00 00  *   v                                                          <        # ...
73 79 86 71 39 01 71 24 05 20 79 13  >13p>13g:1-13p#v_                                                v      # ...
09 79 16 15 10 68 05 10 03 14 01 10  v+"a"%+*298:g31<                                       >      >^ 5      # ...
14 01 03 71 24 13 19 07 68 32 00 00  >12p892*+/:892*+%"a"+22p892*+/"a"+32pv                 $      $  5      # ...
73 79 87 71 39 01 71 12 22 02 14 16  v p430                               <                 $      $  6      # ...
02 11 68 02 25 01 21 22 16 15 06 10  >014p 55*6*8* >:3%1+24p::"d"%8+\"d"/g"m"+24g2gg :" "\`#^_:"~"`|  8      # ...
00 79 16 15 10 22 02 79 13 20 65 68                ^-1                        _v#-" "_v#-9p41+1:g41<  *      # ...
41 00 16 15 06 10 00 79 01 31 06 23                                                   >$$           ^ *      # ...
19 28 68 19 07 05 19 79 12 02 79 00  v                                         <      >$            ^ *      # ...
14 11 10 64 27 68 10 14 15 02 65 68                                            >      $             ^ 1      # ...
83 79 40 14 09 01 71 06 16 20 10 08  >:3%1+24p::"d"%8+\"d"/g"m"+24g2gg  :" "\`#^_:"~"`|               5      # ...
01 79 19 06 28 68 14 01 68 15 06 09  ^-1_v#:                                   _v#-"e"<               p      # ...
75 79 05 09 11 68 19 07 13 20 79 08     ^  :                            p43+1g43<                     0      # ...
14 09 01 71 08 13 17 10 23 71 03 13      >$34g23g`#v_                                               ^ 2      # ...
00 07 16 71 27 11 71 10 18 02 29 29                >34g23p 32g22g12g 43p53p63p                      ^ 5      # ...
08 01 01 73 79 81 71 59 12 02 79 08                                                                   p      # ...
14 08 12 19 79 23 15 06 10 02 28 68  v                                                                <      # ...
19 07 22 08 26 03 15 79 16 15 10 68  v                                                     p51-1<            # ...
03 14 22 12 01 01 20 28 72 71 14 10  >15g:"d"%8+\"d"/g"m"+15g3%4+3gg:25g+25p15g:"d"%8+\"d"/p15g:|            # ...
03 79 16 15 10 68 03 14 22 12 01 01                                                             >$25g.@      # ...
20 28 68 04 14 10 71 01 01 17 10 22                                                                          # ...
...
...
This program is too big to display/execute here, click [download] to get the full program.

Explanation:

This task had a lot of Befunge specific problems, but first let's look at the solving strategy:

  • We go through all 26^3 passwords and test look if they generate illegal characters (smaller 32 or bigger 127).
  • We also see if one of the first twelve characters is a space, assuming the first word is no longer than twelve characters.
  • The leaves us with around one-hundred viable passwords.
  • We use the fact that in the English language the letter e is pretty common and take the password with the highest count of e.
  • The rest is just decrypting the text and counting the values.

Now to our Befunge specific problems:

  • First we need to input the raw data. Our best call is inserting the data directly into the program and then parsing it in the first step into an array.
  • The next problem is the missing xor operator. To perform an xor operation we need to go through all bits and xor them individually (a xor b == (a+b) % 2). This is an extremely pricey operation. And to speed this up we generate an complete 128x128 xor look up-table with all possible xor operations.

All in all a not really optimal problem for Befunge but still a fun challenge


Interpreter steps: 37 085 853
Execution time (BefunExec): 6.30s (5.88 MHz)
Program size: 273 x 128
Solution: 107359
Solved at: 2015-05-17



made with vanilla PHP and MySQL, no frameworks, no bootstrap, no unnecessary* javascript