internal static void Decrypt(string password, string sourceFile, string destinationFile)
{
byte[] abytKey;
byte[] abytSalt;
SymmetricAlgorithm crpSym;
string strPassword = string.Empty;
string strSaltIVFile = string.Empty;
string salt = @"-1254553935.adDi";
abytKey = GetHashKey(password, salt);
UTF8Encoding encoder = new UTF8Encoding();
abytSalt = encoder.GetBytes(salt);
crpSym = SymmetricAlgorithm.Create("Rijndael");
crpSym.Key = abytKey;
crpSym.IV = abytSalt;
FileStream fsCipherText = new FileStream(sourceFile, FileMode.Open, FileAccess.Read);
CryptoStream csDecrypted = new CryptoStream(fsCipherText, crpSym.CreateDecryptor(), CryptoStreamMode.Read);
StreamReader srReader = new StreamReader(csDecrypted);
string str = srReader.ReadToEnd();
try
{
FileStream fsPlainText = new FileStream(destinationFile, FileMode.Create, FileAccess.Write);
fsPlainText.SetLength(0L);
StreamWriter swWriter = new StreamWriter(fsPlainText);
swWriter.Write(str);
swWriter.Close();
}
catch (CryptographicException expCrypto)
{
throw new CryptographicException();
}
finally
{
csDecrypted.Close();
}
}
private void Encrypt(string password, string sourceFile, string destinationFile)
{
byte[] abytKey;
byte[] abytSalt;
string salt = "-1254553935.adDi";
abytKey = GetHashKey(password, salt);
UTF8Encoding encoder = new UTF8Encoding();
abytSalt = encoder.GetBytes(salt);
SymmetricAlgorithm crpSym = SymmetricAlgorithm.Create("Rijndael");
crpSym.Key = abytKey;
crpSym.IV = abytSalt;
FileStream fsInput = new FileStream(sourceFile, FileMode.Open, FileAccess.Read);
byte[] abytInput = new byte[Convert.ToInt32(fsInput.Length)];
fsInput.Read(abytInput, 0, Convert.ToInt32(fsInput.Length));
fsInput.Close();
FileStream fsCipherText = new FileStream(destinationFile, FileMode.Create, FileAccess.Write);
fsCipherText.SetLength(0);
CryptoStream csEncrypted = new CryptoStream(fsCipherText, crpSym.CreateEncryptor(), CryptoStreamMode.Write);
csEncrypted.Write(abytInput, 0, abytInput.Length);
csEncrypted.FlushFinalBlock();
csEncrypted.Close();
fsCipherText.Close();
}
Enctyption/Decryption
Только вот размер раскриптованного файла почти в два раза больше получается, чем исходного. Не пойму, где ошибка.
Код:
Все, спасибо, проблему решила :)