it seems that my code adds 6 bytes to the result file after encrypt decrypt is called..
i tries it on a mkv file..
please help
here is my code
class TripleDESCryptoService : IEncryptor, IDecryptor
{
public void Encrypt(string inputFileName, string outputFileName, string key)
{
EncryptFile(inputFileName, outputFileName, key);
}
public void Decrypt(string inputFileName, string outputFileName, string key)
{
DecryptFile(inputFileName, outputFileName, key);
}
static void EncryptFile(string inputFileName, string outputFileName, string sKey)
{
var outFile = new FileStream(outputFileName, FileMode.OpenOrCreate, FileAccess.ReadWrite);
// The chryptographic service provider we're going to use
var cryptoAlgorithm = new TripleDESCryptoServiceProvider();
SetKeys(cryptoAlgorithm, sKey);
// This object links data streams to cryptographic values
var cryptoStream = new CryptoStream(outFile, cryptoAlgorithm.CreateEncryptor(), CryptoStreamMode.Write);
// This stream writer will write the new file
var encryptionStream = new BinaryWriter(cryptoStream);
// This stream reader will read the file to encrypt
var inFile = new FileStream(inputFileName, FileMode.Open, FileAccess.Read);
var readwe = new BinaryReader(inFile);
// Loop through the file to encrypt, line by line
var date = readwe.ReadBytes((int)readwe.BaseStream.Length);
// Write to the encryption stream
encryptionStream.Write(date);
// Wrap things up
inFile.Close();
encryptionStream.Flush();
encryptionStream.Close();
}
private static void SetKeys(SymmetricAlgorithm algorithm, string key)
{
var keyAsBytes = Encoding.ASCII.GetBytes(key);
algorithm.IV = keyAsBytes.Take(algorithm.IV.Length).ToArray();
algorithm.Key = keyAsBytes.Take(algorithm.Key.Length).ToArray();
}
static void DecryptFile(string inputFilename, string outputFilename, string sKey)
{
// The encrypted file
var inFile = File.OpenRead(inputFilename);
// The decrypted file
var outFile = new FileStream(outputFilename, FileMode.OpenOrCreate, FileAccess.ReadWrite);
// Prepare the encryption algorithm and read the key from the key file
var cryptAlgorithm = new TripleDESCryptoServiceProvider();
SetKeys(cryptAlgorithm, sKey);
// The cryptographic stream takes in the encrypted file
var encryptionStream = new CryptoStream(inFile, cryptAlgorithm.CreateDecryptor(), CryptoStreamMode.Read);
// Write the new unecrypted file
var cleanStreamReader = new BinaryReader(encryptionStream);
var cleanStreamWriter = new BinaryWriter(outFile);
cleanStreamWriter.Write(cleanStreamReader.ReadBytes((int)inFile.Length));
cleanStreamWriter.Close();
outFile.Close();
cleanStreamReader.Close();
}
}