命名空间: System.Security.Cryptography
定义访问数据加密标准 (DES) 算法的加密服务提供程序 (CSP) 版本的包装对象。无法继承此类。
测试1
加密页面
protected void Page_Load(object sender, EventArgs e)
{
// Create a new DES key.
DESCryptoServiceProvider key = new DESCryptoServiceProvider();
Session["key"] = key;
byte[] buffer = Encrypt("This is some plaintext!", key);
int len = buffer.Length;
char[] arr = new char[len];
string s = "";
foreach (byte b in buffer)
{s += Convert.ToString(Convert.ToChar(b)) + "|";}
Response.Redirect("a.aspx?s="+(@s));
}
// Encrypt the string.
public static byte[] Encrypt(string PlainText, SymmetricAlgorithm key)
{
// Create a memory stream.
MemoryStream ms = new MemoryStream();
// Create a CryptoStream using the memory stream and the
// CSP DES key.
CryptoStream encStream = new CryptoStream(ms, key.CreateEncryptor(), CryptoStreamMode.Write);
// Create a StreamWriter to write a string
// to the stream.
StreamWriter sw = new StreamWriter(encStream);
// Write the plaintext to the stream.
sw.WriteLine(PlainText);
// Close the StreamWriter and CryptoStream.
sw.Close();
encStream.Close();
// Get an array of bytes that represents
// the memory stream.
byte[] buffer = ms.ToArray();
// Close the memory stream.
ms.Close();
// Return the encrypted byte array.
return buffer;
}
解密页面
if (Session["key"] != null)
{
if (Request.QueryString["s"] != null)
{
DESCryptoServiceProvider key = new DESCryptoServiceProvider();
key = (DESCryptoServiceProvider)Session["key"];
string s = Request.QueryString["s"].ToString();
string[] sarr = s.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
int len = sarr.Length;
byte[] buffer = new byte[len];
for (int i = 0; i < len; i++)
{
buffer[i] = Convert.ToByte(Convert.ToChar(sarr[i]));
}
string plaintext = Decrypt(buffer, key);
Response.Write(plaintext);
}
}
}
// Decrypt the byte array.
public static string Decrypt(byte[] CypherText, SymmetricAlgorithm key)
{
// Create a memory stream to the passed buffer.
MemoryStream ms = new MemoryStream(CypherText);
// Create a CryptoStream using the memory stream and the
// CSP DES key.
CryptoStream encStream = new CryptoStream(ms, key.CreateDecryptor(), CryptoStreamMode.Read);
// Create a StreamReader for reading the stream.
StreamReader sr = new StreamReader(encStream);
// Read the stream as a string.
string val = sr.ReadLine();
// Close the streams.
sr.Close();
encStream.Close();
ms.Close();
return val;
}
这里是应用了SESSION 的OBJECT值类型和MSDN上的例子结合自己的业务需求做的,但是我如果要实现这种加密和解密不可能在服务器上存个SESSION,所以我要把KEY整成字符串,以前弄过XML的加密解密的,只在把key.Key 转成我的的字符串保留下来就OK了,但是后来发现不能这样,后在网上搜到一资料
using System;
using System.Text;
using System.Globalization;
using System.Security.Cryptography;
class DES
{
// 创建Key
public string GenerateKey()
{
DESCryptoServiceProvider desCrypto = (DESCryptoServiceProvider)DESCryptoServiceProvider.Create();
return ASCIIEncoding.ASCII.GetString(desCrypto.Key);
}
// 加密字符串
public string EncryptString(string sInputString, string sKey)
{
byte [] data = Encoding.UTF8.GetBytes(sInputString);
DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
ICryptoTransform desencrypt = DES.CreateEncryptor();
byte [] result = desencrypt.TransformFinalBlock(data, 0, data.Length);
return BitConverter.ToString(result);
}
// 解密字符串
public string DecryptString(string sInputString, string sKey)
{
string [] sInput = sInputString.Split("-".ToCharArray());
byte [] data = new byte[sInput.Length];
for(int i = 0; i < sInput.Length; i++)
{
data[i] = byte.Parse(sInput[i], NumberStyles.HexNumber);
}
DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
ICryptoTransform desencrypt = DES.CreateDecryptor();
byte [] result = desencrypt.TransformFinalBlock(data, 0, data.Length);
return Encoding.UTF8.GetString(result);
}
}
class Test
{
static void Main()
{
DES des = DES.Create();
string key = des.GenerateKey();
string s0 = "中国软件 - csdn.net";
string s1 = des.EncryptString(s0, key);
string s2 = des.DecryptString(s1, key);
Console.WriteLine("原串: [{0}]", s0);
Console.WriteLine("加密: [{0}]", s1);
Console.WriteLine("解密: [{0}]", s2);
}
}
/* 程序输出:
原串: [中国软件 - csdn.net]
加密: [E8-30-D0-F2-2F-66-52-14-45-9A-DC-C5-85-E7-62-9B-AD-B7-82-CF-A8-0A-59-77]
解密: [中国软件 - csdn.net]
*/
按照上面的再改一下
可以成功,但是为什么自己的会失败呢,原来代码中少了一个东西 DES.IV,是一个什么初始化向量呵呵,
看来MS的东西用是好用,但是出了问题或是自己想搞深点就不行了呀
后来自己加了这一行后,一切OK