I made a program in C # that should make a copy of a folder with files of different types, and simultaneously encrypt them. Used to encrypt the file class DESCryptor. Here is the code

private void cryptFile(string key,string load,string save) { FileStream fsInput = new FileStream(load, FileMode.Open, FileAccess.Read); FileStream fsEncrypted = new FileStream(save, FileMode.OpenOrCreate, FileAccess.Write); DESCryptoServiceProvider DES = new DESCryptoServiceProvider(); DES.Key = ASCIIEncoding.ASCII.GetBytes(key); DES.IV = ASCIIEncoding.ASCII.GetBytes(key); ICryptoTransform desencrypt = DES.CreateEncryptor(); CryptoStream cryptostream = new CryptoStream(fsEncrypted, desencrypt, CryptoStreamMode.Write); byte[] bytearrayinput = new byte[fsInput.Length]; fsInput.Read(bytearrayinput, 0, bytearrayinput.Length); cryptostream.Write(bytearrayinput, 0, bytearrayinput.Length); cryptostream.Close(); fsInput.Close(); fsEncrypted.Close(); } private void decryptFile(string key,string load,string save) { DESCryptoServiceProvider DES = new DESCryptoServiceProvider(); DES.Key = ASCIIEncoding.ASCII.GetBytes(key); DES.IV = ASCIIEncoding.ASCII.GetBytes(key); DES.Padding = PaddingMode.None; FileStream fsread = new FileStream(load, FileMode.Open, FileAccess.Read); ICryptoTransform desdecrypt = DES.CreateDecryptor(); CryptoStream cryptostreamDecr = new CryptoStream(fsread, desdecrypt, CryptoStreamMode.Read); StreamWriter fsDecrypted = new StreamWriter(save); fsDecrypted.Write(new StreamReader(cryptostreamDecr).ReadToEnd()); fsDecrypted.Flush(); fsDecrypted.Close(); } 

There was a problem, the program normally works only with .txt files. And when working with files of another extension, for example .xml, .java, and so on. their contents do not completely coincide with the original, sometimes the Baddata error pops up. In general, they can not decipher.

How to make it work with different file types, or is there an alternative code that will allow to cope with this problem.

    1 answer 1

    For some reason, you are converting a file to a line before writing to disk (run it through StreamWriter / StreamWriter / ReadToEnd - this is all working with data as with strings). Exe is a little like a line, so the data is a little spoiled in the process. Instead, write directly to the file. In general, it is less to work with buffers directly:

     private void cryptFile(string key, string load, string save) { using (FileStream inputStream = File.OpenRead(load)) { using (var outputStream = File.OpenWrite(save)) { DESCryptoServiceProvider DES = new DESCryptoServiceProvider(); DES.Key = ASCIIEncoding.ASCII.GetBytes(key); DES.IV = ASCIIEncoding.ASCII.GetBytes(key); ICryptoTransform desencrypt = DES.CreateEncryptor(); using (CryptoStream cryptostream = new CryptoStream(outputStream, desencrypt, CryptoStreamMode.Write)) { inputStream.CopyTo(cryptostream); } } } } private void decryptFile(string key, string load, string save) { DESCryptoServiceProvider DES = new DESCryptoServiceProvider(); DES.Key = ASCIIEncoding.ASCII.GetBytes(key); DES.IV = ASCIIEncoding.ASCII.GetBytes(key); DES.Padding = PaddingMode.None; using (FileStream inputStream = File.OpenRead(load)) { ICryptoTransform desdecrypt = DES.CreateDecryptor(); CryptoStream cryptostreamDecr = new CryptoStream(inputStream, desdecrypt, CryptoStreamMode.Read); using (var outputStream = File.OpenWrite(save)) { cryptostreamDecr.CopyTo(outputStream); } } } 

    By the way, all cryptographic algorithms work with data as with sets of blocks, so be prepared for the fact that the output file will be slightly longer. To get exactly the same data - you need somewhere (for example, at the beginning of an encrypted file) to save the length, and cut the result when decrypting it. Although there may be more beautiful ways to solve this problem.