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.