После расшифровки файла с помощью алгоритма AES, я получаю это сообщение:
Но когда я нажимаю ОК, файл открывается без потери данных.
Это заставляет пользователя думать, что с файлом что-то не так.
Мой код для шифрования и дешифрования:
/// <summary>
/// AES strong encryption logic used
/// </summary>
/// <param name="encfilepath">file path which need to encrypt</param>
/// <param name="enckey">key used to decrypt</param>
public static void AESEncryptFile(string encfilepath, byte[] enckey)
{
byte[] bytesToBeEncrypted = System.IO.File.ReadAllBytes(System.Security.SecurityElement.Escape(encfilepath));
// Hash the password with SHA256
byte[] passwordBytes = SHA256.Create().ComputeHash(enckey);
//create the new folder to for decryption
var decryptedFilePath = Path.GetDirectoryName(encfilepath) + @"\" + Guid.NewGuid() + @"\" + Path.GetFileName(encfilepath);
if (!Directory.Exists(Path.GetDirectoryName(decryptedFilePath)))
Directory.CreateDirectory(Path.GetDirectoryName(decryptedFilePath));
//decrypt the plain file and store in decryption folder
AES_Encrypt(encfilepath, decryptedFilePath, passwordBytes);
//move the file from decryption folder to actual folder
if (File.Exists(encfilepath))
File.Delete(encfilepath);
File.Move(decryptedFilePath, encfilepath);
Directory.Delete(Path.GetDirectoryName(decryptedFilePath));
}
/// <summary>
/// AES strong encryption logic used
/// </summary>
/// <param name="inputFile">this is plain file path which want to encrypt</param>
/// <param name="outputFile">this is output file where encrypt file need to save</param>
/// <param name="passwordBytes">password byte</param>
private static void AES_Encrypt(string inputFile, string outputFile, byte[] passwordBytes)
{
byte[] saltBytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };
string cryptFile = outputFile;
FileStream fsCrypt = new FileStream(cryptFile, FileMode.Create);
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.Padding = PaddingMode.Zeros;
AES.Mode = CipherMode.CBC;
CryptoStream cs = new CryptoStream(fsCrypt,
AES.CreateEncryptor(),
CryptoStreamMode.Write);
FileStream fsIn = new FileStream(inputFile, FileMode.Open);
int data;
while ((data = fsIn.ReadByte()) != -1)
cs.WriteByte((byte)data);
fsIn.Close();
cs.Close();
fsCrypt.Close();
}
/// <summary>
/// AES strong encryption logic used
/// </summary>
/// <param name="encfilepath">file path which need to encrypt</param>
/// <param name="enckey">key used to decrypt</param>
/// <summary>
/// Decrypt the encrypted file file using AES algorithm
/// </summary>
/// <param name="encryptedFilePath">Selected encrypted file</param>
/// <param name="decryptFilePath">Decrypt file path</param>
/// <param name="userFileName">Selected encrypted file user file name</param>
/// <returns>Value of bool - File decrypted status</returns>
//public bool DecryptFile(string encryptedFilePath, string decryptFilePath, string userFileName)
public static bool AESDecryptFile(string encryptedFilePath, out string decryptFilePath, byte[] encryptionKey)
{
var isDecrypted = false;
try
{
decryptFilePath = Path.GetDirectoryName(encryptedFilePath) + @"\" + Guid.NewGuid() + @"\" + Path.GetFileName(encryptedFilePath);
if (!Directory.Exists(Path.GetDirectoryName(decryptFilePath)))
Directory.CreateDirectory(Path.GetDirectoryName(decryptFilePath));
var passwordBytes = SHA256.Create().ComputeHash(encryptionKey);
AES_Decrypt(encryptedFilePath, decryptFilePath, passwordBytes);
isDecrypted = true;
}
catch (Exception ex)
{
throw ex;
}
return isDecrypted;
}
/// <summary>
/// AES strong encryption logic used
/// </summary>
/// <param name="inputFile">this is cypertext file path which want to decrypt</param>
/// <param name="outputFile">this is output file where decrypted file need to save</param>
/// <param name="passwordBytes">password byte</param>
public static void AES_Decrypt(string inputFile, string outputFile, byte[] passwordBytes)
{
byte[] saltBytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };
FileStream fsCrypt = new FileStream(inputFile, FileMode.Open);
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.Padding = PaddingMode.Zeros;
AES.Mode = CipherMode.CBC;
CryptoStream cs = new CryptoStream(fsCrypt,
AES.CreateDecryptor(),
CryptoStreamMode.Read);
FileStream fsOut = new FileStream(outputFile, FileMode.Create);
int data;
while ((data = cs.ReadByte()) != -1)
fsOut.WriteByte((byte)data);
fsOut.Close();
cs.Close();
fsCrypt.Close();
}
Я повторно использовал этот код:
( AES-шифрование больших файлов )