如何使用 Visual C# 加密和解密文件(3)

        static void DecryptFile(string sInputFilename, string sOutputFilename, string sKey)
        {
            DESCryptoServiceProvider DES 
= new DESCryptoServiceProvider();
            
//A 64 bit key and IV is required for this provider.
            
//Set secret key For DES algorithm.
            DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
            
//Set initialization vector.
            DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey);

            
//Create a file stream to read the encrypted file back.
            FileStream fsread = new FileStream(sInputFilename, FileMode.Open, FileAccess.Read);
            
//Create a DES decryptor from the DES instance.
            ICryptoTransform desdecrypt = DES.CreateDecryptor();
            
//Create crypto stream set to read and do a 
            
//DES decryption transform on incoming bytes.
            CryptoStream cryptostreamDecr = new CryptoStream(fsread, desdecrypt, CryptoStreamMode.Read);
            
//Print the contents of the decrypted file.
            StreamWriter fsDecrypted = new StreamWriter(sOutputFilename);
            fsDecrypted.Write(
new StreamReader(cryptostreamDecr,Encoding.GetEncoding("gb2312")).ReadToEnd());
            fsDecrypted.Flush();
            fsDecrypted.Close();
        }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/5c6f9f4ceb4886993825b03d319c21c0.html