I have seen some code piece (for example, in Ruby)
require 'openssl'
def encrypt_aes_256_cbc(plain_text, encrypt_key)
cipher = OpenSSL::Cipher::AES.new(256, :CBC)
cipher.encrypt
iv = cipher.random_iv
cipher.key = encrypt_key.ljust(cipher.key_len, '\0').slice(0, 32)
encrypted = cipher.update(plain_text) + cipher.final
(encrypted + iv).unpack('H*').first
end
def decrypt_aes_256_cbc(encrypted_text, encrypt_key)
cipher = OpenSSL::Cipher::AES.new(256, :CBC)
cipher.decrypt
raw_data = [encrypted_text].pack('H*')
cipher.iv = raw_data.slice(raw_data.length - 16, 16)
cipher.key = encrypt_key.ljust(cipher.key_len, '\0').slice(0, 32)
cipher.update(raw_data.slice(0, raw_data.length - 16)) + cipher.final
end
This encryption exposes IV to the result,
suppose a hacker gets the result of encrypt_aes_256_cbc
, then he gets the IV,
does this mean that the CBC mode become non sense?
Is this implementation OK?