using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Text;
using System.IO;
using System.Security.Cryptography;
privatevoidmenuItem13_Click(objectsender,System.EventArgse)
...{
//对应加密函数DSACrypData(stringstrcrypto)
stringstrcrypto=richTextBox1.Text;
DSACrypData(strcrypto);
}
publicstaticvoidDSACrypData(stringstrcrypto)
...{
//先要将字符串转换为字节数组,这与编码有关。
stringstr="thisisatest.";
byte[]bytes=Encoding.ASCII.GetBytes(str);
//选择签名方式,有RSA和DSA
DSACryptoServiceProviderdsac=newDSACryptoServiceProvider();
byte[]sign=dsac.SignData(bytes);
//sign便是出来的签名结果。
//下面是认证了
DSACryptoServiceProviderdsac2=newDSACryptoServiceProvider();
dsac2.FromXmlString(dsac.ToXmlString(false));
boolver=dsac2.VerifyData(bytes,sign);
if(ver)
...{
MessageBox.Show("通过");
}
else
...{
MessageBox.Show("不能通过");
}
}
privatevoidmenuItem17_Click(objectsender,System.EventArgse)
...{
//对应加密函数DSACrypData(stringstrcrypto)
stringstrData=richTextBox1.Text;
richTextBox2.Text=DESEncodeData(strData);
}
publicstaticstringDESEncodeData(stringdata)
...{
stringKEY_64="VavicApp";
stringIV_64="VavicApp";
byte[]byKey=System.Text.ASCIIEncoding.ASCII.GetBytes(KEY_64);
byte[]byIV=System.Text.ASCIIEncoding.ASCII.GetBytes(IV_64);
DESCryptoServiceProvidercryptoProvider=newDESCryptoServiceProvider();
inti=cryptoProvider.KeySize;
MemoryStreamms=newMemoryStream();
CryptoStreamcst=newCryptoStream(ms,cryptoProvider.CreateEncryptor(byKey,byIV),CryptoStreamMode.Write);
StreamWritersw=newStreamWriter(cst);
sw.Write(data);
sw.Flush();
cst.FlushFinalBlock();
sw.Flush();
returnConvert.ToBase64String(ms.GetBuffer(),0,(int)ms.Length);
}

privatevoidmenuItem21_Click(objectsender,System.EventArgse)
...{
//对应加密函数DSACrypData(stringstrcrypto)
stringstrData=richTextBox1.Text;
richTextBox2.Text=DESDecodeData(strData);
}
publicstaticstringDESDecodeData(stringdata)
...{
stringKEY_64="VavicApp";
stringIV_64="VavicApp";
byte[]byKey=System.Text.ASCIIEncoding.ASCII.GetBytes(KEY_64);
byte[]byIV=System.Text.ASCIIEncoding.ASCII.GetBytes(IV_64);
byte[]byEnc;
try
...{
byEnc=Convert.FromBase64String(data);
}
catch
...{
returnnull;
}
DESCryptoServiceProvidercryptoProvider=newDESCryptoServiceProvider();
MemoryStreamms=newMemoryStream(byEnc);
CryptoStreamcst=newCryptoStream(ms,cryptoProvider.CreateDecryptor(byKey,byIV),CryptoStreamMode.Read);
StreamReadersr=newStreamReader(cst);
returnsr.ReadToEnd();
}
本文介绍如何利用DSA算法实现数字签名验证,并演示了如何使用DES算法进行数据的加密与解密操作。
5550

被折叠的 条评论
为什么被折叠?



