首页 > Linux操作系统 > Linux操作系统 > .NET中的密码学--对称加密
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 WindowsApplication5
{
///
/// Form1 的摘要说明。
///
public class Form1 : System.Windows.Forms.Form.
{
private System.Windows.Forms.Button button1;
private System.Windows.Forms.TextBox txtData;
///
/// 必需的设计器变量。
///
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.button1 = new System.Windows.Forms.Button();
this.txtData = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(64, 168);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(80, 48);
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// txtData
//
this.txtData.Location = new System.Drawing.Point(56, 64);
this.txtData.Name = "txtData";
this.txtData.TabIndex = 1;
this.txtData.Text = "";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.Add(this.txtData);
this.Controls.Add(this.button1);
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)
{
SymmetricAlgorithm mCryptProv;
MemoryStream mMemStr;
// 加密txtData中的数据,然后将加密结果用MessageBox显示并且回写到TextBox中
// 这里你可以配置任何.NET支持的类
mCryptProv = SymmetricAlgorithm.Create("Rijndael");
// 加密数据将要以流的形式存储在内存中因此我们需要内存Stream对象
mMemStr = new MemoryStream();
//创建ICryptToransform对象.(在这里我们使用默认的密钥和初始向量).
ICryptoTransform mTransform = mCryptProv.CreateEncryptor();
CryptoStream mCSWriter = new CryptoStream(mMemStr,mTransform,CryptoStreamMode.Write);
StreamWriter mSWriter = new StreamWriter(mCSWriter);
mSWriter.Write(this.txtData.Text);
mSWriter.Flush();
mCSWriter.FlushFinalBlock();
// 有一件事情在这里需要注意我们没有在代码的任何地方使用IV和密钥。事实上,在我们在代码里面没有指定他们的时候.NET Framework将为我们自动产生。但是本文包含的例子代码使用的是用户指定的密钥和IV。我们将加密以后的数据使用MemoryStream写到内存中。现在让我们从内存中得到数据的代码。
//数据已经写入内存但是我们需要回显它到TextBox和MessageBox中,因此要做下面的工作。
//为接受数据创建字节数组。
byte[] mBytes = new byte[mMemStr.Length - 1];
mMemStr.Position = 0;
mMemStr.Read(mBytes,0,mBytes.Length);
String mEncData = System.Text.Encoding.UTF8.GetString(mBytes);
MessageBox.Show("加密数据为:\n"+mEncData);
this.txtData.Text = mEncData;
// 从字节转换为字符串必须要编码。在这里我使用了UTF8Encoding。最后,让我们将解密后的数据再次显示在MessageBox和TextBox中。
//
// 现在让我们从内存中得到解密后的数据
//
// 因为我们的数据在内存中,所以我们需要重新使用MemoryStream对象。
//
// 将内存点置0
mMemStr.Position = 0;
mTransform = mCryptProv.CreateDecryptor();
CryptoStream mCSReader = new CryptoStream(mMemStr,mTransform,CryptoStreamMode.Read);
StreamReader mStrReader = new StreamReader(mCSReader);
String mDecData = mStrReader.ReadToEnd();
MessageBox.Show("解密数据为:\n"+mDecData);
this.txtData.Text = mDecData;
}
}
}
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/12639172/viewspace-545021/ ,如需转载,请注明出处,否则将追究法律责任。