none
AesCng Class RRS feed

  • Question

  • Hello... I upgraded to the .NET Framework 4.6.2 so I could take advantage of the new AesCng Class in the Cryptography namespace.

    However, when comparing it "side by side" with regular Aes there seems to be no difference. For example this function...

    private string AesCrypto()
    {
        string DataToEncrypt = "Hello!";
        byte[] Key = new byte[32] { };
        byte[] IV = new byte[16] { };
    
        using (AesCryptoServiceProvider AES = new AesCryptoServiceProvider())
        {
            byte[] DataByteArray = Encoding.UTF8.GetBytes(DataToEncrypt);
            MemoryStream ms = new MemoryStream();
            CryptoStream cs = new CryptoStream(ms, AES.CreateEncryptor(Key, IV), CryptoStreamMode.Write);
            cs.Write(DataByteArray, 0, DataByteArray.Length);
            cs.FlushFinalBlock();
    
            return Convert.ToBase64String(ms.ToArray());
        }
    }

    returns the value: ylsH2mi5zN3DOxdvL00fzQ==

    And so does this function:

    private string AesCrypto()
    {
        string DataToEncrypt = "Hello!";
        byte[] Key = new byte[32] { };
        byte[] IV = new byte[16] { };
    
        using (AesCng AES = new AesCng())
        {
            byte[] DataByteArray = Encoding.UTF8.GetBytes(DataToEncrypt);
            MemoryStream ms = new MemoryStream();
            CryptoStream cs = new CryptoStream(ms, AES.CreateEncryptor(Key, IV), CryptoStreamMode.Write);
            cs.Write(DataByteArray, 0, DataByteArray.Length);
            cs.FlushFinalBlock();
    
            return Convert.ToBase64String(ms.ToArray());
        }
    }

    They both return the same result: ylsH2mi5zN3DOxdvL00fzQ==

    The only difference is one uses AesCryptoServiceProvider and the other uses AesCng.

    Should the results be the same? Or should AesCng be different? Perhaps I am not implementing it correctly?

    I could not find any clear examples on microsoft.com or any other site, explaining the use of AesCng.

    Any help would be greatly appreciated. Thank you!

    Wednesday, February 13, 2019 8:52 AM

All replies

  • Hi SS_Tech,

    Thank you for posting here.

    For your question, you want to know the difference between AesCryptoServiceProvider and AesCng by returning the value.

    Based on my search, their results should be the same.

    The following link shows that the relation between AesCryptoServiceProvider, AesCng, AesManaged, and AES, you could read it.

    https://stackoverflow.com/questions/45473884/what-is-the-difference-between-aes-and-aesmanaged

    Hope my explanation could be helpful.

    Best regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Thursday, February 14, 2019 6:07 AM
    Moderator
  • Hello Jack,

    I appreciate your answer on this issue. It makes sense that  AesManaged, AesCryptoServiceProvider, and AesCng are concrete implementations of AES in managed code. But I don't see the difference in encryption strength.

    According to Microsoft the AesCng Class provides a Cryptography Next Generation (CNG) implementation of the Advanced Encryption Standard (AES) algorithm.

    https://docs.microsoft.com/en-us/dotnet/api/system.security.cryptography.aescng?view=netframework-4.7.2

    So, even though they are all based off of AES, I was expecting that using AesCng for encryption would be better than using AesCryptoServiceProvider (hence Next Generation).

    But each encrypts a string identically. I was expecting AesCng to output a different result if it is stronger encryption. But I guess it's not.

    Thank you!

    Friday, February 15, 2019 12:27 AM
  • Hi

    Is your problem solved? If so, please post "Mark as answer" to the appropriate answer, so that it will help other members to find the solution quickly if they face a similar issue.
    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.



    Monday, February 18, 2019 7:05 AM
    Moderator