Skip to content

Instantly share code, notes, and snippets.

@gatesphere
Created October 4, 2012 19:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gatesphere/3835917 to your computer and use it in GitHub Desktop.
Save gatesphere/3835917 to your computer and use it in GitHub Desktop.
Diffie-Hellman key exchange and XOR encryption
#!/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