-
Notifications
You must be signed in to change notification settings - Fork 0
/
lightning-crypto-nash.rb
43 lines (31 loc) · 1.65 KB
/
lightning-crypto-nash.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#!/usr/bin/env ruby
require 'openssl'
KEY_LENGTH = 4096 # Key size (in bits)
digest_function = OpenSSL::Digest::SHA256.new # Predetermined signature digest function
# Generate RSA private/public key pairs for both parties...
keypair_snowden = OpenSSL::PKey::RSA.new(KEY_LENGTH)
keypair_mwrc = OpenSSL::PKey::RSA.new(KEY_LENGTH)
# Public key export for exchange between parties...
pubkey_snowden = OpenSSL::PKey::RSA.new(keypair_snowden.public_key.to_der)
pubkey_meetup = OpenSSL::PKey::RSA.new(keypair_mwrc.public_key.to_der)
# Plain text messages...
message_to_snowden = 'You are a patriot!'
message_to_meetup = "Russia is really nice this time of year..."
# Generate digital signatures using private keys...
signature_meetup = keypair_mwrc.sign(digest_function, message_to_snowden)
signature_snowden = keypair_snowden.sign(digest_function, message_to_meetup)
# Encrypt messages using the other party's public key...
encrypted_for_snowden = pubkey_snowden.public_encrypt(message_to_snowden) #from Meetup
encrypted_for_meetup = pubkey_meetup.public_encrypt(message_to_meetup) #from Snowden
# Decrypt messages using own private keys...
decrypted_snowden = keypair_snowden.private_decrypt(encrypted_for_snowden)
decrypted_meetup = keypair_mwrc.private_decrypt(encrypted_for_meetup)
# Signature validation and console output...
if pubkey_meetup.verify(digest_function, signature_meetup, decrypted_snowden)
puts "Edward Snowden received from @mwrc:"
puts "\"#{message_to_snowden}\"\n\n"
end
if pubkey_snowden.verify(digest_function, signature_snowden, decrypted_meetup)
puts "@mwrc received from Edward Snowden:"
puts "\"#{message_to_meetup}\""
end