locked
AES Encryption 256 bit RRS feed

  • Question

  • User1588000814 posted

    Hello,

    I am encrypting data using the below AES code:

    Public AesIV As String = "1233123456789012"
    Public AesKey As String = "1233123456789012"
    
    Private Function aesEncrypt(strData As String) As String
    ' AesCryptoServiceProvider
    Dim aes As New AesCryptoServiceProvider()
    aes.BlockSize = 128
    aes.KeySize = 128
    aes.IV = Encoding.UTF8.GetBytes(AesIV)
    aes.Key = Encoding.UTF8.GetBytes(AesKey)
    aes.Mode = CipherMode.CBC
    aes.Padding = PaddingMode.PKCS7
    
    ' Convert string to byte array
    Dim src As Byte() = Encoding.Unicode.GetBytes(strData)
    
    ' encryption
    Using enc As ICryptoTransform = aes.CreateEncryptor()
    Dim dest As Byte() = enc.TransformFinalBlock(src, 0, src.Length)
    
    'encode encrypted bytes to URL safe string and return
    
    Return HttpServerUtility.UrlTokenEncode(dest)
    
    End Using
    End Function

    As you can see I currently set the aes.KeySize to 128 bit and URLTokenEncode the return data.

    My question is this: how come when I change the aes.KeySize to 256 (and the AesKey to 32 character length) the returned encrypted string is exactly the same as it is for 128 bit? It doesnt look like the aes.KeySize code is doing anything at all. I just need some clarificiation if possible!

    Thanks in advance

    Wednesday, February 19, 2014 7:32 AM

Answers

  • User-1818759697 posted

    Hi,

    how come when I change the aes.KeySize to 256 (and the AesKey to 32 character length) the returned encrypted string is exactly the same as it is for 128 bit? It doesnt look like the aes.KeySize code is doing anything at all.

    For this situation, AesCryptoServiceProvider.KeySize Property Gets or sets the size, in bits, of the secret key. The minimum size of the key is 128 bits, and the maximum size is 256 bits. The of length Keys can be 128, 192, or 256 bits.

    In this scenario, your codes

     Public AesKey As String = "1233123456789012" 

    always the same, even you change the AesCryptoServiceProvider.KeySize to 256, however, the AesKey not change, so the result not change either.

    So you can refer to the following example for more information:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Security.Cryptography;
    using System.IO;
    using System.Text;
    
    namespace Testing
    {
        public partial class AESEncryption : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                string text = "ABCDEFGHABCDEFGHABCDEFGHABCDEFGH";
                Response.Write("128:"+Encrypt128(text) + "<br/><br/><br/>");
    
                Response.Write("128:" + Decrypt128(Encrypt128(text)) + "<br/><br/><br/>");
    
                Response.Write("256:"+Encrypt256(text) + "<br/><br/><br/>");
    
                Response.Write("256:"+Decrypt256(Encrypt256(text)));
            }
    
            private const string AesIV128 = @"!QAZ2WSX#EDC4RFV";
            private const string AesKey128 = @"5TGB&YHN7UJM(IK<";
    
            /// <summary>
            /// AES Encryption
            /// </summary>
            private string Encrypt128(string text)
            {
                // AesCryptoServiceProvider
                AesCryptoServiceProvider aes = new AesCryptoServiceProvider();
                //aes.BlockSize = 128;
                //aes.KeySize=256;
                aes.IV = Encoding.UTF8.GetBytes(AesIV128);
                aes.Key = Encoding.UTF8.GetBytes(AesKey128);
                aes.Mode = CipherMode.CBC;
                aes.Padding = PaddingMode.PKCS7;
    
                // Convert string to byte array
                byte[] src = Encoding.Unicode.GetBytes(text);
    
                // encryption
                using (ICryptoTransform encrypt = aes.CreateEncryptor())
                {
                    byte[] dest = encrypt.TransformFinalBlock(src, 0, src.Length);
    
                    // Convert byte array to Base64 strings
                    return Convert.ToBase64String(dest);
                }
            }
    
            /// <summary>
            /// AES decryption
            /// </summary>
            private string Decrypt128(string text)
            {
                // AesCryptoServiceProvider
                AesCryptoServiceProvider aes = new AesCryptoServiceProvider();
                //aes.BlockSize = 128;
                //aes.KeySize = 256;
                aes.IV = Encoding.UTF8.GetBytes(AesIV128);
                aes.Key = Encoding.UTF8.GetBytes(AesKey128);
                aes.Mode = CipherMode.CBC;
                aes.Padding = PaddingMode.PKCS7;
    
                // Convert Base64 strings to byte array
                byte[] src = System.Convert.FromBase64String(text);
    
                // decryption
                using (ICryptoTransform decrypt = aes.CreateDecryptor())
                {
                    byte[] dest = decrypt.TransformFinalBlock(src, 0, src.Length);
                    return Encoding.Unicode.GetString(dest);
                }
            }
    
            private const string AesIV256 = @"!QAZ2WSX#EDC4RFV";
            private const string AesKey256 = @"5TGB&YHN7UJM(IK<5TGB&YHN7UJM(IK<";
    
    
            private string Encrypt256(string text)
            {
                // AesCryptoServiceProvider
                AesCryptoServiceProvider aes = new AesCryptoServiceProvider();
                aes.BlockSize = 128;
                aes.KeySize = 256;
                aes.IV = Encoding.UTF8.GetBytes(AesIV256);
                aes.Key = Encoding.UTF8.GetBytes(AesKey256);
                aes.Mode = CipherMode.CBC;
                aes.Padding = PaddingMode.PKCS7;
    
                // Convert string to byte array
                byte[] src = Encoding.Unicode.GetBytes(text);
    
                // encryption
                using (ICryptoTransform encrypt = aes.CreateEncryptor())
                {
                    byte[] dest = encrypt.TransformFinalBlock(src, 0, src.Length);
    
                    // Convert byte array to Base64 strings
                    return Convert.ToBase64String(dest);
                }
            }
    
            /// <summary>
            /// AES decryption
            /// </summary>
            private string Decrypt256(string text)
            {
                // AesCryptoServiceProvider
                AesCryptoServiceProvider aes = new AesCryptoServiceProvider();
                aes.BlockSize = 128;
                aes.KeySize = 256;
                aes.IV = Encoding.UTF8.GetBytes(AesIV256);
                aes.Key = Encoding.UTF8.GetBytes(AesKey256);
                aes.Mode = CipherMode.CBC;
                aes.Padding = PaddingMode.PKCS7;
    
                // Convert Base64 strings to byte array
                byte[] src = System.Convert.FromBase64String(text);
    
                // decryption
                using (ICryptoTransform decrypt = aes.CreateDecryptor())
                {
                    byte[] dest = decrypt.TransformFinalBlock(src, 0, src.Length);
                    return Encoding.Unicode.GetString(dest);
                }
            }
        }
    }

    For more information you could refer to:

    http://programmers-en.high-way.info/cs/aes.html

    http://www.dreamincode.net/forums/topic/156922-aes-encryption-and-c%23/

    Regards

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, February 20, 2014 4:30 AM

All replies

  • User-1818759697 posted

    Hi,

    how come when I change the aes.KeySize to 256 (and the AesKey to 32 character length) the returned encrypted string is exactly the same as it is for 128 bit? It doesnt look like the aes.KeySize code is doing anything at all.

    For this situation, AesCryptoServiceProvider.KeySize Property Gets or sets the size, in bits, of the secret key. The minimum size of the key is 128 bits, and the maximum size is 256 bits. The of length Keys can be 128, 192, or 256 bits.

    In this scenario, your codes

     Public AesKey As String = "1233123456789012" 

    always the same, even you change the AesCryptoServiceProvider.KeySize to 256, however, the AesKey not change, so the result not change either.

    So you can refer to the following example for more information:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Security.Cryptography;
    using System.IO;
    using System.Text;
    
    namespace Testing
    {
        public partial class AESEncryption : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                string text = "ABCDEFGHABCDEFGHABCDEFGHABCDEFGH";
                Response.Write("128:"+Encrypt128(text) + "<br/><br/><br/>");
    
                Response.Write("128:" + Decrypt128(Encrypt128(text)) + "<br/><br/><br/>");
    
                Response.Write("256:"+Encrypt256(text) + "<br/><br/><br/>");
    
                Response.Write("256:"+Decrypt256(Encrypt256(text)));
            }
    
            private const string AesIV128 = @"!QAZ2WSX#EDC4RFV";
            private const string AesKey128 = @"5TGB&YHN7UJM(IK<";
    
            /// <summary>
            /// AES Encryption
            /// </summary>
            private string Encrypt128(string text)
            {
                // AesCryptoServiceProvider
                AesCryptoServiceProvider aes = new AesCryptoServiceProvider();
                //aes.BlockSize = 128;
                //aes.KeySize=256;
                aes.IV = Encoding.UTF8.GetBytes(AesIV128);
                aes.Key = Encoding.UTF8.GetBytes(AesKey128);
                aes.Mode = CipherMode.CBC;
                aes.Padding = PaddingMode.PKCS7;
    
                // Convert string to byte array
                byte[] src = Encoding.Unicode.GetBytes(text);
    
                // encryption
                using (ICryptoTransform encrypt = aes.CreateEncryptor())
                {
                    byte[] dest = encrypt.TransformFinalBlock(src, 0, src.Length);
    
                    // Convert byte array to Base64 strings
                    return Convert.ToBase64String(dest);
                }
            }
    
            /// <summary>
            /// AES decryption
            /// </summary>
            private string Decrypt128(string text)
            {
                // AesCryptoServiceProvider
                AesCryptoServiceProvider aes = new AesCryptoServiceProvider();
                //aes.BlockSize = 128;
                //aes.KeySize = 256;
                aes.IV = Encoding.UTF8.GetBytes(AesIV128);
                aes.Key = Encoding.UTF8.GetBytes(AesKey128);
                aes.Mode = CipherMode.CBC;
                aes.Padding = PaddingMode.PKCS7;
    
                // Convert Base64 strings to byte array
                byte[] src = System.Convert.FromBase64String(text);
    
                // decryption
                using (ICryptoTransform decrypt = aes.CreateDecryptor())
                {
                    byte[] dest = decrypt.TransformFinalBlock(src, 0, src.Length);
                    return Encoding.Unicode.GetString(dest);
                }
            }
    
            private const string AesIV256 = @"!QAZ2WSX#EDC4RFV";
            private const string AesKey256 = @"5TGB&YHN7UJM(IK<5TGB&YHN7UJM(IK<";
    
    
            private string Encrypt256(string text)
            {
                // AesCryptoServiceProvider
                AesCryptoServiceProvider aes = new AesCryptoServiceProvider();
                aes.BlockSize = 128;
                aes.KeySize = 256;
                aes.IV = Encoding.UTF8.GetBytes(AesIV256);
                aes.Key = Encoding.UTF8.GetBytes(AesKey256);
                aes.Mode = CipherMode.CBC;
                aes.Padding = PaddingMode.PKCS7;
    
                // Convert string to byte array
                byte[] src = Encoding.Unicode.GetBytes(text);
    
                // encryption
                using (ICryptoTransform encrypt = aes.CreateEncryptor())
                {
                    byte[] dest = encrypt.TransformFinalBlock(src, 0, src.Length);
    
                    // Convert byte array to Base64 strings
                    return Convert.ToBase64String(dest);
                }
            }
    
            /// <summary>
            /// AES decryption
            /// </summary>
            private string Decrypt256(string text)
            {
                // AesCryptoServiceProvider
                AesCryptoServiceProvider aes = new AesCryptoServiceProvider();
                aes.BlockSize = 128;
                aes.KeySize = 256;
                aes.IV = Encoding.UTF8.GetBytes(AesIV256);
                aes.Key = Encoding.UTF8.GetBytes(AesKey256);
                aes.Mode = CipherMode.CBC;
                aes.Padding = PaddingMode.PKCS7;
    
                // Convert Base64 strings to byte array
                byte[] src = System.Convert.FromBase64String(text);
    
                // decryption
                using (ICryptoTransform decrypt = aes.CreateDecryptor())
                {
                    byte[] dest = decrypt.TransformFinalBlock(src, 0, src.Length);
                    return Encoding.Unicode.GetString(dest);
                }
            }
        }
    }

    For more information you could refer to:

    http://programmers-en.high-way.info/cs/aes.html

    http://www.dreamincode.net/forums/topic/156922-aes-encryption-and-c%23/

    Regards

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, February 20, 2014 4:30 AM
  • User1588000814 posted

    Ah OK so upping the key length to 32 (32*8=256) and then adjust the KeySize to 256 is all I need to do.

    Thanks!

    Thursday, February 20, 2014 7:42 AM