C# &引用;指定的密码模式对此算法无效;在.NET内核中,但在.NET标准中工作
我正在尝试解密aes加密文件。我能够在.NET标准中成功地解密该文件。但当我在.NETCore中使用相同的代码时,我得到了以下异常C# &引用;指定的密码模式对此算法无效;在.NET内核中,但在.NET标准中工作,c#,encryption,.net-core,aes,.net-standard,C#,Encryption,.net Core,Aes,.net Standard,我正在尝试解密aes加密文件。我能够在.NET标准中成功地解密该文件。但当我在.NETCore中使用相同的代码时,我得到了以下异常 System.Security.Cryptography.CryptographicException: 'Specified cipher mode is not valid for this algorithm.' 这是我用来解密的代码 public void FileDecrypt(string inputFile, string outputFile, s
System.Security.Cryptography.CryptographicException: 'Specified cipher mode is not valid for this algorithm.'
这是我用来解密的代码
public void FileDecrypt(string inputFile, string outputFile, string password)
{
byte[] passwordBytes = System.Text.Encoding.UTF8.GetBytes(password);
byte[] salt = new byte[32];
FileStream fsCrypt = new FileStream(inputFile, FileMode.Open);
fsCrypt.Read(salt, 0, salt.Length);
RijndaelManaged AES = new RijndaelManaged();
AES.KeySize = 256;
AES.BlockSize = 128;
var key = new Rfc2898DeriveBytes(passwordBytes, salt, 50000);
AES.Key = key.GetBytes(AES.KeySize / 8);
AES.IV = key.GetBytes(AES.BlockSize / 8);
AES.Padding = PaddingMode.PKCS7;
AES.Mode = CipherMode.CFB;
using (CryptoStream cs = new CryptoStream(fsCrypt, AES.CreateDecryptor(), CryptoStreamMode.Read))
{
using (FileStream fsOut = new FileStream(outputFile, FileMode.Create))
{
int read;
byte[] buffer = new byte[1048576];
while ((read = cs.Read(buffer, 0, buffer.Length)) > 0)
{
fsOut.Write(buffer, 0, read);
}
}
}
}
若我在.NET内核中删除密码模式并解密,我将得到一个无效的异常
System.Security.Cryptography.CryptographicException: 'Padding is invalid and cannot be removed.'
我更改了不同的填充模式并进行了尝试,但输出文件无效(未正确递减)
为什么我在.Net内核中得到密码模式无效异常,而不是在.Net标准中?
我应该在.NET Core中做什么更改才能正确解密
我正在使用
.NET Core SDK (reflecting any global.json):
Version: 2.1.503
目前,.NET Core只支持三种模式(CBC=1、CTS=5、ECB=2)
看起来它目前没有在.NET Core中实现,但应该在.NET Core 3中实现。