C#-对称加密的一个例子

加密

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Security.Cryptography;
using System.IO;

namespace 对称加密应用
{
///
/// Form1 的摘要说明。
///
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
///
/// 必需的设计器变量。

private System.Windows.Forms.TextBox textBox3;
private System.Windows.Forms.OpenFileDialog openfile;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.SaveFileDialog savefile;
private System.Windows.Forms.Button button4;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.CheckBox checkBox1;

//创建实例
RijndaelManaged rij = new RijndaelManaged();//
//全局变量,标记文件的扩展名
private static string ext=null;
//标记加密成功与否
private static bool enresult = false;
//标记解密成功与否的标记
private static bool deresult = false;

///
private System.ComponentModel.Container components = null;

public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();

//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}

///
/// 清理所有正在使用的资源。
///
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows 窗体设计器生成的代码
///
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
///
private void InitializeComponent()
{
this.textBox1 = new System.Windows.Forms.TextBox();
this.textBox2 = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.textBox3 = new System.Windows.Forms.TextBox();
this.openfile = new System.Windows.Forms.OpenFileDialog();
this.button3 = new System.Windows.Forms.Button();
this.savefile = new System.Windows.Forms.SaveFileDialog();
this.button4 = new System.Windows.Forms.Button();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.checkBox1 = new System.Windows.Forms.CheckBox();
this.SuspendLayout();
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(88, 96);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(168, 21);
this.textBox1.TabIndex = 0;
this.textBox1.Text = "";
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(88, 136);
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(168, 21);
this.textBox2.TabIndex = 1;
this.textBox2.Text = "";
//
// button1
//
this.button1.Location = new System.Drawing.Point(88, 200);
this.button1.Name = "button1";
this.button1.TabIndex = 2;
this.button1.Text = "加密";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(192, 200);
this.button2.Name = "button2";
this.button2.TabIndex = 3;
this.button2.Text = "解密";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// textBox3
//
this.textBox3.HideSelection = false;
this.textBox3.Location = new System.Drawing.Point(88, 48);
this.textBox3.MaxLength = 17;
this.textBox3.Name = "textBox3";
this.textBox3.PasswordChar = '$';
this.textBox3.TabIndex = 4;
this.textBox3.Text = "";
this.textBox3.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
//
// button3
//
this.button3.Location = new System.Drawing.Point(280, 96);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(40, 23);
this.button3.TabIndex = 5;
this.button3.Text = "Open";
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// button4
//
this.button4.Location = new System.Drawing.Point(280, 136);
this.button4.Name = "button4";
this.button4.Size = new System.Drawing.Size(40, 23);
this.button4.TabIndex = 6;
this.button4.Text = "Save";
this.button4.Click += new System.EventHandler(this.button4_Click);
//
// label1
//
this.label1.Location = new System.Drawing.Point(88, 24);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(136, 23);
this.label1.TabIndex = 7;
this.label1.Text = "请输入你的密码:";
//
// label2
//
this.label2.Location = new System.Drawing.Point(216, 48);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(184, 23);
this.label2.TabIndex = 8;
this.label2.Text = "(密码大小8至16个字符)";
//
// checkBox1
//
this.checkBox1.Location = new System.Drawing.Point(88, 168);
this.checkBox1.Name = "checkBox1";
this.checkBox1.Size = new System.Drawing.Size(216, 24);
this.checkBox1.TabIndex = 9;
this.checkBox1.Text = "删除源文件";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(456, 278);
this.Controls.Add(this.checkBox1);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Controls.Add(this.button4);
this.Controls.Add(this.button3);
this.Controls.Add(this.textBox3);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Controls.Add(this.textBox2);
this.Controls.Add(this.textBox1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);

}
#endregion

///
/// 应用程序的主入口点。
///
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

//加密文件的按钮
private void button1_Click(object sender, System.EventArgs e)
{
if(textBox1.Text != null || textBox2.Text != null)
encryption(textBox3.Text,textBox1.Text,textBox2.Text);
if(checkBox1.Checked==true && enresult==true)
DeleteFile(textBox1.Text);
}
//用于加密的函数
public void encryption(string textBox,string readfile,string writefile)
{
try
{
if(textBox.Length >=8 && textBox.Length<=16 )//判断密码的字符的大小
{

byte [] key = System.Text.Encoding.Default.GetBytes( textBox );
byte [] iv = rij.IV;
Rijndael crypt = Rijndael.Create();
ICryptoTransform transform = crypt.CreateEncryptor(key ,iv);
//写进文件
FileStream fswrite = new FileStream(writefile,FileMode.Create);
CryptoStream cs = new CryptoStream( fswrite ,transform ,CryptoStreamMode.Write );
//打开文件
FileStream fsread = new FileStream(readfile,FileMode.Open);
int length;
while((length = fsread.ReadByte() )!= -1)
cs.WriteByte((byte)length);

fsread.Close();
cs.Close();
fswrite.Close();
enresult = true ;//成功加密
MessageBox.Show("已经成功完成加密任务!");
}
else
{
      MessageBox.Show("密码的最小长度为8个字符,最大长度为16个字符!");
return ;
}
}
catch (Exception e)
{
MessageBox.Show(e.ToString());
}
}
//用于解密的函数
public void decryption(string textBox,string readfile,string writefile)
{
try
{
if(textBox.Length >=8 && textBox.Length<=16 )
{
byte [] key = System.Text.Encoding.Default.GetBytes( textBox );
byte [] iv = rij.IV;
Rijndael crypt = Rijndael.Create();
ICryptoTransform transform = crypt.CreateDecryptor(key,iv);
//读取加密后的文件
FileStream fsopen = new FileStream(readfile,FileMode.Open);
CryptoStream cs = new CryptoStream( fsopen ,transform ,CryptoStreamMode.Read );
//把解密后的结果写进文件
FileStream fswrite = new FileStream(writefile,FileMode.OpenOrCreate);
int length;
while ((length = cs.ReadByte()) != -1)
fswrite.WriteByte((byte)length);

fswrite.Close();
cs.Close();
fsopen.Close();
deresult=true; //成功解密
MessageBox.Show("已经成功完成解密任务!");
}
else
{
      MessageBox.Show("密码的最小长度为8个字符,最大长度为16个字符!");
return ;
}
}
catch (Exception e)
{
MessageBox.Show(e.ToString());
}
}
//解密文件
private void button2_Click(object sender, System.EventArgs e)
{
decryption( textBox3.Text,textBox1.Text,textBox2.Text ) ;
if(checkBox1.Checked==true && deresult==true)
DeleteFile(textBox1.Text);
}
//用于打开文件的按钮
private void button3_Click(object sender, System.EventArgs e)
{
openfile = new OpenFileDialog();openfile.Filter ="All files (*.*)|*.*" ;
openfile.ShowDialog();
textBox1.Text = openfile.FileName;
ext = getfileext(openfile.FileName);
}

private void button4_Click(object sender, System.EventArgs e)
{
savefile = new SaveFileDialog();
savefile.Filter =ext + " files" +"(*."+ext+")|*."+ ext +"|All files (*.*)|*.*" ;
savefile.ShowDialog();
textBox2.Text = savefile.FileName;
}
//得到文件的扩展名
private string getfileext(string filename)
{
try
{
char [] point = new char[] {'.'};
string [] filename2 = filename.Split(point);
return filename2[1];
}
catch
{
return null;
}
}
//删除文件的函数
public void DeleteFile(string filename)
{
try
{
File.Delete(filename);
}
catch (Exception e)
{
MessageBox.Show(e.ToString());
}
}

}
}

时间: 2024-09-02 00:18:20

C#-对称加密的一个例子的相关文章

C#-对称加密的一个例子 Rijndael

加密 using System;using System.Drawing;using System.Collections;using System.ComponentModel;using System.Windows.Forms;using System.Data;using System.Security.Cryptography;using System.IO; namespace 对称加密应用{ ///  /// Form1 的摘要说明. ///  public class Form1

NET中的密钥(密码)学--对称加密

介绍 在.NET之前,使用非托管的Win32APIs加密解密数据是一件非常痛苦的事情.为了这个加密解密的目的,.NET配置了一组类(和命名空间).现在你有很多类可以使用每种不同 的算法保护你的数据.在.NET里面Crypttography命名空间下又定义了3种类型的加密方法.他们是AsymmetricAlgorithm,SymmetricAlgorithm和HashAlgorithm.所有的这些类(和.NET密码学类型)都是抽象类.我们今天将要描述SymmetricAlgorithm.剩下的将在

.Net加密与解密——对称加密

   一,思路       对称加密含有一个被称为密钥的东西,在消息发送前使用密钥对消息进行加密,得到密文并发送,接收方收到密文后,使用相同的密钥进行解密,获得原消息.   PS:使用密钥对消息进行加密的过程,由加密算法来完成的,加密算法通常也是公开的. 二,对称加密的流程             1,发送方和接收方持有相同的密钥,并严格保密 2,发送方使用密钥对消息进行加密,然后发送消息 3,接收方收到消息后,使用相同的密钥对消息进行解密 PS:在这一过程中,第三方可能截获消息,但得到的知识一

java EJB 加密与解密原理的一个例子_php技巧

加密与解密原理的一个例子 package lockunlock;  import Java.awt.*;  import java.awt.event.*;  import java.Applet.*;  import javax.Swing.*;  import java.util.*;  public class LockUnlock extends JApplet {  private boolean isStandalone = false;  //Get a parameter val

对称加密与非对称加密

1 对称加密 对称加密是最快速.最简单的一种加密方式,加密与解密用的是同样的密钥.对称加密有很多种算法,由于它效率很高,所以被广泛使用在很多加密协议的核心当中. 对称加密通常使用的是相对较小的密钥,一般小于256 bit.因为密钥越大加密越强,但加密与解密的过程越慢.若只用1 bit来做这个密钥,那黑客们可以先试着用0来解密,不行的话就再用1解密.但密钥有足够大,黑客们可能永远也无法破解,但加密和解密的过程要花费很长的时间.密钥的大小既要照顾到安全性,也要照顾到效率. 2000年10月2日,美国

PHP对称加密函数实现数据的加密解密_php实例

项目中有一个地方用到了将用户ID加密.传至下个接点进行反解的需求.(原谅我不能透漏太多-_-!),第一个想到的就是康盛Ucenter中的一个函数,后来搜了下,在简明魔法中也找到了个简单的方法,遂整合了下,形成了自己使用的函数. 一.对称加密 发送方将明文使用密钥和算法处理成密文发送出去,接收方使用密钥和算法将密文处理成明文,发收信双方使用同一个密钥对数据进行加密和解密. 因为使用同一个密钥加密.解密,所以安全性上不仅与算法有关,密钥的安全也很重要. 当然并不是密钥越复杂越好,相反密钥通常比较小的

PHP对称加密函数实现数据的加密解密

项目中有一个地方用到了将用户ID加密.传至下个接点进行反解的需求.(原谅我不能透漏太多-_-!),第一个想到的就是康盛Ucenter中的一个函数,后来搜了下,在简明魔法中也找到了个简单的方法,遂整合了下,形成了自己使用的函数. 一.对称加密 发送方将明文使用密钥和算法处理成密文发送出去,接收方使用密钥和算法将密文处理成明文,发收信双方使用同一个密钥对数据进行加密和解密. 因为使用同一个密钥加密.解密,所以安全性上不仅与算法有关,密钥的安全也很重要. 当然并不是密钥越复杂越好,相反密钥通常比较小的

对称加密(3) NET对称加密体系

本节介绍System.Security.Cryptography名称空间中的对称加密类. 1. SymmetricAlgorithm类 SymmetricAlgorithm是抽象类,是所有对称加密算法的基类,该类定义的成员在其子类AES类中做介绍. 当使用派生类时,从安全的角度考虑,仅在使用完对象后强制垃圾回收是不够的.必须对该对象显式调用Clear方法,以便在释放对象之前将对象中所包含的所有敏感数据清零.注意,垃圾回收并不会将回收对象的内容清零,只是将内存标记为可用于重新分配.因而,垃圾回收对

使用的DES对称加密

加密 在网站使用Cookie或者存放数据到数据库中的时候时常会用到加密解密,MD5非常好用,但是有的时候需要进行逆运算.那么此时DES对称加密就比较好用了.设定一个密钥,然后对所有的数据进行加密.代码介绍如下,事先声明仅为小弟个人理解,请各位多多指教Imports SystemImports System.IOImports System.TextImports System.DiagnosticsImports System.Security.CryptographyImports Syste