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;
private
void
menuItem13_Click(
object
sender,System.EventArgse)
... {
//对应加密函数DSACrypData(stringstrcrypto)
stringstrcrypto=richTextBox1.Text;
DSACrypData(strcrypto);
}
public static void DSACrypData( string strcrypto)
... {
//先要将字符串转换为字节数组,这与编码有关。
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("不能通过");
}
}
private void menuItem17_Click( object sender,System.EventArgse)
... {
//对应加密函数DSACrypData(stringstrcrypto)
stringstrData=richTextBox1.Text;
richTextBox2.Text=DESEncodeData(strData);
}
public static string DESEncodeData( string data)
... {
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);
}
private void menuItem21_Click( object sender,System.EventArgse)
... {
//对应加密函数DSACrypData(stringstrcrypto)
stringstrData=richTextBox1.Text;
richTextBox2.Text=DESDecodeData(strData);
}
public static string DESDecodeData( string data)
... {
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();
}
... {
//对应加密函数DSACrypData(stringstrcrypto)
stringstrcrypto=richTextBox1.Text;
DSACrypData(strcrypto);
}
public static void DSACrypData( string strcrypto)
... {
//先要将字符串转换为字节数组,这与编码有关。
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("不能通过");
}
}
private void menuItem17_Click( object sender,System.EventArgse)
... {
//对应加密函数DSACrypData(stringstrcrypto)
stringstrData=richTextBox1.Text;
richTextBox2.Text=DESEncodeData(strData);
}
public static string DESEncodeData( string data)
... {
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);
}
private void menuItem21_Click( object sender,System.EventArgse)
... {
//对应加密函数DSACrypData(stringstrcrypto)
stringstrData=richTextBox1.Text;
richTextBox2.Text=DESDecodeData(strData);
}
public static string DESDecodeData( string data)
... {
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();
}