Dreaming of ciphers
function my_stream_cipher($plaintext, $key) { $ciphertext = $plaintext; $sha = sha1($key); for ($i = 0; $i < 5; $i++) { mt_srand( hexdec( substr($sha, ( $i * 8 ), 8 ) ) ); for ($j = 0; $j < strlen($plaintext); $j++) $ciphertext{$j} = chr( ord( $ciphertext{$j} ) ^ mt_rand(0, 255) ); } return $ciphertext; }
I was trying to sleep this morning but was harassed endlessly by a really simple cipher that wouldn't stop running simulations in my head. It seemed like it'd be partially useful for some PHP projects I'm working on so I got up, fired up Aptana, and typed it up. A little google-fu revealed it to be a 5-pass vernam cipher.
This is how it works, in a nutshell:
- Set current ciphertext to plaintext
- Break the SHA1 of the key into 5 individual 32-bit ints
- For each int:
- Seed the RNG with the int to generate a stream of random characters
- XOR the current ciphertext with the stream
Can anyone spot the obvious flaw in this approach?
