Шифрование файла - C#
Формулировка задачи:
Всем привет.
Есть класс, который шифрует и дешифрует файл.
Но проблема с кириллицей, есть ли возможность подключить ее?
public class Cryptfile { internal static void Decryptfile(string file, string key, string tempfile) { FileStream fsFileIn = File.OpenRead(file); FileStream fsKeyFile = File.OpenRead(key); FileStream fsFileOut = File.Create(tempfile); File.SetAttributes(tempfile, FileAttributes.Hidden); TripleDESCryptoServiceProvider cryptAlgorithm = new TripleDESCryptoServiceProvider(); BinaryReader brFile = new BinaryReader(fsKeyFile); cryptAlgorithm.Key = brFile.ReadBytes(24); cryptAlgorithm.IV = brFile.ReadBytes(8); CryptoStream csEncrypt = new CryptoStream(fsFileIn, cryptAlgorithm.CreateDecryptor(), CryptoStreamMode.Read); StreamReader srCleanStream = new StreamReader(csEncrypt); StreamWriter swCleanStream = new StreamWriter(fsFileOut); swCleanStream.Write(srCleanStream.ReadToEnd()); swCleanStream.Close(); fsFileOut.Close(); srCleanStream.Close(); fsKeyFile.Close(); fsFileIn.Close(); csEncrypt.Close(); } internal static void Encryptfile(string file,string key,string tempfile) { FileStream fsFileOut = File.Create(tempfile); TripleDESCryptoServiceProvider cryptAlgorithm = new TripleDESCryptoServiceProvider(); CryptoStream csEncrypt = new CryptoStream(fsFileOut, cryptAlgorithm.CreateEncryptor(), CryptoStreamMode.Write); StreamWriter swEncStream = new StreamWriter(csEncrypt); StreamReader srFile = new StreamReader(file); string currLine = srFile.ReadLine(); while (currLine != null) { swEncStream.Write(currLine); currLine = srFile.ReadLine(); } srFile.Close(); swEncStream.Flush(); swEncStream.Close(); FileStream fsFileKey = File.Create(key); BinaryWriter bwFile = new BinaryWriter(fsFileKey); bwFile.Write(cryptAlgorithm.Key); bwFile.Write(cryptAlgorithm.IV); bwFile.Flush(); bwFile.Close(); fsFileOut.Close(); csEncrypt.Close(); fsFileKey.Close(); } }
Решение задачи: «Шифрование файла»
textual
Листинг программы
public class CryptFile { internal static void DecryptFile<T>(string srcFile, string keyFile, string outFile) where T : SymmetricAlgorithm, new() { using (T cryptAlgorithm = new T()) { using (FileStream fsKeyFile = File.OpenRead(keyFile)) using (BinaryReader brFile = new BinaryReader(fsKeyFile)) { cryptAlgorithm.Key = brFile.ReadBytes(cryptAlgorithm.KeySize >> 3); cryptAlgorithm.IV = brFile.ReadBytes(cryptAlgorithm.BlockSize >> 3); } using (FileStream fsFileOut = File.Create(outFile)) using (FileStream fsFileIn = File.OpenRead(srcFile)) using (ICryptoTransform crypto = cryptAlgorithm.CreateDecryptor()) using (CryptoStream csEncrypt = new CryptoStream(fsFileIn, crypto, CryptoStreamMode.Read)) csEncrypt.CopyTo(fsFileOut); } } internal static void EncryptFile<T>(string srcFile, string keyFile, string outFile) where T : SymmetricAlgorithm, new() { using (T cryptAlgorithm = new T()) { using (FileStream fsKeyFile = File.Create(keyFile)) using (BinaryWriter bwFile = new BinaryWriter(fsKeyFile)) { bwFile.Write(cryptAlgorithm.Key); bwFile.Write(cryptAlgorithm.IV); } using (FileStream fsFileOut = File.Create(outFile)) using (FileStream fsFileIn = File.OpenRead(srcFile)) using (ICryptoTransform crypto = cryptAlgorithm.CreateEncryptor()) using (CryptoStream csEncrypt = new CryptoStream(fsFileOut, crypto, CryptoStreamMode.Write)) fsFileIn.CopyTo(csEncrypt); } } }
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д