C# Example of AES256 encryption using System.Security.Cryptography.Aes

前端 未结 3 1322
攒了一身酷
攒了一身酷 2021-01-01 12:07

I need to implement AES 256 encryption /decryption and I haven\'t been able to find an example that works correctly.

MSDN suggests that I should use the AES class.

3条回答
  •  无人及你
    2021-01-01 12:52

    Once I'd discovered all the information of how my client was handling the encryption/decryption at their end it was straight forward using the AesManaged example suggested by dtb.

    The finally implemented code started like this:

        try
        {
            // Create a new instance of the AesManaged class.  This generates a new key and initialization vector (IV).
            AesManaged myAes = new AesManaged();
    
            // Override the cipher mode, key and IV
            myAes.Mode = CipherMode.ECB;
            myAes.IV = new byte[16] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; // CRB mode uses an empty IV
            myAes.Key = CipherKey;  // Byte array representing the key
            myAes.Padding = PaddingMode.None;
    
            // Create a encryption object to perform the stream transform.
            ICryptoTransform encryptor = myAes.CreateEncryptor();
    
            // TODO: perform the encryption / decryption as required...
    
        }
        catch (Exception ex)
        {
            // TODO: Log the error 
            throw ex;
        }
    

提交回复
热议问题