关于DES加密解决办法
飞天的大鹅 2019-08-01 03:00:37 关于.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)
}
}