本文介绍了解密不起作用C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的文件具有加密格式.我想解密我的文件.我已经尝试过这种方法,但这不起作用.
受保护的 无效 Button2_Click1(对象发件人,EventArgs e) { DecryptFile( @" d:\ eula.3082.txt" , @" d:\ sql \ eula.3082.txt",GenerateKey()); } 静态 无效 DecryptFile(字符串 sInputFilename, 字符串 sOutputFilename, 字符串 sKey) { DESCryptoServiceProvider DES = 新 DESCryptoServiceProvider(); // 此提供程序需要64位密钥和IV. // 设置用于DES算法的密钥. DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey); // 设置初始化向量. DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey); // 创建文件流以将加密后的文件读回. FileStream fsread = 新 FileStream(sInputFilename, FileMode.Open, FileAccess.Read); // 从DES实例创建DES解密器. ICryptoTransform desdecrypt = DES.CreateDecryptor(); // 创建用于读取并执行的密码流集 // 对传入字节的DES解密转换. CryptoStream cryptostreamDecr = 新 CryptoStream(fsread, 解密, CryptoStreamMode.Read); // 打印解密文件的内容. StreamWriter fsDecrypted = 新 StreamWriter(sOutputFilename); fsDecrypted.Write(新 StreamReader(cryptostreamDecr).ReadToEnd()); fsDecrypted.Flush(); fsDecrypted.Close(); }
在这一行中,我遇到了错误:数据错误
fsDecrypted.Write(new StreamReader(cryptostreamDecr).ReadToEnd());
解决方案
I have my file woth encrypted format.i want to decrypt my file.i have tried like this but this is not working.
protected void Button2_Click1(object sender, EventArgs e) { DecryptFile(@"d:\eula.3082.txt", @"d:\sql\eula.3082.txt", GenerateKey()); } 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).ReadToEnd()); fsDecrypted.Flush(); fsDecrypted.Close(); }
In this line i am getting error: Bad data
fsDecrypted.Write(new StreamReader(cryptostreamDecr).ReadToEnd());
解决方案
这篇关于解密不起作用C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!