我在我的应用程序中使用AES加密。我使用了3种类型的加密AES-128,AES-192,AES-256密钥。当我使用相同的文本加密不同的密钥大小(128或192或256)时,加密的字符串长度对于所有密钥大小(128和192和256)是相同的,而加密的字符只是不同。它是否正确?每个密钥大小的加密字符串长度是否始终相同?
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);
}
查看下面的链接。键的大小不会改变输出长度。 (分组密码加密)
您使用的是SQL Server 2005还是以上版本?如果是这样,您可以使用VARCHAR(MAX)或NVARCHAR(MAX)作为列类型。
如果你想要更精确一点......
RijndaelManaged的最大块大小为256位(32字节)。
您的最大输入大小为20个字符,因此即使我们假设每个字符有4个字节的最坏情况,也只能达到80个字节,然后将加密过程最多填充到96个字节。
如果在加密输出上使用Base64编码,将从96个加密字节创建128个字符。如果您使用十六进制编码,那么将从96个加密字节创建192个字符(如果您在十六进制字符串前面添加“0x”,则可能会添加几个额外字符)。在任何一种情况下,200字符的列宽应该给你足够的余量。
(注意:这些只是我头脑中的计算。我还没有证实它们确实是正确的!)