【发布时间】:2016-11-03 05:15:07
【问题描述】:
我正在使用下面的 AES 算法代码来加密给定的文本。
string EncryptionKey = "abc";
string originalText = "Original text";
byte[] clearBytes = Encoding.ASCII.GetBytes(originalText);
using (Aes encryptor = Aes.Create())
{
Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(EncryptionKey, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
encryptor.Key = pdb.GetBytes(32);
encryptor.IV = pdb.GetBytes(16);
using (MemoryStream ms = new MemoryStream())
{
using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateEncryptor(), CryptoStreamMode.Write))
{
cs.Write(clearBytes, 0, clearBytes.Length);
cs.Close();
}
string encryptedText = Convert.ToBase64String(ms.ToArray());
}
}
输出:
XBqY9wBUpAa+ly7P2cdZBQ==
如果我增加原始文本的字符,那么加密文本的大小也会增加。
例如:
输入:
原文长度增加
输出:
RU3azV4sslgUWFSoCKxdKHN/qDCEcKXN/tCFa80Bcmu1O418T/CGRZc3mIzx0f2Q
结果字符串不应根据输入文本增加。如何做到这一点?
【问题讨论】:
标签: c# algorithm encryption cryptography aes