AES ECB vs CBC

Which encryption to use ?

ECB is faster. ECB, Electronic Code Book, encrypts two identical blocks into two identical cipher texts. Because of this, it is considered insecure.

CBC is slower. CBC, Cipher Block Chaining, involves XORing the plaintext of each block with the previous block’s ciphertext before encrypting. This ensures that if two blocks of plaintext are identical they will produce totally unrelated ciphertext blocks. The “slower” is due to the time required to perform the XOR operation.



Using ecb:

Key used: 14SZXSWWcB1MXZD09Y1tMOri2kYRsUAHBpjXULetJ8s=

Plain text 1: sahkfaskjdaasfhkasfasdf


Cipher text: y3Pjdo1Ffkc0Db4IRBRKOPQIerrifGgAiZA8uUS8yLc=


Now changing plain text 1 on second position (a changes to c) 

Plain text 2: schkfaskjdaasfhkasfasdf


Cipher test generated: 4uAHU8xLRn/iX+udKBLxjPQIerrifGgAiZA8uUS8yLc=



Here ecb generates identical cipher (only first characters have changed)


Encryption with mysql:
SET SESSION block_encryption_mode = 'aes-256-ecb';
select TO_BASE64(AES_ENCRYPT('sahkfaskjdaasfhkasfasdf', (FROM_BASE64('14SZXSWWcB1MXZD09Y1tMOri2kYRsUAHBpjXULetJ8s='))));



Using CBC

Key used: 14SZXSWWcB1MXZD09Y1tMOri2kYRsUAHBpjXULetJ8s=

Plain text 1: sahkfaskjdaasfhkasfasdf

IV : lkfkeitpsksn3lir


Cipher: zRhvPaFCOqrV0F6WjnGYxSEEFzxDNkFgEaScvmrlAqw=


Plain test 2 : schkfaskjdaasfhkasfasdf


Cipher : pMVYPJME4aU7aZPuJ335KdzT2TvLvxYBzX3wdh0FWGU=


Here cbc generates completely new cipher (although the plain text is identical)



Mysql implementation:

SET SESSION block_encryption_mode = 'aes-256-cbc';

select to_base64(AES_ENCRYPT('sahkfaskjdaasfhkasfasdf', (FROM_BASE64('14SZXSWWcB1MXZD09Y1tMOri2kYRsUAHBpjXULetJ8s=')),'lkfkeitpsksn3lir'));




Key note:
The key must be 16/24/32 bytes long. Generating random key with openssl:
openssl rand -base64 16/24/32


The initialization vector must be 16 bytes long.





Comments

Popular posts from this blog

Brief Look Into Mysql Enterprise Audit

Mysql CharacterSet and Collation

Mysql Enterprise Thread Pool