Created
September 17, 2013 16:07
-
-
Save ewalk153/6596535 to your computer and use it in GitHub Desktop.
Basic example script for encrypting/decrypting with 3DES.
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
| require 'openssl' | |
| require 'base64' | |
| # Generate with this: OpenSSL::Cipher::Cipher.new("des-ede-cbc").tap{|c| c.encrypt}.random_key | |
| key = "\xBEB\xBA;\xA5\xEA-\x1A4\x0E8\xAC\x13\xF1s9\xB2\x87\xD3D\x9A\xC2\xA8\xC7" | |
| def encrypt(message, key) | |
| des_cbc=OpenSSL::Cipher::Cipher.new("des-ede-cbc") | |
| des_cbc.encrypt | |
| des_cbc.key = key | |
| ciphertext = des_cbc.update(message) | |
| ciphertext << des_cbc.final | |
| Base64.encode64(ciphertext) | |
| end | |
| def decrypt(message_enc, key) | |
| des_cbc=OpenSSL::Cipher::Cipher.new("des-ede-cbc") | |
| des_cbc.decrypt | |
| des_cbc.key = key | |
| des_cbc.update(Base64.decode64(message_enc)) | |
| end | |
| # puts encrypt("hi boss, this better be good, asdfasdl;kfj;asdlkfja;sdlkfj;asldkfj", key) | |
| # puts decrypt("wxJqJkPmgEEdlEaPPieERgH7zCVJFmyCLa4MAUUx7T1/jN33E1/bLjhqIbTz\nah4IsytZpuytSC9J6j2ICg8U5rD6I+1S0Cxk\n", key) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment