I'm trying to work out which AES cipher is best to use. I'm working with the OpenSSL ruby gem.
While testing the various options I noticed that most of the ciphers result in partial or even full decryption regardless of the IV value.
I'm just wondering if this is something I should be concerned about, or even if it is a bug that the gem maintainers should be aware of.
Eg: CBC, CFB8, CFB1, CFB, CBC-HMAC-SHA256, and CBC-HMAC-SHA1, only the first 16 bytes remain encrypted.
Eg: ECB allows full decryption regardless of IV
This is the ruby code I've used for testing.
ciphers = [
'AES-256-CBC', 'AES-256-CTR', 'AES-256-XTS',
'AES-256-OFB', 'AES-256-OCB', 'AES-256-ECB',
'AES-256-CFB8', 'AES-256-CFB1', 'AES-256-CFB',
'AES-256-CBC-HMAC-SHA256', 'AES-256-CBC-HMAC-SHA1',
]
def encrypt(plain_text, pwd, salt, cipher_name)
cipher = OpenSSL::Cipher.new(cipher_name)
cipher.encrypt
cipher.random_iv # purposefully set to random
cipher.key = OpenSSL::PKCS5.pbkdf2_hmac(pwd, salt, 20000, cipher.key_len, OpenSSL::Digest::SHA256.new)
encrypted = cipher.update(plain_text)
encrypted << cipher.final
end
def decrypt(encrypted, pwd, salt, cipher_name)
cipher = OpenSSL::Cipher.new(cipher_name)
cipher.decrypt
cipher.random_iv # purposefully set to random
cipher.key = OpenSSL::PKCS5.pbkdf2_hmac(pwd, salt, 20000, cipher.key_len, OpenSSL::Digest::SHA256.new)
decrypted = cipher.update(encrypted)
decrypted << cipher.final
end
ciphers.each do |cipher_name|
pwd = 'some hopefully not to easily guessable password'
salt = OpenSSL::Random.random_bytes(16)
plain_text = "Spicy jalapeno tail ribeye dolore ut. Pork belly ut porchetta culpa aliquip brisket shank est. Pastrami ribeye porchetta cow qui."
begin
puts
puts '== %s ==' % cipher_name
encrypted = encrypt(plain_text, pwd, salt, cipher_name)
puts decrypt(encrypted, pwd, salt, cipher_name)
rescue => e
puts e.message
end
end
For those without ruby, this is the output of the above code:
== AES-256-CBC ==
��l�~��u@�=o� ail ribeye dolore ut. Pork belly ut porchetta culpa aliquip brisket shank est. Pastrami ribeye porchetta cow qui.
== AES-256-CTR ==
/& q]$���{�YhwH�X*�N���.p�E�̕;�� 3|t�L
v�'��T�KCأ���y�X���ՎR`/��]d`L�WI�"�?P���Ǖ�B|E�u5������Zq����LϠ�4
== AES-256-XTS ==
^��:ok
@6�4^O�cNJ��'� C/ ���V�(�)k5��)��S
^�3���4�r��A���q�=����p�^�d3;� �.U˕|k�U/�ʝ��y��qd�/���7HΒ����l�e
== AES-256-OFB ==
iH�"�=�tL�eG��Z~o
��moӦQ!��<z��vր2}�#6�
���%�oM��XI#y�t���($���"~
== AES-256-OCB ==
== AES-256-ECB ==
Spicy jalapeno tail ribeye dolore ut. Pork belly ut porchetta culpa aliquip brisket shank est. Pastrami ribeye porchetta cow qui.
== AES-256-CFB8 ==
�xYa���ShR�
ail ribeye dolore ut. Pork belly ut porchetta culpa aliquip brisket shank est. Pastrami ribeye porchetta cow qui.
== AES-256-CFB1 ==
+��g~ᲆ$e�
��ail ribeye dolore ut. Pork belly ut porchetta culpa aliquip brisket shank est. Pastrami ribeye porchetta cow qui.
== AES-256-CFB ==
���=�?)�2�96`�2�ail ribeye dolore ut. Pork belly ut porchetta culpa aliquip brisket shank est. Pastrami ribeye porchetta cow qui.
== AES-256-CBC-HMAC-SHA256 ==
_?��?���p��}ail ribeye dolore ut. Pork belly ut porchetta culpa aliquip brisket shank est. Pastrami ribeye porchetta cow qui.
== AES-256-CBC-HMAC-SHA1 ==
[˲��H"`��.qv�ail ribeye dolore ut. Pork belly ut porchetta culpa aliquip brisket shank est. Pastrami ribeye porchetta cow qui.