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.EventArgs e)
... {
//对应加密函数DSACrypData(string strcrypto)
string strcrypto=richTextBox1.Text;
DSACrypData(strcrypto);
}
public static void DSACrypData( string strcrypto)
... {
//先要将字符串转换为字节数组,这与编码有关。
string str = "this is a test.";
byte[] bytes = Encoding.ASCII.GetBytes(str);
//选择签名方式,有RSA和DSA
DSACryptoServiceProvider dsac = new DSACryptoServiceProvider();
byte[] sign = dsac.SignData(bytes);
//sign便是出来的签名结果。
//下面是认证了
DSACryptoServiceProvider dsac2 = new DSACryptoServiceProvider();
dsac2.FromXmlString(dsac.ToXmlString(false));
bool ver = dsac2.VerifyData(bytes, sign);
if (ver)
...{
MessageBox.Show("通过");
}
else
...{
MessageBox.Show("不能通过");
}
}
private void menuItem17_Click( object sender, System.EventArgs e)
... {
//对应加密函数DSACrypData(string strcrypto)
string strData=richTextBox1.Text;
richTextBox2.Text=DESEncodeData(strData);
}
public static string DESEncodeData( string data)
... {
string KEY_64 = "VavicApp";
string IV_64 = "VavicApp";
byte[] byKey = System.Text.ASCIIEncoding.ASCII.GetBytes(KEY_64);
byte[] byIV = System.Text.ASCIIEncoding.ASCII.GetBytes(IV_64);
DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
int i = cryptoProvider.KeySize;
MemoryStream ms = new MemoryStream();
CryptoStream cst = new CryptoStream(ms,cryptoProvider.CreateEncryptor(byKey,byIV),CryptoStreamMode.Write);
StreamWriter sw = new StreamWriter(cst);
sw.Write(data);
sw.Flush();
cst.FlushFinalBlock();
sw.Flush();
return Convert.ToBase64String(ms.GetBuffer(),0,(int)ms.Length);
}
private void menuItem21_Click( object sender, System.EventArgs e)
... {
//对应加密函数DSACrypData(string strcrypto)
string strData=richTextBox1.Text;
richTextBox2.Text=DESDecodeData(strData);
}
public static string DESDecodeData( string data)
... {
string KEY_64 = "VavicApp";
string IV_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
...{
return null;
}
DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
MemoryStream ms = new MemoryStream(byEnc);
CryptoStream cst = new CryptoStream(ms,cryptoProvider.CreateDecryptor(byKey,byIV),CryptoStreamMode.Read);
StreamReader sr = new StreamReader(cst);
return sr.ReadToEnd();
}
... {
//对应加密函数DSACrypData(string strcrypto)
string strcrypto=richTextBox1.Text;
DSACrypData(strcrypto);
}
public static void DSACrypData( string strcrypto)
... {
//先要将字符串转换为字节数组,这与编码有关。
string str = "this is a test.";
byte[] bytes = Encoding.ASCII.GetBytes(str);
//选择签名方式,有RSA和DSA
DSACryptoServiceProvider dsac = new DSACryptoServiceProvider();
byte[] sign = dsac.SignData(bytes);
//sign便是出来的签名结果。
//下面是认证了
DSACryptoServiceProvider dsac2 = new DSACryptoServiceProvider();
dsac2.FromXmlString(dsac.ToXmlString(false));
bool ver = dsac2.VerifyData(bytes, sign);
if (ver)
...{
MessageBox.Show("通过");
}
else
...{
MessageBox.Show("不能通过");
}
}
private void menuItem17_Click( object sender, System.EventArgs e)
... {
//对应加密函数DSACrypData(string strcrypto)
string strData=richTextBox1.Text;
richTextBox2.Text=DESEncodeData(strData);
}
public static string DESEncodeData( string data)
... {
string KEY_64 = "VavicApp";
string IV_64 = "VavicApp";
byte[] byKey = System.Text.ASCIIEncoding.ASCII.GetBytes(KEY_64);
byte[] byIV = System.Text.ASCIIEncoding.ASCII.GetBytes(IV_64);
DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
int i = cryptoProvider.KeySize;
MemoryStream ms = new MemoryStream();
CryptoStream cst = new CryptoStream(ms,cryptoProvider.CreateEncryptor(byKey,byIV),CryptoStreamMode.Write);
StreamWriter sw = new StreamWriter(cst);
sw.Write(data);
sw.Flush();
cst.FlushFinalBlock();
sw.Flush();
return Convert.ToBase64String(ms.GetBuffer(),0,(int)ms.Length);
}
private void menuItem21_Click( object sender, System.EventArgs e)
... {
//对应加密函数DSACrypData(string strcrypto)
string strData=richTextBox1.Text;
richTextBox2.Text=DESDecodeData(strData);
}
public static string DESDecodeData( string data)
... {
string KEY_64 = "VavicApp";
string IV_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
...{
return null;
}
DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
MemoryStream ms = new MemoryStream(byEnc);
CryptoStream cst = new CryptoStream(ms,cryptoProvider.CreateDecryptor(byKey,byIV),CryptoStreamMode.Read);
StreamReader sr = new StreamReader(cst);
return sr.ReadToEnd();
}