我找到了这段代码并在VS 2017 C#中使用它来加密和解密文件.如果我加密.txt文件,然后解密文件并将解密的输出与原始文件进行比较,则匹配.如果我尝试使用.bin文件,它不匹配,我无法弄清楚原因.我认为它应该适用于任何类型的文件?谁能看出问题是什么?
static void AESEncryptCBC(string plainText, Stream encryptedOutput, byte[] key)
{
if (plainText == null || plainText == string.Empty)
return;
if (key == null | key.Length == 0)
return;
if (encryptedOutput == null)
return;
using (var aes = new AesManaged())
{
aes.Key = key;
var salt = new byte[16];
using (var rngCSP = new RNGCryptoServiceProvider())
{
rngCSP.GetBytes(salt);
}
aes.IV = salt;
using (var encryptor = aes.CreateEncryptor())
{
encryptedOutput.Write(salt, 0, salt.Length);//write the salt to the begining of the stream
using …Run Code Online (Sandbox Code Playgroud)