【发布时间】:2019-02-08 05:43:06
【问题描述】:
我在我的应用程序中使用 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);
}
【问题讨论】:
-
你试过不同的字符串长度吗?发布一些数字。
-
并发布加密代码。您的加密可能是错误的。
-
您尝试解密您加密的内容吗?如果你做错了,你可能无法解密它们。也发布您的代码。
-
@Richie86 感谢您的关注。我能够解密它并得到确切的文本