Rijndael crypt=Rijndael.Create();
public void btn_jiami_Click(object sender,System.EventArgs e)
{
//加密
// ICryptoTransform transform=crypt.CreateEncryptor();
// FileStream fs=new FileStream("H:\\testfile.txt",FileMode.Create);
// CryptoStream cs=new CryptoStream(fs,transform,CryptoStreamMode.Write);
// StreamWriter sw=new StreamWriter(cs);
// sw.Write(this.txt_Text.Text);
// sw.Flush();
// cs.FlushFinalBlock();
// sw.Close();
// this.txt_Text.Text="";
FileStream fs=new FileStream("H:\\test.txt",FileMode.Create);
ICryptoTransform transformEncode=new ToBase64Transform();
CryptoStream csEncode=new CryptoStream(fs,transformEncode,CryptoStreamMode.Write);
ICryptoTransform transformEncrypt=crypt.CreateEncryptor();
CryptoStream csEncrypt=new CryptoStream(csEncode,transformEncrypt,CryptoStreamMode.Write);
StreamWriter w=new StreamWriter(csEncrypt);
w.Write(this.txt_Text.Text);
w.Flush();
csEncrypt.FlushFinalBlock();
w.Close();
this.txt_Text.Text="";
}
public void btn_jiemi_Click(object sender,System.EventArgs e)
{
//解密
// ICryptoTransform transform=crypt.CreateDecryptor();
// FileStream fs=new FileStream("H:\\testfile.txt",FileMode.Open);
// CryptoStream cs=new CryptoStream(fs,transform,CryptoStreamMode.Read);
// StreamReader sr=new StreamReader(cs);
// string text=sr.ReadToEnd();
// sr.Close();
// this.txt_Text.Text=text;
FileStream fs=new FileStream("H:\\test.txt",FileMode.Open);
ICryptoTransform transformDecode=new FromBase64Transform();
CryptoStream csDecode=new CryptoStream(fs,transformDecode,CryptoStreamMode.Read);
ICryptoTransform transformDecrypt=crypt.CreateDecryptor();
CryptoStream csDecrypt=new CryptoStream(csDecode,transformDecrypt,CryptoStreamMode.Read);
StreamReader r=new StreamReader(csDecrypt);
string text=r.ReadToEnd();
this.txt_Text.Text=""+text+"";
}
出现以下问题,是那里出错了,麻烦各位了
PKCS7 填充无效,无法被移除。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。
异常详细信息: System.Security.Cryptography.CryptographicException: PKCS7 填充无效,无法被移除。
源错误:
行 70: CryptoStream csDecrypt=new CryptoStream(csDecode,transformDecrypt,CryptoStreamMode.Read);
行 71: StreamReader r=new StreamReader(csDecrypt);
行 72: string text=r.ReadToEnd();
行 73: this.txt_Text.Text=""+text+"";
行 74: }