Last active
June 8, 2021 21:54
-
-
Save JoshCheek/acb39f3b87ef633fc5fc652a961e7083 to your computer and use it in GitHub Desktop.
Edit the secret message in encrypt.rb, run it, tell me what it prints
This file contains hidden or 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
| # I used this to generate the public / private keypair and sign them | |
| require 'base64' | |
| require 'openssl' | |
| private_key = OpenSSL::PKey::RSA.generate 1024 | |
| public_key = private_key.public_key | |
| signature = private_key.sign | |
| OpenSSL::Digest::SHA256.new, | |
| public_key.to_pem | |
| ) | |
| File.write 'josh.sig', Base64.strict_encode64(signature) | |
| File.write 'josh.pub', public_key.to_pem | |
| File.write 'josh.priv', private_key.to_pem |
This file contains hidden or 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
| # The only thing you need to edit is what's in this heredoc: | |
| secret_message = <<SECRET | |
| write your | |
| secret message | |
| in here | |
| SECRET | |
| # These are in the standard library, you shouldn't need to install anything | |
| require 'openssl' | |
| require 'base64' | |
| # Verify my public key: I signed it with my private key | |
| josh_pub_key = OpenSSL::PKey.read <<PEM | |
| -----BEGIN PUBLIC KEY----- | |
| MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDOI0DWR7hdRlho4lNu5ymHOVzw | |
| M1uHGHmfEpt2N6givU2ty0j1+/NYYLGf6pSi3KwBSBShqPe4mZ5DFByzCl3K5kDx | |
| eDmKN9B8qGjzftbo+IMSimNGyP6POdvpr9p+1lBsWCSzX5joxSrmSkKVHhMWXMVd | |
| ogG5vFyuRmTfqlegKQIDAQAB | |
| -----END PUBLIC KEY----- | |
| PEM | |
| josh_sig = Base64.strict_decode64 'Yga8FwuplMTskFEJrFOMeOegVikBIbMdySq0VXsnmB2wjOxG2YI8+pzeibOLmvdU3j8GVpUJZSkfkKeCR7GYO0X14MCVt175qr7Is8L/+qlg/qbRjo0rXup0sUOcM/ohgb9L5Yk9FrWY24uavkplL7ceuC8hQoEBtUIFGYIVz40=' | |
| verified = josh_pub_key.verify( | |
| OpenSSL::Digest::SHA256.new, | |
| josh_sig, | |
| josh_pub_key.to_pem | |
| ) | |
| unless verified | |
| $stderr.puts 'Invalid signature, DO NOT TRUST!' | |
| exit 1 | |
| end | |
| # Generate a Cipher to encrypt the data | |
| cipher = OpenSSL::Cipher::AES.new(256, :CBC) | |
| cipher.encrypt | |
| key = cipher.random_key | |
| iv = cipher.random_iv | |
| # encrypt / encode / print all the data | |
| puts Base64.strict_encode64(josh_pub_key.public_encrypt(key)) | |
| puts | |
| puts Base64.strict_encode64(josh_pub_key.public_encrypt(iv)) | |
| puts | |
| puts Base64.encode64 cipher.update(secret_message) + cipher.final |
This file contains hidden or 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
| encrypted_key = "FIXME" | |
| encrypted_iv = "FIXME" | |
| encrypted_msg = <<MSG | |
| FIXME | |
| MSG | |
| require 'openssl' | |
| require 'base64' | |
| josh_priv_key = OpenSSL::PKey.read File.read 'josh.priv' | |
| decipher = OpenSSL::Cipher::AES.new(256, :CBC) | |
| decipher.decrypt | |
| decipher.key = key = josh_priv_key.private_decrypt Base64.strict_decode64 encrypted_key | |
| decipher.iv = iv = josh_priv_key.private_decrypt Base64.strict_decode64 encrypted_iv | |
| puts decipher.update(Base64.decode64 encrypted_msg) + decipher.final |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment