C#-RijndaelManaged

原创 2004年07月20日 21:16:00

这个小程序,算不上什么.为了节省时间.请看代码!

 

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 对称加密应用
{
 /// <summary>
 /// Form1 的摘要说明。
 /// </summary>
 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;
  /// <summary>
  /// 必需的设计器变量。
  
  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;
  
  /// </summary>
  private System.ComponentModel.Container components = null;

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

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

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

  #region Windows 窗体设计器生成的代码
  /// <summary>
  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  /// 此方法的内容。
  /// </summary>
  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

  /// <summary>
  /// 应用程序的主入口点。
  /// </summary>
  [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());
   }
  }


 }
}

使用RijndaelManaged对epub文件进行加密

最近工作中遇到的问题,需要对epub进行加密,浏览了几种加密后最后敲定的使用该方法进行加密解密,自己就稍微写了些,可以实现加密解密,但是里面还有很多不完善的地方。谢谢日志,做做mark,后期继续改进。...
  • FlyAurora
  • FlyAurora
  • 2015-03-13 15:38:46
  • 1481

C# 加密字符串(RijndaelManaged)

public static string GetNewToken(string loginName, string randomKey, string culture, string country)...
  • dannywj1371
  • dannywj1371
  • 2011-05-25 09:12:00
  • 7661

C#实现AES加解密方法

using System; using System.Collections.Generic; using System.Text; using System.Security.Cryptogr...
  • yxtyxt3311
  • yxtyxt3311
  • 2011-11-22 13:10:20
  • 18567

C#实现AES(Rijndael算法)加密解密

AES(Rijndael算法) Rijndael(读作rain-dahl)是由美国国家标准与技术协会(NIST)所选的高级加密标准(AES)的候选算法。 Rijndael算法首先是一个密钥分组加...
  • qq563129582
  • qq563129582
  • 2017-05-16 11:20:12
  • 3845

AES 加密,net和java 通用加密

项目里面很多地方用到加密,网上找了圈不尽人意,这里整理一下
  • jy_justsoso
  • jy_justsoso
  • 2014-07-25 10:12:12
  • 2874

C# 加密-Rijndael

Rijndael 属对称加密,对称加密在加密和解密时都使用相同的密钥。2000 年 10 月,NIST 选择 Rijndael(发音为 "Rhine dale")作为 AES 算法,用以取代 DES。...
  • deepwishly
  • deepwishly
  • 2009-12-25 23:25:00
  • 1988

MD5、DES、RC2、Rijndael加密算法C#源码

//DES.cs //--------------------------- using System; using System.Collections.Generic; using System....
  • woshinia
  • woshinia
  • 2012-09-27 11:35:17
  • 2461

C#加密解密

利用C#方法给字符串加密解密 using UnityEngine; using System; using System.Security.Cryptography; using System.Te...
  • LIQIANGEASTSUN
  • LIQIANGEASTSUN
  • 2014-12-30 14:10:23
  • 1110

DES和AES加密:指定键的大小对于此算法无效

“System.ArgumentException”类型的未经处理的异常在 mscorlib.dll 中发生 其他信息: 指定键的大小对于此算法无效。 在看DES和AES加密的时候,找了个...
  • myinc
  • myinc
  • 2017-01-22 17:23:16
  • 3874

C#-RijndaelManaged

这个小程序,算不上什么.为了节省时间.请看代码!   using System;using System.Drawing;using System.Collections;using System.C...
  • nieyong
  • nieyong
  • 2004-07-20 21:16:00
  • 5328
收藏助手
不良信息举报
您举报文章:C#-RijndaelManaged
举报原因:
原因补充:

(最多只允许输入30个字)