解密不起作用C# [英] decryption not working c#
本文介绍了解密不起作用C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的文件具有加密格式.我想解密我的文件.我已经尝试过这种方法,但这不起作用.
受保护的 无效 Button2_Click1(对象发件人,EventArgs e)
{
DecryptFile( @" , 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());
解决方案
请确保加密和解密的密钥相同.
我认为您的问题可能是关键:
GenerateKey()
如果生成随机密钥,则将获得异常,因为需要使用与加密相同的密钥来解密文件.
除非您创建的应用程序应该使用字典或强力攻击来破解密钥,但是即使算法的密钥很长,算法也不是最强的算法,但这将需要一段时间.
我们还可以看到crypto命令,您是否已加密文件.确保您使用的算法和加密算法完全相同.
这是一篇精彩的文章,可帮助您开始进行加密和解密.
使用C#简单加密和解密数据 [ ^ ]
以下是一些可能对您有帮助的链接.
使用TripleDESCryptoServiceProvider类在.NET中进行加密/解密的功能
尝试一下
Encryption-Decryption-asp .net
这也是
希望对您有所帮助.
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());
解决方案
Make sure the key is the same for encryption and decryption.
I think your problem may be the key :
GenerateKey()
If you are generating a random key you will get an exception becuase a file needs to be decrtypted with the same key it was encrypted.
unless you are creating an application that is supposed to hack the key using a dictionary or brite force attack , but even though the algorithm isnt the strongest if they key is long that will take a while.
Also can we see the encrypt command , did you encrypt the file . Make sure you are using the exact same algorithm and encoding that encrypted it.
Heres a brilliant article to get you started on encryption and decryption.
Simple encrypting and decrypting data in C#[^]
Hi,
Here are a few links which might help you.
Encryption/Decryption Function in .NET using the TripleDESCryptoServiceProvider Class
Try this
Encryption-Decryption-in-asp.net
And this too
Hope this helps..
这篇关于解密不起作用C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文