分类: C/C++
2007-08-23 13:12:34
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
textBoxKey.Text = "Hello, World!";
}
private void buttonClearCT_Click(object sender, EventArgs e)
{
richTextBoxClearText.Text = "";
}
private void buttonClearCryptograph_Click(object sender, EventArgs e)
{
richTextBoxCryptograph.Text = "";
}
private void buttonOpenClearText_Click(object sender, EventArgs e)
{
if (openFileDialogClearText.ShowDialog() == DialogResult.OK)
{
richTextBoxClearText.LoadFile(openFileDialogClearText.FileName, RichTextBoxStreamType.PlainText);
}
else
{
MessageBox.Show("Error in file open!");
return;
}
}
private void buttonEncrypt_Click(object sender, EventArgs e)
{
String strDESKey;
String strClearText;
if (richTextBoxClearText.Text.Length == 0)
{
MessageBox.Show("Please input Clear Text!");
return;
}
//Get Key
if (textBoxKey.Text.Length == 0)
{
MessageBox.Show("Use the default key:\"Hello, World!\"");
strDESKey = "Hello, World!";
}
else
{
strDESKey = textBoxKey.Text;
}
//Get Clear Text
strClearText = richTextBoxClearText.Text;
EncryptString EncryptNow = new EncryptString();
String CryptographText = EncryptNow.Encrypt(strDESKey, strClearText);
if (CryptographText.Equals(null))
{
MessageBox.Show("Error in Encrypt!");
return;
}
else
{
richTextBoxCryptograph.Text = CryptographText;
}
}
private void buttonDecrypt_Click(object sender, EventArgs e)
{
String strCryptographText;
String strDESKey;
if (richTextBoxCryptograph.Text.Length == 0)
{
MessageBox.Show("Please input Cryptograph!");
return;
}
if (textBoxKey.Text.Length == 0)
{
MessageBox.Show("Use the default key:\"Hello, World!\"");
strDESKey = "Hello, World!";
}
else
{
strDESKey = textBoxKey.Text;
}
strCryptographText = richTextBoxCryptograph.Text;
DecryptDES DecryptNow = new DecryptDES();
String ClearText = DecryptNow.Decrypt(strDESKey, strCryptographText);
if (ClearText.Equals(string.Empty))
{
MessageBox.Show("Error in Decrypt!");
return;
}
else
{
richTextBoxClearText.Text = ClearText;
}
}
private void buttonOpenCryptograph_Click(object sender, EventArgs e)
{
if (openFileDialogCrytograph.ShowDialog() == DialogResult.OK)
{
richTextBoxCryptograph.LoadFile(openFileDialogCrytograph.FileName, RichTextBoxStreamType.PlainText);
}
else
{
MessageBox.Show("Error in file open!");
return;
}
}
private void buttonSaveClearText_Click(object sender, EventArgs e)
{
if (saveFileDialogClearText.ShowDialog() == DialogResult.OK)
{
richTextBoxClearText.SaveFile(saveFileDialogClearText.FileName, RichTextBoxStreamType.PlainText);
}
else
{
MessageBox.Show("Error in file saving!");
return;
}
}
private void buttonSaveCrypt_Click(object sender, EventArgs e)
{
if (saveFileDialogCryptograph.ShowDialog() == DialogResult.OK)
{
richTextBoxCryptograph.SaveFile(saveFileDialogCryptograph.FileName, RichTextBoxStreamType.PlainText);
}
else
{
MessageBox.Show("Error in file saving!");
return;
}
}
private void buttonQuit_Click(object sender, EventArgs e)
{
Application.Exit();
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;
using System.IO;
namespace WindowsApplication1
{
class EncryptString
{
private static byte[] IV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
public String Encrypt(String Key, String ClearText)
{
byte[] rgbKey = Encoding.UTF8.GetBytes(Key.Substring(0, 8));
byte[] rgbIV = IV;
byte[] clearTextArray = Encoding.UTF8.GetBytes(ClearText);
try
{
DESCryptoServiceProvider dCSP = new DESCryptoServiceProvider();
MemoryStream mStream = new MemoryStream();
CryptoStream cStream = new CryptoStream(mStream, dCSP.CreateEncryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
cStream.Write(clearTextArray, 0, clearTextArray.Length);
cStream.FlushFinalBlock();
return Convert.ToBase64String(mStream.ToArray());
}
catch
{
return string.Empty;
}
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;
using System.IO;
namespace WindowsApplication1
{
class DecryptDES
{
private static byte[] IV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
public String Decrypt(String Key, String DecryptText)
{
try
{
byte[] rgbKey = Encoding.UTF8.GetBytes(Key.Substring(0, 8));
byte[] rgbIV = IV;
byte[] decryptArray = Convert.FromBase64String(DecryptText);
DESCryptoServiceProvider DCSP = new DESCryptoServiceProvider();
MemoryStream mStream = new MemoryStream();
CryptoStream cStream = new CryptoStream(mStream, DCSP.CreateDecryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
cStream.Write(decryptArray, 0, decryptArray.Length);
cStream.FlushFinalBlock();
return Encoding.UTF8.GetString(mStream.ToArray());
}
catch
{
return string.Empty;
}
}
}
}