webform1.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="work078.WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>DES加密解密的示例</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table border="0">
<tr>
<td colspan="2" align="center">DES加密解密的示例</td>
</tr>
<tr>
<td>
<asp:TextBox ID="txtSource" runat="server" Columns="24" Rows="6" TextMode="MultiLine"></asp:TextBox>
</td>
<td>
<asp:TextBox ID="txtResult" runat="server" Columns="24" Rows="6" TextMode="MultiLine"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Button ID="Button1" runat="server" Text="DES加密" OnClick="Button1_Click" />
</td>
<td>
<asp:Button ID="Button2" runat="server" Text="DES解密" OnClick="Button2_Click" />
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
webform1.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace work078
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
/// <summary>
/// 加密
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Button1_Click(object sender, EventArgs e)
{
string sourceStr = txtSource.Text;
txtSource.Text = DESProvider.EncryptString(sourceStr);
txtResult.Text = DESProvider.EncryptString(sourceStr);
}
/// <summary>
/// 解密
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Button2_Click(object sender, EventArgs e)
{
txtResult.Text = DESProvider.DecryptString(txtResult.Text);
}
}
}
desprovider.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace work078
{
/// <summary>
/// DES 算法加密 / 解密
/// </summary>
public class DESProvider
{
private DESProvider()
{
}
//默认秘钥
private static string key = "abcdefgh";
/// <summary>
/// 获取,设置 秘钥
/// </summary>
public static string Key
{
get
{
return DESProvider.key;
}
set
{
DESProvider.key = value;
}
}
/// <summary>
/// DES加密
/// </summary>
/// <param name="enString"></param>
/// <param name="key"></param>
/// <returns></returns>
public static string EncryptString(string enString, string key)
{
if (string.IsNullOrEmpty(enString))
{
throw new ArgumentNullException("enString", "不能为空");
}
if (string.IsNullOrEmpty(key))
{
throw new ArgumentNullException("key","不能为空");
}
//将秘钥转换成字节数组
byte[] keyBytes = System.Text.Encoding.UTF8.GetBytes(key);
//设置初始化向量
byte[] keyIV = keyBytes;
//将加密字符串转换成UTF8编码的字节数组
byte[] inputByteArray = System.Text.Encoding.UTF8.GetBytes(enString);
//
byte[] resultByteArray = EncryBytes(inputByteArray, keyBytes, keyIV);
return Convert.ToBase64String(resultByteArray);
}
/// <summary>
/// DES加密
/// </summary>
/// <param name="enString"></param>
/// <returns></returns>
public static string EncryptString(string enString)
{
return EncryptString(enString, DESProvider.Key);
}
/// <summary>
///
/// </summary>
/// <param name="sourceBytes"></param>
/// <param name="keyBytes"></param>
/// <param name="keyIV"></param>
/// <returns></returns>
private static byte[] EncryBytes(byte[] sourceBytes, byte[] keyBytes, byte[] keyIV)
{
if (sourceBytes == null || keyBytes == null || keyIV == null)
{
throw new ArgumentNullException("sourceBytes和keyBytes", "不能为空");
}
else
{
keyBytes = CheckByteArrayLength(keyBytes);
keyIV = CheckByteArrayLength(keyIV);
System.Security.Cryptography.DESCryptoServiceProvider provider = new System.Security.Cryptography.DESCryptoServiceProvider();
System.IO.MemoryStream mStream = new System.IO.MemoryStream();
System.Security.Cryptography.CryptoStream cStream = new System.Security.Cryptography.CryptoStream(mStream, provider.CreateEncryptor(keyBytes,keyIV), System.Security.Cryptography.CryptoStreamMode.Write);
cStream.Write(sourceBytes, 0, sourceBytes.Length);
cStream.FlushFinalBlock();
byte[] buffer = mStream.ToArray();
mStream.Close();
cStream.Close();
return buffer;
}
}
/// <summary>
/// 检查秘钥或初始化向量长度,如果不是8的倍数或长度大于64,则截取前8个元素
/// </summary>
/// <param name="byteArray"></param>
/// <returns></returns>
private static byte[] CheckByteArrayLength(byte[] byteArray)
{
byte[] resultBytes = new byte[8];
if (byteArray.Length < 8)
{
return System.Text.Encoding.UTF8.GetBytes("12345678");
}
else if (byteArray.Length % 8 != 0 || byteArray.Length > 64)
{
//复制前8位
Array.Copy(byteArray, 0, resultBytes, 0, 8);
return resultBytes;
}
else
{
return byteArray;
}
}
/// <summary>
/// 解密
/// </summary>
/// <param name="deString"></param>
/// <param name="key"></param>
/// <returns></returns>
public static string DecryptString(string deString, string key)
{
if (string.IsNullOrEmpty(deString))
{
throw new ArgumentNullException("deString", "不能为空");
}
if (string.IsNullOrEmpty(key))
{
throw new ArgumentNullException("key", "不能为空");
}
byte[] keyBytes = System.Text.Encoding.UTF8.GetBytes(key);
byte[] keyIV = keyBytes;
byte[] inputByteArray = Convert.FromBase64String(deString);
byte[] resultByteArray = DecryptBytes(inputByteArray, keyBytes, keyIV);
return System.Text.Encoding.UTF8.GetString(resultByteArray);
}
public static string DecryptString(string deString)
{
return DecryptString(deString,DESProvider.Key);
}
/// <summary>
///
/// </summary>
/// <param name="sourceBytes"></param>
/// <param name="keyBytes"></param>
/// <param name="keyIV"></param>
/// <returns></returns>
private static byte[] DecryptBytes(byte[] sourceBytes, byte[] keyBytes, byte[] keyIV)
{
if (sourceBytes == null || keyBytes == null || keyIV == null)
{
throw new ArgumentNullException("sourceBytes,keyBytes,keyIV", "不能为空");
}
else
{
keyBytes = CheckByteArrayLength(keyBytes);
keyIV = CheckByteArrayLength(keyIV);
System.Security.Cryptography.DESCryptoServiceProvider provider = new System.Security.Cryptography.DESCryptoServiceProvider();
System.IO.MemoryStream mStream = new System.IO.MemoryStream();
System.Security.Cryptography.CryptoStream cStream = new System.Security.Cryptography.CryptoStream(mStream, provider.CreateDecryptor(keyBytes, keyIV), System.Security.Cryptography.CryptoStreamMode.Write);
cStream.Write(sourceBytes, 0, sourceBytes.Length);
cStream.FlushFinalBlock();
byte[] buffer = mStream.ToArray();
mStream.Close();
cStream.Close();
return buffer;
}
}
}
}