本文介绍了解密方法在c#中无法正常工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当我解密加密文本时,它不会解密文档的前几个单词。
我的加密方法是:
when I decrypt the encrypted text it doesn't decrypt the first few words of the document.
My Encryption method is :
public string EncryptString(string plainText,string key)
{
RijndaelManaged rijndaelCipher = new RijndaelManaged();
rijndaelCipher.Key =CreateKey(key);
MemoryStream memoryStream = new MemoryStream();
ICryptoTransform rijndaelEncryptor = rijndaelCipher.CreateEncryptor();
CryptoStream cryptoStream = new CryptoStream(memoryStream, rijndaelEncryptor, CryptoStreamMode.Write);
byte[] plainBytes = Encoding.ASCII.GetBytes(plainText);
cryptoStream.Write(plainBytes, 0, plainBytes.Length);
cryptoStream.FlushFinalBlock();
byte[] cipherBytes = memoryStream.ToArray();
memoryStream.Close();
cryptoStream.Close();
string cipherText = Convert.ToBase64String(cipherBytes, 0, cipherBytes.Length);
return cipherText;
}
private byte[] CreateKey(string password)
{
var salt = new byte[] { 1, 2, 23, 234, 37, 48, 134, 63, 248, 4 };
const int Iterations = 9872;
var rfc2898DeriveBytes = new Rfc2898DeriveBytes(password, salt, Iterations);
return rfc2898DeriveBytes.GetBytes(32);
}
解密方法是:
Decryption Method is :
public string DecryptString(string cipherText,string key)
{
RijndaelManaged rijndaelCipher = new RijndaelManaged();
rijndaelCipher.Key =CreateKey(key);
MemoryStream memoryStream = new MemoryStream();
ICryptoTransform rijndaelDecryptor = rijndaelCipher.CreateDecryptor();
CryptoStream cryptoStream = new CryptoStream(memoryStream, rijndaelDecryptor, CryptoStreamMode.Write);
string plainText = String.Empty;
try
{
byte[] cipherBytes = Convert.FromBase64String(cipherText);
cryptoStream.Write(cipherBytes, 0, cipherBytes.Length);
cryptoStream.FlushFinalBlock();
byte[] plainBytes = memoryStream.ToArray();
plainText = Encoding.ASCII.GetString(plainBytes, 0, plainBytes.Length);
}
finally
{
memoryStream.Close();
cryptoStream.Close();
}
return plainText;
}
我的关键是:ghb(我提供的)当调用解密方法时)..
方法调用:
My key is : "ghb" (which I provide when decryption method is called)..
Methods call :
textBox1.Text = BCF.EncryptString(textBox1.Text,"ghb");//encryption time
textBox1.Text = BCF.DecryptString(textBox1.Text,"ghb");//decryption time
如何解决这个问题?
问候
Jayanta。
How to solved this method ?
Regards
Jayanta.
推荐答案
这篇关于解密方法在c#中无法正常工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!