Cryptopals 5 - Implementing repeated-key XOR


Challenge 5 in Cryptopals.

Today’s challenge is another of those preparatory work that might seem like a chore (or a blessing! More difficult encryption, more resistance!) but at the end of the day we already suspect what lies ahead.

My solution is pretty bare-bones and leaves a lot to be desired:

sub repeated_xor_encryption ($plaintext, $key) {
   my $plen = length $plaintext;
   my $klen = length $key;
   my $reps = int($plen / $klen) + 1;
   my $megakey = substr($key x $reps, 0, $plen);
   return $plaintext ^. $megakey;

I first build a $megakey by concatenating enough input $key copies so that I can cover the whole $plaintext, then apply our xor friend that we learned a few challenges ago.

This is messy, possibly from multiple points of view. Space-wise it’s inefficient; time-wise, it probably gives out too much information about the key length.

Probably the best thing to do would be to operate char-by-char, so that we only use the strictly necessary space and perform a constant operation for each character, yielding a much more stable time. Whatever, anyway.

Stay safe and secure!

Comments? Octodon, Twitter, GitHub, Reddit, or drop me a line!