文件的加密和解密在解密时导致一个空白文件
本文关键字:解密 文件 空白 一个 加密 | 更新日期: 2023-09-27 18:18:17
下面是我用来测试文件加密和解密的一个简单代码片段。发生的事情是加密工作正常-然而,当我解密时,文件是空白的,我不完全确定为什么。
我也得到了一个"填充无效,不能被删除"的问题,但我可以用Padding.None
解决这个问题,但我不明白为什么解密时文件为空。我用一个简单的文本文档来测试。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.IO;
namespace EncryptionTest
{
class Program
{
static void Main(string[] args)
{
var Mode = "";
bool Loop = true;
while (Loop)
{
Console.WriteLine("Encrypt or Decrypt? (E/D)");
Mode = Console.ReadLine();
if (Mode.ToUpper() == "E" || Mode.ToUpper() == "D")
switch (Mode.ToUpper())
{
case "E":
Mode = "Encrypt";
Loop = false;
continue;
case "D":
Mode = "Decrpt";
Loop = false;
continue;
}
else
Console.WriteLine("Must be E OR D");
}
Console.WriteLine("What directory do you want to encrypt?");
var fileDirectory = Console.ReadLine();
Console.WriteLine("What password do you want to use?");
var passWord = Console.ReadLine();
string[] files = Directory.GetFiles(fileDirectory);
for (int i = 0; i < files.Length; i++)
{
byte[] bytesToBeEncrypted = File.ReadAllBytes(files[i]);
byte[] passwordBytes = Encoding.UTF8.GetBytes(passWord);
// Hash the password with SHA256
passwordBytes = SHA256.Create().ComputeHash(passwordBytes);
byte[] bytesCyrpt = new byte[0];
if (Mode == "Encrypt")
{
bytesCyrpt = AES_Encrypt(bytesCyrpt, passwordBytes);
File.WriteAllBytes(encryptedFileName(files[i]), bytesCyrpt);
File.Delete(files[i]);
}
else
{
bytesCyrpt = AES_Decrypt(bytesCyrpt, passwordBytes);
File.WriteAllBytes(decryptFileName(files[i]), bytesCyrpt);
}
}
}
public static byte[] AES_Encrypt(byte[] bytesToBeEncrypted, byte[] passwordBytes)
{
byte[] encryptedBytes = null;
// Set your salt here, change it to meet your flavor:
// The salt bytes must be at least 8 bytes.
byte[] saltBytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };
using (MemoryStream ms = new MemoryStream())
{
using (RijndaelManaged AES = new RijndaelManaged())
{
AES.KeySize = 256;
AES.BlockSize = 128;
var key = new Rfc2898DeriveBytes(passwordBytes, saltBytes, 1000);
AES.Key = key.GetBytes(AES.KeySize / 8);
AES.IV = key.GetBytes(AES.BlockSize / 8);
AES.Mode = CipherMode.CBC;
using (var cs = new CryptoStream(ms, AES.CreateEncryptor(), CryptoStreamMode.Write))
{
cs.Write(bytesToBeEncrypted, 0, bytesToBeEncrypted.Length);
cs.Close();
}
encryptedBytes = ms.ToArray();
}
}
return encryptedBytes;
}
public static byte[] AES_Decrypt(byte[] bytesToBeDecrypted, byte[] passwordBytes)
{
byte[] decryptedBytes = null;
// Set your salt here, change it to meet your flavor:
// The salt bytes must be at least 8 bytes.
byte[] saltBytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };
using (MemoryStream ms = new MemoryStream())
{
using (RijndaelManaged AES = new RijndaelManaged())
{
AES.KeySize = 256;
AES.BlockSize = 128;
var key = new Rfc2898DeriveBytes(passwordBytes, saltBytes, 1000);
AES.Key = key.GetBytes(AES.KeySize / 8);
AES.IV = key.GetBytes(AES.BlockSize / 8);
AES.Mode = CipherMode.CBC;
using (var cs = new CryptoStream(ms, AES.CreateDecryptor(), CryptoStreamMode.Write))
{
cs.Write(bytesToBeDecrypted, 0, bytesToBeDecrypted.Length);
cs.Close();
}
decryptedBytes = ms.ToArray();
}
}
return decryptedBytes;
}
private static string encryptedFileName(string fileName)
{
string new_file_name = string.Empty;
string extension = fileName.Split('.').Last();
new_file_name = fileName.Replace(extension, "");
new_file_name = new_file_name + "_encrypt." + extension;
return new_file_name;
}
private static string decryptFileName(string fileName)
{
string new_file_name = fileName;
new_file_name = new_file_name.Replace("_encrypt", "");
return new_file_name;
}
}
}
您没有向您的方法传递任何字节。您需要传递bytestosterone beencrypted数组。您正在传递bytesCrypt数组,其中包含0字节
byte[] bytesToBeEncrypted = File.ReadAllBytes(files[i]);
byte[] passwordBytes = Encoding.UTF8.GetBytes(passWord);
// Hash the password with SHA256
passwordBytes = SHA256.Create().ComputeHash(passwordBytes);
byte[] bytesCyrpt = new byte[0];
if (Mode == "Encrypt")
{
bytesCyrpt = AES_Encrypt(bytesToBeEncrypted, passwordBytes);
File.WriteAllBytes(encryptedFileName(files[i]), bytesCyrpt);
File.Delete(files[i]);
}
else
{
bytesCyrpt = AES_Decrypt(bytesToBeEncrypted, passwordBytes);
File.WriteAllBytes(decryptFileName(files[i]), bytesCyrpt);
}