作者:未知
文本的DES加密
为了对称加密的安全,将密码进行封装,先新建一个用于保存密码的类库cl:
using System;
using System.Text ;
namespace cl
{
/// <summary>
/// Class1 的摘要说明。
/// </summary>
public class Class1
{
public Class1()
{
}
public string getiv()
{
string iv="********";//八位
return iv;
}
public string getkey()
{
string key="01160129";//八位
return key;
}
}
}
然后新建asp.net项目(C#)
在 .aspx中
using cl;
namespace test//给一个文本产生一个散列值
{
/// <summary>
/// computehash 的摘要说明。
/// </summary>
public class computehash : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.TextBox TextBox1;
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.TextBox TextBox3;
protected System.Web.UI.WebControls.Button Button2;
protected System.Web.UI.WebControls.TextBox TextBox4;
protected System.Web.UI.WebControls.Button Button3;
protected System.Web.UI.WebControls.TextBox TextBox5;
protected System.Web.UI.WebControls.Button Button4;
protected System.Web.UI.WebControls.Button Button5;
protected System.Web.UI.WebControls.Label Label2;
protected System.Web.UI.WebControls.TextBox TextBox7;
protected System.Web.UI.WebControls.Label Label3;
protected System.Web.UI.WebControls.TextBox TextBox8;
protected System.Web.UI.WebControls.TextBox TextBox9;
protected System.Web.UI.WebControls.Label Label4;
protected System.Web.UI.WebControls.Button Button6;
protected System.Web.UI.WebControls.Label Label5;
protected System.Web.UI.WebControls.TextBox TextBox2;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Button2.Click += new System.EventHandler(this.Button2_Click);
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Button3.Click += new System.EventHandler(this.Button3_Click);
this.Button4.Click += new System.EventHandler(this.Button4_Click);
this.Button5.Click += new System.EventHandler(this.Button5_Click);
this.Button6.Click += new System.EventHandler(this.Button6_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void Button1_Click(object sender, System.EventArgs e)
{
byte[] bt=UTF8Encoding.UTF8.GetBytes(TextBox1.Text );//UTF8需要对Text的引用
MD5CryptoServiceProvider objMD5;
objMD5=new MD5CryptoServiceProvider ();
byte[] output=objMD5.ComputeHash (bt);
TextBox2.Text =BitConverter.ToString (output);
}
private void Button2_Click(object sender, System.EventArgs e)
{
byte[] bt=UTF8Encoding.UTF8.GetBytes(TextBox1.Text );//UTF8需要对Text的引用
MD5CryptoServiceProvider objMD5;
objMD5=new MD5CryptoServiceProvider ();
byte[] output=objMD5.ComputeHash (bt);
TextBox4.Text =BitConverter.ToString (output);
}
private void Button3_Click(object sender, System.EventArgs e)
{
Class1 cl=new Class1 ();
string iv1=cl.getiv ();
string key1=cl.getkey ();
byte[] iv=UTF8Encoding.UTF8 .GetBytes (iv1);
byte[] key=UTF8Encoding.UTF8 .GetBytes (key1);
byte[] source=UTF8Encoding.UTF8 .GetBytes (TextBox1.Text );
//定义加密对象
DESCryptoServiceProvider objdes;
objdes=new DESCryptoServiceProvider ();
//设置加密对象值
objdes.IV =iv;
objdes.Key =key;
//创建加密器对象
ICryptoTransform objEncryptor;
objEncryptor=objdes.CreateEncryptor (objdes.Key ,objdes.IV );
//准备将加密的文本写入secret.txt中
FileStream objfs;
objfs=new FileStream (MapPath("secret.txt"),FileMode.Create ,FileAccess.Write );
//写入
CryptoStream cryptostream;
cryptostream=new CryptoStream (objfs,objEncryptor,CryptoStreamMode.Write );
cryptostream.Write (source,0,source.Length );
cryptostream.Close ();
}
private void Button4_Click(object sender, System.EventArgs e)
{
Class1 cl=new Class1 ();
string iv1=cl.getiv ();
string key1=cl.getkey ();
byte[] iv=UTF8Encoding.UTF8 .GetBytes (iv1);
byte[] key=UTF8Encoding.UTF8 .GetBytes (key1);
byte[] source=UTF8Encoding.UTF8 .GetBytes (TextBox1.Text );
//定义加密对象
DESCryptoServiceProvider objdes;
objdes=new DESCryptoServiceProvider ();
//设置加密对象值
objdes.IV =iv;
objdes.Key =key;
//创建加密器对象
ICryptoTransform objEncryptor;
objEncryptor=objdes.CreateEncryptor (objdes.Key ,objdes.IV );
//写到内存
MemoryStream ms=new MemoryStream ();
CryptoStream cs=new CryptoStream (ms,objEncryptor,CryptoStreamMode.Write );
cs.Write (source,0,source.Length );
cs.FlushFinalBlock ();
ms.Close ();
TextBox5.Text =BitConverter.ToString (ms.ToArray ());
}
private void Button5_Click(object sender, System.EventArgs e)
{
DSACryptoServiceProvider objdsa;
objdsa=new DSACryptoServiceProvider ();
byte[] source=UTF8Encoding.UTF8 .GetBytes (TextBox1.Text );
//公开秘钥
TextBox8.Text =objdsa.ToXmlString (false);
//私有秘钥
TextBox9.Text =objdsa.ToXmlString (true);
//数字签名
TextBox7.Text =BitConverter.ToString (objdsa.SignData (source));
}
}
}
分享到:
相关推荐
DSA数字签 DSA数字签DSA数字签名 DSA数字签名
实现数字签名算法以及验证数字签名算法的正确性。本程序经本人验证
在一些初始化处理后,MD5以512位分组来处理输入文本,每一分组又划分为16个32位子分组。算法的输出由四个32位分组组成,将它们级联形成一个128位散列值。 首先填充消息使其长度恰好为一个比512位的倍数仅小64位的数...
DSA数字签名算法 包括MD5,sha1的信息摘要算法。文件导入功能
通过实现数字签名算法(DSA),加深对数字签名算法的理解,同时学习Hash算法的实现。 1)利用C\C++语言实现DSA算法。 2)DSA中的Hash函数采用SHA算法。
详细的DSA源码,并经过验证和分析,对DSA数字签名的安全分析非常有用
java AES,DES,3DES,RAS,DSA 加密算法实现
我不知道是这个DSA算法本身有问题还是什么.验证签名这块也有问题
DSA 数字签名 本程序实现DSA数字签名 是学习和研究密码学的良好资源 DSA 数字签名 本程序实现DSA数字签名 是学习和研究密码学的良好资源
java写的DSA数字签名算法,是密码学中的典型算法
实现数字签名功能,通过DSA算法实现,包含源程序等
NULL 博文链接:https://smallbee.iteye.com/blog/1611875
java关于加密的算法demo代码(Base64加密\数字签名\对称加密算法\非对称加密算法\消息摘要算法) JAVA安全实现三种方式: 1.JDK 2.Commons Codec 3.Bouncy Castle 一。非对称加密算法: 1.DH 2.RSA 3.ELGamal 二。...
DSA数字签名的安全性分析
DSA 数字签名算法 VC++实现 绝对好用 能够对文件进行数字签名 防止篡改
DSA签名程序和DSA验证程序。 签名程序流程: a. 读入字符串(从屏幕或文本文件中),字符串内容应包含自己的学号或姓名; b. 计算该字符串的SHA-1值; c. 生成DSA密钥对(利用dsa_make_key); d. 利用dsa_export...
各种加密算法比较md5_DSA_DES_RSA.doc
?理解公钥密码及数字签名相关的基本概念 ?理解并能够编写RSA或DSA数字签名算法 ?熟练应用C++编程实现数字签名体制
1)利用C\C++语言实现DSA算法。 2)DSA中的Hash函数采用SHA算法。 (1)消息填充:因为我们存储的时候是以字节为单位存储的,所以消息的长度(单位:位)一定是 8 的倍数。而我们填充的时候也一定是 8 位、8 位...
DSA数字签名算法.doc