数据加密 附资源包DEMO 凯撒加密 DES对称加密 AES加密
在学数据加密解密的时候了解到了各种加密解密的方式
1凯撒加密
输入加密值 key值, 点击加密 会出现加密后的值
以下是凯撒加密的代码段
string inPut = insert.Text;//定义一个字符串,接收用户的输入
int key = Convert.ToInt32(Kcode.Text) % 26;
char res;
foreach (char c in inPut)//遍历字符串
{
if (c != 32)
{
if (!char.IsUpper(c))
{
if (c + key > 122)
{
int a1 = c + key - 122;
res = Convert.ToChar(96 + a1);
}
else
{
res = Convert.ToChar(c + key);
}
output.AppendText(res.ToString());
}
if (char.IsUpper(c))
{
if (c + key > 90)
{
int a1 = c + key - 90;
res = Convert.ToChar(64 + a1);
}
else
{
res = Convert.ToChar(c + key);
}
output.AppendText(res.ToString());
}
}
}
MessageBox.Show(output.Text);
2DES 对称加密
1选择文件
2选择对应的加密方式
3在输入完成秘钥后将得到指定的密文。
以下是代码片段
string key = textBox8.Text;
string path = textBox1.Text;
string path1 = textBox2.Text;
string content=null;
if (path.Equals(null) || path.Equals(""))
{
MessageBox.Show("文件路径不能为空");
return;
}
try
{
FileStream myfs = new FileStream(path, FileMode.Open);
//创建读取器
StreamReader mySr = new StreamReader(myfs,Encoding.Default);
//读取文件所有内容
content = mySr.ReadToEnd();
DES des = new DES();
string str= des.DesEncrypt(content, key);
MessageBox.Show("加密完成加密前文件内的值为" + content + "加密后的值" + str+"已写入文件");
//关闭读取器
mySr.Close();
//关闭文件流
myfs.Close();
FileStream myfs1 = new FileStream(path1, FileMode.Create);
//创建写入器
StreamWriter mySw = new StreamWriter(myfs1);
//将录入的内容写入文件
mySw.Write(str);
//关闭写入器
mySw.Close();
//关闭文件流
myfs1.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
完整代码如下
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace Jiami
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button6_Click(object sender, EventArgs e)
{
output.Clear();
jiami();
}
public void jiami()
{
string inPut = insert.Text;//定义一个字符串,接收用户的输入
int key = Convert.ToInt32(Kcode.Text) % 26;
char res;
foreach (char c in inPut)//遍历字符串
{
if (c != 32)
{
if (!char.IsUpper(c))
{
if (c + key > 122)
{
int a1 = c + key - 122;
res = Convert.ToChar(96 + a1);
}
else
{
res = Convert.ToChar(c + key);
}
output.AppendText(res.ToString());
}
if (char.IsUpper(c))
{
if (c + key > 90)
{
int a1 = c + key - 90;
res = Convert.ToChar(64 + a1);
}
else
{
res = Convert.ToChar(c + key);
}
output.AppendText(res.ToString());
}
}
}
MessageBox.Show(output.Text);
}
private void Form1_Load(object sender, EventArgs e)
{
textBox1.Text = @"F:\visual2010\text-input.txt";
textBox2.Text = @"F:\visual2010\text-output.txt";
textBox4.Text = @"F:\visual2010\text-output.txt";
textBox3.Text = @"F:\visual2010\text-output2.txt";
}
private void button7_Click(object sender, EventArgs e)
{
string key = textBox8.Text;
string path = textBox1.Text;
string path1 = textBox2.Text;
string content=null;
if (path.Equals(null) || path.Equals(""))
{
MessageBox.Show("文件路径不能为空");
return;
}
try
{
FileStream myfs = new FileStream(path, FileMode.Open);
//创建读取器
StreamReader mySr = new StreamReader(myfs,Encoding.Default);
//读取文件所有内容
content = mySr.ReadToEnd();
DES des = new DES();
string str= des.DesEncrypt(content, key);
MessageBox.Show("加密完成加密前文件内的值为" + content + "加密后的值" + str+"已写入文件");
//关闭读取器
mySr.Close();
//关闭文件流
myfs.Close();
FileStream myfs1 = new FileStream(path1, FileMode.Create);
//创建写入器
StreamWriter mySw = new StreamWriter(myfs1);
//将录入的内容写入文件
mySw.Write(str);
//关闭写入器
mySw.Close();
//关闭文件流
myfs1.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void groupBox4_Enter(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
this.ofdMain.ShowDialog();
textBox1.Text = ofdMain.FileName;
}
private void button2_Click(object sender, EventArgs e)
{
this.ofdMain.ShowDialog();
textBox2.Text = ofdMain.FileName;
}
private void button4_Click(object sender, EventArgs e)
{
this.ofdMain.ShowDialog();
textBox4.Text = ofdMain.FileName;
}
private void button3_Click(object sender, EventArgs e)
{
this.ofdMain.ShowDialog();
textBox3.Text = ofdMain.FileName;
}
private void button8_Click(object sender, EventArgs e)
{
string path1 = textBox3.Text;
string key = textBox8.Text;
string path = textBox4.Text;
string content = null;
if (path.Equals(null) || path.Equals(""))
{
MessageBox.Show("文件路径不能为空");
return;
}
try
{
FileStream myfs = new FileStream(path, FileMode.Open);
//创建读取器
StreamReader mySr = new StreamReader(myfs, Encoding.Default);
//读取文件所有内容
content = mySr.ReadToEnd();
DES des = new DES();
string str = des.DesDecrypt(content, key);
MessageBox.Show("解密完成解密前文件内的值为" + content + "解密后的值为" + str + "已写入文件");
//关闭读取器
mySr.Close();
//关闭文件流
myfs.Close();
FileStream myfs1 = new FileStream(path1, FileMode.Create);
//创建写入器
StreamWriter mySw = new StreamWriter(myfs1);
//将录入的内容写入文件
mySw.Write(str);
//关闭写入器
mySw.Close();
//关闭文件流
myfs1.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void button9_Click(object sender, EventArgs e)
{
string path = textBox1.Text;
string path1 = textBox2.Text;
string content = null;
if (path.Equals(null) || path.Equals(""))
{
MessageBox.Show("文件路径不能为空");
return;
}
try
{
FileStream myfs = new FileStream(path, FileMode.Open);
//创建读取器
StreamReader mySr = new StreamReader(myfs, Encoding.Default);
//读取文件所有内容
content = mySr.ReadToEnd();
Rijndael rijndel = new Rijndael();
rijndel.SymmetricMethod(textBox6.Text);
string str = rijndel.Encrypto(content);
MessageBox.Show("加密成功,加密后的信息为" + str);
mySr.Close();
//关闭文件流
myfs.Close();
FileStream myfs1 = new FileStream(path1, FileMode.Create);
//创建写入器
StreamWriter mySw = new StreamWriter(myfs1);
//将录入的内容写入文件
mySw.Write(str);
//关闭写入器
mySw.Close();
//关闭文件流
myfs1.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void button5_Click_1(object sender, EventArgs e)
{
string path = textBox4.Text;
string path1 = textBox3.Text;
string content = null;
if (path.Equals(null) || path.Equals(""))
{
MessageBox.Show("文件路径不能为空");
return;
}
try
{
FileStream myfs = new FileStream(path, FileMode.Open);
//创建读取器
StreamReader mySr = new StreamReader(myfs, Encoding.Default);
//读取文件所有内容
content = mySr.ReadToEnd();
Rijndael rijndel = new Rijndael();
rijndel.SymmetricMethod(textBox6.Text);
string str = rijndel.Decrypto(content);
MessageBox.Show("解密成功,解密后的信息为" + str);
mySr.Close();
//关闭文件流
myfs.Close();
FileStream myfs1 = new FileStream(path1, FileMode.Create);
//创建写入器
StreamWriter mySw = new StreamWriter(myfs1);
//将录入的内容写入文件
mySw.Write(str);
//关闭写入器
mySw.Close();
//关闭文件流
myfs1.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}
若要下载 点击下面蓝色字体跳转。
加密源码下载