emrekizildas / EntityFrameworkCore.EncryptColumn

Encrypt & Decrypt your databases columns using EntityFramework Core.
MIT License
87 stars 40 forks source link

Security problem with the IV AES #9

Open ByteDecoder opened 2 years ago

ByteDecoder commented 2 years ago

Since the library is always using the same IV, is potentially fragile in terms of security.

byte[] iv = new byte[16];

Here always the IV vector is initialized to the same value. What it should be, every encrypted value must have a different IV and also add this IV block to the final ciphertext, to have the different values as result.

The IV is not a secret, so is safe to be included in the final ciphertext.

The .Net Aes already has a random IV value when is created:

` using (Aes myAes = Aes.Create()) {

        // Encrypt the string to an array of bytes.
        byte[] encrypted = EncryptStringToBytes_Aes(original, myAes.Key, myAes.IV);

`

If you see, the AES instance when is used with Aes.Create() already create an IV.

A possible solution is to create a new buffer that combines these two values as:

Buffer.BlockCopy(iv, 0, result, 0, iv.Length); Buffer.BlockCopy(encryptedContent, 0, result, iv.Length, encryptedContent.Length);

An also, since probably is using the library, should probably be needed to create a migration plan to encrypt properly the actual data encrypted by the library by projects that used it.

For the decryption, you need to split the hypertext and the IV from the block, and from there, you can use the key, and the unique IV to decrypt the value.

emrekizildas commented 2 years ago

If we make this transition, can we continue to decrypt data encrypted using the previous IV?

ByteDecoder commented 2 years ago

if you make a change like this, the previous data encrypted will need some sort of retroactive data update, since you are not storing an IV in the final ciphertext. Since you always are using a new byte[16] a data upgrade for previous records could be easily done. In other words, this will be a breaking change for the persons that use this for their data.

On the other hand, the new encryptions that will be fine using this secure approach, since the ciphertext will be concatenated with their generated IV, and from there can be decrypted.