1

I am using AES encryption in my application. I used 3 types of encryption AES-128,AES-192, AES-256 keysize. When I encrypt with different keysize(128 or 192 or 256) with the same text, the encrypted string length is same for all keysize(128 and 192 and 256) whereas encrypted characters only differs. Is this correct? Is the length of the encrypted string length always same for every keysize?

  static string GetEncryptedString_Aes(string plainText, byte[] Key, byte[] IV)
    {
        // Check arguments.
        if (plainText == null || plainText.Length <= 0)
            throw new ArgumentNullException("plainText");
        if (Key == null || Key.Length <= 0)
            throw new ArgumentNullException("Key");
        if (IV == null || IV.Length <= 0)
            throw new ArgumentNullException("IV");
        byte[] encrypted;

        // Create an AesCryptoServiceProvider object
        // with the specified key and IV.
        using (AesCryptoServiceProvider aesAlg = new AesCryptoServiceProvider())
        {
            aesAlg.Key = Key;
            aesAlg.IV = IV;

            // Create an encryptor to perform the stream transform.
            ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);

            // Create the streams used for encryption.
            using (MemoryStream msEncrypt = new MemoryStream())
            {
                using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
                {
                    using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
                    {
                        //Write all data to the stream.
                        swEncrypt.Write(plainText);
                    }

                    encrypted = msEncrypt.ToArray();
                }
            }
        }


        // Return the encrypted bytes from the memory stream.
        return Convert.ToBase64String(encrypted);

    }
karthikraja
  • 457
  • 1
  • 5
  • 8

3 Answers3

1

Checkout the link below. The size of your key doesn't change your output length. (Block cipher encryption)

Size of data after AES/CBC and AES/ECB encryption

Richie86
  • 169
  • 1
  • 4
  • Do note that this is specific to AES. You can tell from the min/max blocksize values, they are the same at 128b / 16B – H H Sep 05 '18 at 22:18
0

Depending on the padding, key sizes etc and algo used you would get odd sizes

I always like to validate it so I just fidle around with the type I like to use and the data size I expect the input to have, like this i know what the size for the data needs to be in the database.

Try and play with your data in the link and see what you need.

Walter Verhoeven
  • 3,867
  • 27
  • 36
-2

Are you using SQL Server 2005 or above? If so you could just use VARCHAR(MAX) or NVARCHAR(MAX) for the column type.

If you want to be a bit more precise...

The maximum block size for RijndaelManaged is 256 bits (32 bytes).

Your maximum input size is 20 characters, so even if we assume a worst-case scenario of 4 bytes per character, that'll only amount to 80 bytes, which will then be padded up to a maximum of 96 bytes for the encryption process.

If you use Base64 encoding on the encrypted output that will create 128 characters from the 96 encrypted bytes. If you use hex encoding then that will create 192 characters from the 96 encrypted bytes (plus maybe a couple of extra characters if you're prefixing the hex string with "0x"). In either case a column width of 200 characters should give you more than enough headroom.

(NB: These are just off-the-top-of-my-head calculations. I haven't verified that they're actually correct!)

Aftab Lala
  • 136
  • 6