【发布时间】:2019-12-22 17:16:02
【问题描述】:
我正在尝试加密一些密钥及其抛出
The input data is not a complete block。我在 stackoverflow 上尝试了一些答案,但似乎都不起作用!
代码如下:
public static string Encrypt(string value)
{
string EncryptionKey = "THISISSOMEENCRYPTIONKEY";
byte[] clearBytes = Encoding.UTF8.GetBytes(value);
using (Aes encryptor = Aes.Create())
{
encryptor.BlockSize = 128;
encryptor.Mode = CipherMode.ECB;
encryptor.Padding = PaddingMode.None;
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(); //the input data is not a complete block
}
value = Convert.ToBase64String(ms.ToArray());
}
}
return value;
}
附言我在ASP.NET Core 1.0 MVC 上的一些旧项目中有相同的代码并且它工作正常,而这个是 .NET Core 2.2 (Library) API,它给我抛出了那个错误!
【问题讨论】:
-
哪一行报错了?
标签: c# .net encryption .net-core asp.net-core-webapi