Created
October 4, 2012 19:43
-
-
Save gatesphere/3835917 to your computer and use it in GitHub Desktop.
Diffie-Hellman key exchange and XOR encryption
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env io | |
Person := Object clone do( | |
pub ::= nil // "composed" key | |
priv ::= nil // private key | |
shared ::= nil // generated key | |
init := method( | |
self setPub(nil) | |
self setPriv(nil) | |
self setShared(nil) | |
) | |
with := method(salt, priv, | |
self clone setPriv(priv) setPub(priv ^ salt) | |
) | |
gen_shared := method(other, | |
self setShared(self priv ^ other) | |
) | |
encrypt := method(data, | |
self shared ^ data | |
) | |
asString := method( | |
"pub: #{self pub} priv: #{self priv}" interpolate | |
) | |
) | |
salt := 42 // shared secret | |
data := 123 // cleartext | |
alice := Person with(salt, 23) | |
bob := Person with(salt, 12) | |
writeln("Environment information:") | |
writeln("public salt: #{salt}" interpolate) | |
writeln("data to encrypt: #{data}" interpolate) | |
writeln("Alice - #{alice}" interpolate) | |
writeln("Bob - #{bob}" interpolate) | |
writeln("Swapping keys...") | |
alice gen_shared(bob pub) | |
bob gen_shared(alice pub) | |
writeln("Alice - shared: #{alice shared}" interpolate) | |
writeln("Bob - shared: #{bob shared}" interpolate) | |
writeln("Alice encrypts, Bob decrypts: #{bob encrypt(alice encrypt(data))}" interpolate) | |
writeln("Bob encrypts, Alice decrypts: #{alice encrypt(bob encrypt(data))}" interpolate) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment