insecurity_of_XOR.sf 857 B

12345678910111213141516171819202122
  1. #!/usr/bin/ruby
  2. # XOR can be insecure!
  3. var key1 = "a"*64 # Alice key
  4. var key2 = "z"*64 # Bob key
  5. var msg = "Hello world! How are you doing?"
  6. var c1 = (msg ^ key1) # Alice encrypts the message with her private key and sends it to Bob
  7. var c2 = ( c1 ^ key2) # Bob encrypts the encrypted message with his private key and sends the message back to Alice
  8. var c3 = ( c2 ^ key1) # Alice removes her encryption layer and sends the message back to Bob
  9. say (c3 ^ key2) # Bob can now remove his layer of encryption and read the message
  10. # However, this is insecure!
  11. # Someone that intercepts the encrypted messages, can decrypt them!
  12. say (c2 ^ c3) # Alice's private key
  13. say (c1 ^ c2) # Bob's private key
  14. say (c1 ^ c2 ^ c3) # The original message decrypted from the encrypted messages