作者:未知
文本的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));
}
}
}
分享到:
相关推荐
4. **LAM(Lamport signatures)**:Lamport签名是Leslie Lamport提出的一种早期的数字签名方案,它使用一对单向散列函数来创建一个短签名,能够验证消息的完整性和来源。 5. **LZW(Lempel-Ziv-Welch)**:LZW是一...
- **MD5**:生成128位固定长度的散列值,广泛用于文件完整性校验,但由于存在碰撞问题,安全性较低。 - **SHA**:包括SHA-1和SHA-2系列,可以生成不同长度的散列值,SHA-256和SHA-512提供更高的安全性。 - **MAC...
本文将深入探讨几种常见的加密算法,包括BASE64、MD5、SHA、HMAC,并简述DES、PBE、RSA、DH、DSA和ECC,旨在为读者提供一个全面的视角,理解这些算法的工作原理及其应用场景。 ### BASE64编码 BASE64是一种用于将...
#### 一、BASE64与单向加密算法MD5&SHA&MAC ##### BASE64 BASE64是一种编码格式,并非真正的加密算法。它主要用于将二进制数据转换成文本格式的数据,以便在网络上传输。由于某些传输协议只支持ASCII字符集,因此...
这包括加密(Encryption)、解密(Decryption)、数字签名(Digital Signatures)、消息认证码(Message Authentication Codes, MACs)以及认证(Authentication)等技术。 接着,我们来看看"算法"这一概念。在密码...
4. **全面的算法支持**:包括对AES、DES、3DES、Blowfish、Twofish、RSA、DSA、ECC等多种加密和签名算法的支持。 5. **易用性**:提供清晰的API接口,使得集成到项目中变得简单直观。 ### 二、Cryptopp库中的核心...
- **RSA**: RSA是最常用的公钥加密算法之一,广泛应用于数字签名和数据加密。 - **DSA**: DSA主要用于数字签名,其安全性基于离散对数问题。 - **ECDSA**: ECDSA是基于椭圆曲线的数字签名算法,相比传统的RSA/DSA,...
- **DSA (Digital Signature Algorithm)**: 专用于数字签名的算法。 - **ECDSA (Elliptic Curve Digital Signature Algorithm)**: 基于椭圆曲线的数字签名算法,提供更高的安全性。 **1.4 回调函数** 在OpenSSL中...
摘要算法(又称散列算法)能将任意长度的数据转化为固定长度的摘要信息,如MD5、SHA系列算法。公钥算法则涉及非对称加密技术,包括RSA、DSA、ECC(椭圆曲线密码学)等算法。 书中还对OpenSSL的安装过程进行了说明,...
- **应用场景**: 常用于验证数据的完整性和一致性,例如数字签名、消息认证码(MAC)等。 **1.3 公钥算法** - **定义**: 公钥加密算法使用一对密钥——公钥和私钥,其中公钥用于加密,私钥用于解密。 - **常见算法**...