Code Golf: XOR Encryption

Viewed 7015

From: Encryption Co.
To: x$*sj4 (that's you)

Your mission, should you choose to accept it, is to create a program in the shortest number of keystrokes that

  • Takes two filenames parameters (either command line or stdin), the first is a file containing the key and the second some message. Both files will be plain text.

  • Applies the key to the message using XOR encryption, overwriting the file.

Example:

Input File:

StackOverflow is Cool

Key:

Code Golf

Hex dump of Encrypted Output file:

0000000: 101b 0506 4b08 1909 1425 030b 1200 2e1c  ....K....%......
0000010: 4c25 2c00 080d 0a                        L%,....

For simplicity, assume files can fit in memory


This message will self-encrypt in 5... 4... 3... 2... 1...

     #####
    #### _\_  ________
    ##=-[.].]| \      \
    #(    _\ |  |------|
     #   __| |  ||||||||
      \  _/  |  ||||||||
   .--'--'-. |  | ____ |
  / __      `|__|[o__o]|
_(____nm_______ /____\____ 

XOR Encryption is impossible to crack if the size of the key is greater or equal to the size of the message and the key is generated by an unbiased random process. See: One-time pad. So no "Poor Encryption" here.

23 Answers

C#, 168:

using System.IO;class a{static void Main(string[] b){File.WriteAllBytes(b[0],File.ReadAllBytes(b[0]).Select((x,i)=>x^File.ReadAllBytes(b[1])[i%d.Length]).ToArray());}}

A functional solution. I saved variables by inlining the read operation which causes it to be executed over and over.

Related