为什么我只能加密16个字符的文字?
使用:
string plainText = "1234567890123456";
不起作用:
string plainText = "12345678901234561";
不起作用:
string plainText = "123456789012345";
代码:
string plainText = "1234567890123456";
byte[] plainTextBytes = Encoding.UTF8.GetBytes(plainText);
byte[] keyBytes = System.Text.Encoding.UTF8.GetBytes("1234567890123456");
byte[] initVectorBytes = System.Text.Encoding.UTF8.GetBytes("1234567890123456");
RijndaelManaged symmetricKey = new RijndaelManaged();
symmetricKey.Mode = CipherMode.CBC;
symmetricKey.Padding = PaddingMode.Zeros;
ICryptoTransform encryptor = symmetricKey.CreateDecryptor(keyBytes, initVectorBytes);
MemoryStream memoryStream = new MemoryStream();
CryptoStream cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write);
cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length);
cryptoStream.FlushFinalBlock();
byte[] cipherTextBytes = memoryStream.ToArray();
memoryStream.Close();
cryptoStream.Close();
string cipherText = Convert.ToBase64String(cipherTextBytes);
Console.ReadLine();
答案 0 :(得分:3)
我不确定我是否理解这个问题,但看看我认为其意图是以下代码
symmetricKey.CreateDecryptor
应该是
symmetricKey.CreateEncryptor
答案 1 :(得分:1)
可能是因为 AES 是一个块密码,每个块有128位...也许你只需要添加一个填充length % 128 == 0
。
(我不是C#开发人员,但实现并不关心自己添加填充)
只是一个提示:尝试使用256位