using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace ZU14
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//ZU14.DES des = new ZU14.DES();
ZU14.DES des = null;
private void btn_jiami_Click(object sender, EventArgs e)
{
textBox2.Text = des.Encrypt(textBox1.Text);
// MessageBox.Show("加密成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
}
private void btn_jiemi_Click(object sender, EventArgs e)
{
textBox3.Text = des.Decrypt(textBox2.Text);
//MessageBox.Show("解密成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
}
private void btn_wjjiami_Click(object sender, EventArgs e)
{
OpenFileDialog open = new OpenFileDialog();
open.InitialDirectory = @"d:\";
open.Filter = "文本文件(*.txt,*.doc,*.xls)|*.txt;*.doc;*.xls";
if (open.ShowDialog()== DialogResult.OK)
{
des.EncryptFile(open.FileName, open.FileName);
MessageBox.Show("加密成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
}
}
private void btn_wjjiemi_Click(object sender, EventArgs e)
{
OpenFileDialog open = new OpenFileDialog();
open.InitialDirectory = @"d:\";
open.Filter = "文本文件(*.txt,*.doc,*.xls)|*.txt;*.doc;*.xls";
if (open.ShowDialog() == DialogResult.OK)
{
des.DecryptFile(open.FileName);
MessageBox.Show("解密成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
}
}
private void button1_Click(object sender, EventArgs e)
{
zifu.setmisi1 = textBox4.Text.Trim();
zifu.setmisi2 = textBox5.Text.Trim();
des = new ZU14.DES();
}
}
}
上面的代码是窗体的
下面是调用的两个类的
using System;
using System.Collections.Generic;
using System.Text;
namespace ZU14
{
class zifu
{
private static string misi1;
private static string misi2;
public static string getmisi1
{
get
{
return misi1;
}
}
public static string setmisi1
{
set
{
misi1 = value;
}
}
public static string getmisi2
{
get
{
return misi2;
}
}
public static string setmisi2
{
set
{
misi2 = value;
}
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Security;
using System.Security.Cryptography;
using System.Collections;
using System.Data;
using System.Windows.Forms;
namespace ZU14
{
class DES
{
string iv =zifu.getmisi1; //"1234的yza";
string key = zifu.getmisi2;//"123在yzb";
/// <summary>
/// DES加密偏移量,必须是>=8位长的字符串
/// </summary>
public string IV
{
get { return iv; }
set { iv = value; }
}
/// <summary>
/// DES加密的私钥,必须是8位长的字符串
/// </summary>
public string Key
{
get { return key; }
set { key = value; }
}
/// <summary>
/// 对字符串进行DES加密
/// </summary>
/// <param name="sourceString">待加密的字符串</param>
/// <returns>加密后的BASE64编码的字符串</returns>
public string Encrypt(string sourceString)
{
byte[] btKey = Encoding.Default.GetBytes(key);
byte[] btIV = Encoding.Default.GetBytes(iv);
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
using (MemoryStream ms = new MemoryStream())
{
byte[] inData = Encoding.Default.GetBytes(sourceString);
try
{
using (CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(btKey, btIV), CryptoStreamMode.Write))
{
cs.Write(inData, 0, inData.Length);
cs.FlushFinalBlock();
}
return Convert.ToBase64String(ms.ToArray());
}
catch
{
throw;
}
}
}
/// <summary>
/// 对DES加密后的字符串进行解密
/// </summary>
/// <param name="encryptedString">待解密的字符串</param>
/// <returns>解密后的字符串</returns>
public string Decrypt(string encryptedString)
{
byte[] btKey = Encoding.Default.GetBytes(key);
byte[] btIV = Encoding.Default.GetBytes(iv);
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
using (MemoryStream ms = new MemoryStream())
{
byte[] inData = Convert.FromBase64String(encryptedString);
try
{
using (CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(btKey, btIV), CryptoStreamMode.Write))
{
cs.Write(inData, 0, inData.Length);
cs.FlushFinalBlock();
}
return Encoding.Default.GetString(ms.ToArray());
}
catch
{
throw;
}
}
}
/// <summary>
/// 对文件内容进行DES加密
/// </summary>
/// <param name="sourceFile">待加密的文件绝对路径</param>
/// <param name="destFile">加密后的文件保存的绝对路径</param>
public void EncryptFile(string sourceFile, string destFile)
{
if (!File.Exists(sourceFile)) throw new FileNotFoundException("指定的文件路径不存在!", sourceFile);
byte[] btKey = Encoding.Default.GetBytes(key);
byte[] btIV = Encoding.Default.GetBytes(iv);
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
byte[] btFile = File.ReadAllBytes(sourceFile);
using (FileStream fs = new FileStream(destFile, FileMode.Create, FileAccess.Write))
{
try
{
using (CryptoStream cs = new CryptoStream(fs, des.CreateEncryptor(btKey, btIV), CryptoStreamMode.Write))
{
cs.Write(btFile, 0, btFile.Length);
cs.FlushFinalBlock();
}
}
catch
{
// throw;
}
finally
{
fs.Close();
}
}
}
/// <summary>
/// 对文件内容进行DES加密,加密后覆盖掉原来的文件
/// </summary>
/// <param name="sourceFile">待加密的文件的绝对路径</param>
public void EncryptFile(string sourceFile)
{
EncryptFile(sourceFile, sourceFile);
}
/// <summary>
/// 对文件内容进行DES解密
/// </summary>
/// <param name="sourceFile">待解密的文件绝对路径</param>
/// <param name="destFile">解密后的文件保存的绝对路径</param>
public void DecryptFile(string sourceFile, string destFile)
{
if (!File.Exists(sourceFile)) throw new FileNotFoundException("指定的文件路径不存在!", sourceFile);
byte[] btKey = Encoding.Default.GetBytes(key);
byte[] btIV = Encoding.Default.GetBytes(iv);
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
byte[] btFile = File.ReadAllBytes(sourceFile);
using (FileStream fs = new FileStream(destFile, FileMode.Create, FileAccess.Write))
{
try
{
using (CryptoStream cs = new CryptoStream(fs, des.CreateDecryptor(btKey, btIV), CryptoStreamMode.Write))
{
cs.Write(btFile, 0, btFile.Length);
cs.FlushFinalBlock();
}
}
catch
{
// MessageBox.Show(ex.Message);
//throw;
}
finally
{
fs.Close();
}
}
}
/// <summary>
/// 对文件内容进行DES解密,加密后覆盖掉原来的文件
/// </summary>
/// <param name="sourceFile">待解密的文件的绝对路径</param>
public void DecryptFile(string sourceFile)
{
DecryptFile(sourceFile, sourceFile);
}
}
}
有什么看不明白的,再联系我,我的账号就是我的QQ
温馨提示:内容为网友见解,仅供参考