System.Security.Cryptography简单应用。
本人对此研究并不多,如有差错,还多请赐教。
using System;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Security.Cryptography;
using System.Globalization;
namespace test
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private int oldWith = 0;
private int oldHight = 0;
private void Form1_Load(object sender, EventArgs e)
{
this.Opacity = 0.9;
oldWith = this.Width;
oldHight = this.Height;
}
private void button1_Click(object sender, EventArgs e)
{
byte[] PlainByte = null;
switch (comboBox1.SelectedIndex)
{
case 0:
PlainByte = Encoding.ASCII.GetBytes(PlainText.Text);
break;
case 1:
PlainByte = Encoding.Unicode.GetBytes(PlainText.Text);
break;
case 2:
PlainByte = Encoding.BigEndianUnicode.GetBytes(PlainText.Text);
break;
case 3:
PlainByte = Encoding.UTF7.GetBytes(PlainText.Text);
break;
case 4:
PlainByte = Encoding.UTF8.GetBytes(PlainText.Text);
break;
case 5:
PlainByte = Encoding.UTF32.GetBytes(PlainText.Text);
break;
default:
MessageBox.Show("Encoding error!");
return;
}
textBox1.Text = BitConverter.ToString(MD5.Create().ComputeHash(PlainByte)).Replace("-", "");
textBox2.Text = BitConverter.ToString(SHA1.Create().ComputeHash(PlainByte)).Replace("-", "");
textBox3.Text = BitConverter.ToString(SHA256.Create().ComputeHash(PlainByte)).Replace("-", "");
textBox4.Text = BitConverter.ToString(SHA384.Create().ComputeHash(PlainByte)).Replace("-", "");
textBox5.Text = BitConverter.ToString(SHA512.Create().ComputeHash(PlainByte)).Replace("-", "");
textBox6.Text = BitConverter.ToString(new DSACryptoServiceProvider().SignData(PlainByte)).Replace("-", "");
RSAPKCS1SignatureFormatter rsasf = new RSAPKCS1SignatureFormatter(new RSACryptoServiceProvider());
//rsasf.SetKey(key);
rsasf.SetHashAlgorithm("SHA1");
textBox7.Text = BitConverter.ToString(rsasf.CreateSignature(SHA1.Create().ComputeHash(PlainByte))).Replace("-", "");
ICryptoTransform Encrypt;
DES des = DES.Create();
des.GenerateKey();
des.GenerateIV();
Encrypt = des.CreateEncryptor();
textBox8.Text = string.Format("Key:{0}, IV:{1}, Cryptograph:{2}",
BitConverter.ToString(des.Key),
BitConverter.ToString(des.IV),
BitConverter.ToString(Encrypt.TransformFinalBlock(PlainByte, 0, PlainByte.Length))).Replace("-", "");
RC2 rc2 = RC2.Create();
rc2.GenerateKey();
rc2.GenerateIV();
Encrypt = rc2.CreateEncryptor();
textBox9.Text = string.Format("Key:{0}, IV:{1}, Cryptograph:{2}",
BitConverter.ToString(rc2.Key),
BitConverter.ToString(rc2.IV),
BitConverter.ToString(Encrypt.TransformFinalBlock(PlainByte, 0, PlainByte.Length))).Replace("-", "");
}
private void Form1_ResizeEnd(object sender, EventArgs e)
{
this.Height = oldHight;
if (this.Width < 466) this.Width = 466;
int v = this.Width - oldWith;
PlainText.Width += v;
button1.Width += v;
foreach (Control i in this.Controls)
if (i is TextBox) i.Width += v;
oldWith = this.Width;
}
}
}