Шифрование файла - C#

Узнай цену своей работы

Формулировка задачи:

Всем привет. Есть класс, который шифрует и дешифрует файл. Но проблема с кириллицей, есть ли возможность подключить ее?
Листинг программы
  1. public class Cryptfile
  2. {
  3. internal static void Decryptfile(string file, string key, string tempfile)
  4. {
  5. FileStream fsFileIn = File.OpenRead(file);
  6. FileStream fsKeyFile = File.OpenRead(key);
  7. FileStream fsFileOut = File.Create(tempfile);
  8. File.SetAttributes(tempfile, FileAttributes.Hidden);
  9. TripleDESCryptoServiceProvider cryptAlgorithm = new TripleDESCryptoServiceProvider();
  10. BinaryReader brFile = new BinaryReader(fsKeyFile);
  11. cryptAlgorithm.Key = brFile.ReadBytes(24);
  12. cryptAlgorithm.IV = brFile.ReadBytes(8);
  13. CryptoStream csEncrypt = new CryptoStream(fsFileIn, cryptAlgorithm.CreateDecryptor(), CryptoStreamMode.Read);
  14. StreamReader srCleanStream = new StreamReader(csEncrypt);
  15. StreamWriter swCleanStream = new StreamWriter(fsFileOut);
  16. swCleanStream.Write(srCleanStream.ReadToEnd());
  17. swCleanStream.Close();
  18. fsFileOut.Close();
  19. srCleanStream.Close();
  20. fsKeyFile.Close();
  21. fsFileIn.Close();
  22. csEncrypt.Close();
  23. }
  24. internal static void Encryptfile(string file,string key,string tempfile)
  25. {
  26. FileStream fsFileOut = File.Create(tempfile);
  27. TripleDESCryptoServiceProvider cryptAlgorithm = new TripleDESCryptoServiceProvider();
  28. CryptoStream csEncrypt = new CryptoStream(fsFileOut, cryptAlgorithm.CreateEncryptor(), CryptoStreamMode.Write);
  29. StreamWriter swEncStream = new StreamWriter(csEncrypt);
  30. StreamReader srFile = new StreamReader(file);
  31. string currLine = srFile.ReadLine();
  32. while (currLine != null)
  33. {
  34. swEncStream.Write(currLine);
  35. currLine = srFile.ReadLine();
  36. }
  37. srFile.Close();
  38. swEncStream.Flush();
  39. swEncStream.Close();
  40. FileStream fsFileKey = File.Create(key);
  41. BinaryWriter bwFile = new BinaryWriter(fsFileKey);
  42. bwFile.Write(cryptAlgorithm.Key);
  43. bwFile.Write(cryptAlgorithm.IV);
  44. bwFile.Flush();
  45. bwFile.Close();
  46. fsFileOut.Close();
  47. csEncrypt.Close();
  48. fsFileKey.Close();
  49. }
  50. }

Решение задачи: «Шифрование файла»

textual
Листинг программы
  1.     public class CryptFile
  2.     {
  3.         internal static void DecryptFile<T>(string srcFile, string keyFile, string outFile) where T : SymmetricAlgorithm, new()
  4.         {
  5.             using (T cryptAlgorithm = new T())
  6.             {
  7.                 using (FileStream fsKeyFile = File.OpenRead(keyFile))
  8.                 using (BinaryReader brFile = new BinaryReader(fsKeyFile))
  9.                 {
  10.                     cryptAlgorithm.Key = brFile.ReadBytes(cryptAlgorithm.KeySize >> 3);
  11.                     cryptAlgorithm.IV = brFile.ReadBytes(cryptAlgorithm.BlockSize >> 3);
  12.                 }
  13.  
  14.                 using (FileStream fsFileOut = File.Create(outFile))
  15.                 using (FileStream fsFileIn = File.OpenRead(srcFile))
  16.                 using (ICryptoTransform crypto = cryptAlgorithm.CreateDecryptor())
  17.                 using (CryptoStream csEncrypt = new CryptoStream(fsFileIn, crypto, CryptoStreamMode.Read))
  18.                     csEncrypt.CopyTo(fsFileOut);
  19.             }
  20.         }
  21.  
  22.         internal static void EncryptFile<T>(string srcFile, string keyFile, string outFile) where T : SymmetricAlgorithm, new()
  23.         {
  24.             using (T cryptAlgorithm = new T())
  25.             {
  26.                 using (FileStream fsKeyFile = File.Create(keyFile))
  27.                 using (BinaryWriter bwFile = new BinaryWriter(fsKeyFile))
  28.                 {
  29.                     bwFile.Write(cryptAlgorithm.Key);
  30.                     bwFile.Write(cryptAlgorithm.IV);
  31.                 }
  32.  
  33.                 using (FileStream fsFileOut = File.Create(outFile))
  34.                 using (FileStream fsFileIn = File.OpenRead(srcFile))
  35.                 using (ICryptoTransform crypto = cryptAlgorithm.CreateEncryptor())
  36.                 using (CryptoStream csEncrypt = new CryptoStream(fsFileOut, crypto, CryptoStreamMode.Write))
  37.                     fsFileIn.CopyTo(csEncrypt);
  38.             }
  39.         }
  40.     }

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

14   голосов , оценка 4 из 5

Нужна аналогичная работа?

Оформи быстрый заказ и узнай стоимость

Бесплатно
Оформите заказ и авторы начнут откликаться уже через 10 минут
Похожие ответы