Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
[1] pry(main)> data = "Vare, very confidential data"
=> "Vare, very confidential data"
[2] pry(main)> require "openssl"
=> true
[3] pry(main)> cipher = OpenSSL::Cipher::AES.new(128, :CBC)
=> #<OpenSSL::Cipher::AES:0x007f9332ca8df8>
[4] pry(main)> cipher.encrypt
=> #<OpenSSL::Cipher::AES:0x007f9332ca8df8>
[5] pry(main)> key = cipher.random_key
=> "\xFE\xCF\xF8G}\xE5\xCE\xCD\x9CP+\xBF\xC1)?j"
[6] pry(main)> iv = cipher.random_iv
=> "\xF4\xD3\xED\xB7\x9Dp\x02u\xCAoA\xD5\x89\x8AT\x88"
[7] pry(main)> encrypted = cipher.update(data) + cipher.final
=> "\x9B\x16\xEC\x127\x83\xA9\x93\x8B\x00\xE0\x80\xFE\xFE\xDE0\xF50b\x05\xAE\xCF\n\xCA3\xAB\x03%\xE2\xFEc\f"
[8] pry(main)> decipher = OpenSSL::Cipher::AES.new(128, :CBC)
=> #<OpenSSL::Cipher::AES:0x007f9332e2b4f0>
[9] pry(main)> decipher.decrypt
=> #<OpenSSL::Cipher::AES:0x007f9332e2b4f0>
[10] pry(main)> decipher.key = key
=> "\xFE\xCF\xF8G}\xE5\xCE\xCD\x9CP+\xBF\xC1)?j"
[11] pry(main)> decipher.iv = iv
=> "\xF4\xD3\xED\xB7\x9Dp\x02u\xCAoA\xD5\x89\x8AT\x88"
[12] pry(main)> plain = decipher.update(encrypted) + decipher.final
=> "Vare, very confidential data"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment