OpenSSL TripleDES algorithm in Ruby
OpenSSL has a number of different ciphers available for use when encrypting data. An algorithm I found myself using recently with little discussion on the web is the Triple DES cipher with cipher-block chaining mode (CBC). Triple DES is the name for the Triple Data Encryption Algorithm block cipher.
The second last line is particularly useful to know since the encryption/decryption algorithm operates on 96 bytes at a time, meaning, if you simply call the
update method once, the output will be the first 96 bytes of the original data string. So you want to make sure that you call the
final method on the cipher to retrieve the remainder. This is particularly useful to remember when you use Base64 encoding/decoding to transmit your data over email
or the web. Consider the following:
A word of warning, it is recommended that that you use a passphrase to generate the initialization vector and cipher key instead of setting these manually as you may inadvertently expose your encrypted data to vulnerabilities. If setting the IV, make sure it is at least 8 bytes, and that the cipher key is at least 24 bytes.