關于.net 的DES加密解決辦法
EDS加密是一個分組加密演算法,通過64位分組對資料進行加密。設計EDS加密程式
Public static string EncodeDES(string encryptString, string encryptkey)
{
Try
{
Byte[]rgbkey=encoding.UTF8.Getbytes(encryptkey.Substring(0,8));
Byte[]rgbIV=keyIV;
Byte[]inputArray=encoding.UTE8.Getbytes(encryptString);
DESCryptoservicePrvider desCSP= DESCryptoservicePrvider();
MemoryStream mStream=new MemoryStream();
CryptoStream cStream=new CryptoStream(mStream, desCSP.CreateEncryptor(rgbKey, rgbIV),
CryptoStreamMode.Write);
cSteam.Write(inputArray,0,inputArray.Length);
cSteam.FlushFinalBlock()
return Convert. ToBase64String(mStream.ToArray());
}
Catch
{
Return encryptString;
}
}
設計解碼程式
Public static string DecodeDES(string decryptString, string decryptkey)
{
Try
{
Byte[]rgbkey=Encoding.UTF8.Getbytes(Decryptkey.Substring(0,8));
Byte[]rgbIV=keyIV;
Byte[]inputArray=encoding.UTE8.Getbytes(decryptString);
DESCryptoservicePrvider desCSP= new DESCryptoservicePrvider();
MemoryStream mStream=new MemoryStream();
CryptoStream cStream=new CryptoStream(mStream, desCSP. CreateEncryptor(rgbKey, rgbIV),
CryptoStreamMode .Write);
cSteam.Write(inputArray,0, inputArray. Length);
cSteam. FlushFinalBlock()
return Convert. ToBase64String(mStream.ToArray());
}
Catch
{
Return decryptString;
}
}
為btnEncode添加事件,實作DES加密
Private viod btnEncode_Click(objiect sender, EventArgs e)
{
StreamRadear datain=File.OpenTxet(tbBeforeCrypFile.Text);
String strTmp;
ArrayList alldate =new Arraylist();
While((strTmp=datain.ReadLine())!=null)
{
Alldate.Add(strTmp);
}
Foreach(string str in alldate)
{
tbBeforeCrypContent.Text+=str;
tbBeforeCrypContent.Text+=”\r\n”;
try
{
String temp= tbBeforeCrypContent.Text;
tbBeforeCrypContent.Text=EncodeDES(temp,keyDes);
}
Catch(System.Exception ep)
{
MassageBox.Show(ep.Mssage);
}
FileSteam fileSteam=new fileSteam(tbAfterCrypFile.Text,filemode.openorcreate);
Steamwriter steamwriter=new steamwriter(filesteam)
//開始寫入
Steamwrite.write(tbaftercrypcontent.text);
//清空快取
Streamwriter.flush();
//關閉流
Steamwrite.close();
Filesteam.close();
}
為btnDecode添加事件,實作DES解密
Private viod btndecode_click(object sender,eventargse)
{
Try
{
String temp=tbaftercrypcontent.text;
tbaftercrypcontent.text=decodeDES(temp,keydes);
}
Catch(System.Exception ep)
{
Massagebox.show(ep.massage)
}
}
uj5u.com熱心網友回復:
你要實作什么,有相關的物件。轉載請註明出處,本文鏈接:https://www.uj5u.com/net/129535.html
上一篇:WPF免費教程
