I am trying to make AES-256 CBC encryption work in PHP, Ruby (using SymmetricEncryption) and Javascript (using CryptoJS). As for the first 2:
<?php
openssl_encrypt(
'Hello!', 'aes-256-cbc',
'1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF',
0,
'1234567890ABCDEF1234567890ABCDEF'
); // => 'BAd5fmmMTvRE4Ohvf3GpCw=='
ruby_cipher = SymmetricEncryption::Cipher.new(
key: "1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF",
iv: "1234567890ABCDEF1234567890ABCDEF",
cipher_name: 'aes-256-cbc'
)
ruby_cipher.encrypt("Hello!") # => 'BAd5fmmMTvRE4Ohvf3GpCw=='
But according to this answer the above key/iv only provide 128 bit security.
PHP and Ruby take the key and IV as a binary string. They don't assume that it is Hex-encoded. So, although this key has 256 bits in it, the security is actually only 128 bits, because each character has only 4 bit in a Hex-encoded string.
So using only half of the key/iv provides the same encryption result in CryptoJS.
CryptoJS.AES.encrypt(
"Hello!",
CryptoJS.enc.Utf8.parse('1234567890ABCDEF1234567890ABCDEF'),
iv: CryptoJS.enc.Utf8.parse('1234567890ABCDEF')
).toString() // 'BAd5fmmMTvRE4Ohvf3GpCw=='
How do I generate string key and iv's that provide 256 bit security?