Search code examples
c#encryptionaesinitialization-vector

Can you attach aesIV to the data encrypted by AES cryptograpy algorithm?


Following the standard instruction for using AES algorithm, I have not been able to encrypt/decrypt some text properly using AES in C#. Though I found that I could if I attached the data called aesIV with the encrypted data separately. In my understanding aesIV is already embedded in the encrypted data. So I think it does not make security issues. Please let me ask whether I can do it or not.

Thank you in advance.


Solution

  • The most common option is to prepend the random IV during encryption to the cipher text. Then remove it from the cipher text set the IV during decryption. In the common managed crypto for C# the random IV property is automatically generated, so you can simply retrieve it and prepend it.

    See AesManaged and the example (where the IV is transmitted separately, but otherwise it is ok).

    If you decrypt directly after prepending the IV then you will get blocksize bytes of garbage. It's normally not the way to proceed, because it is as easy to remove and use the IV in advance as removing the garbage.

    As you assume, the IV may be transmitted in plain.