在我的編程實(shí)踐中,需要從.NET的Web Form頁(yè)面?zhèn)鬟f加密的字符串信息(如用戶名和密碼等)到ASP頁(yè)面,然后在該頁(yè)面對(duì)該加密字符串進(jìn)行解密。如果傳遞的不是加密串,通過(guò)GET或POST的方式就可以直接傳遞并在ASP頁(yè)面中接收,但問(wèn)題是在.NET的Web Form頁(yè)面中加了密的字符串如何才能在ASP中進(jìn)行解密呢?這主要由于ASP并不能直接訪問(wèn)由.NET提供的托管類(lèi)和組件。這時(shí)我們就只能借助于COM組件來(lái)實(shí)現(xiàn)了,通過(guò)COM的互操作我們可通過(guò).NET生成COM組件,然后在ASP頁(yè)面中訪問(wèn)該COM組件就可以了。
本文實(shí)現(xiàn)的是將加密的用戶名與密碼從.aspx頁(yè)面?zhèn)鬟f到.asp頁(yè)面,下面就來(lái)介紹這些操作的具體步驟:
一、制作具有加密、解密字符串的.NET程序集(VS.NET類(lèi)庫(kù)工程)
這個(gè)程序集將會(huì)變成COM組件,使用DES對(duì)稱加密代碼,可以加密碼,可以加密解密,支持中文!
//文件名:StringCrypt.cs
using System;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
using System.IO;
using System.Text;
namespace jonson
{
// 首先建立接口,這個(gè)是Com必須使用的
[Guid("BF6F9C17-37FA-4ad9-9601-C11AD5316F2C")]
public interface IEncrypt
{
string Encrypt(string pToEncrypt,string sKey);
string Decrypt(string pToDecrypt,string sKey);
}
//接口的實(shí)現(xiàn)
[Guid("3FBDBB63-3C36-4602-89E1-73EDB0F167D0")]
public class StringCrypt : IEncrypt
{
// 加密的方法
public string Encrypt(string pToEncrypt, string sKey)
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
//把字符串放到byte數(shù)組中
byte[] inputByteArray = Encoding.Default.GetBytes(pToEncrypt);
//byte[] inputByteArray=Encoding.Unicode.GetBytes(pToEncrypt);
//建立加密對(duì)象的密鑰和偏移量
//原文使用ASCIIEncoding.ASCII方法的GetBytes方法
//使得輸入密碼必須輸入英文文本
des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(),CryptoStreamMode.Write);
//Write the byte array into the crypto stream
//(It will end up in the memory stream)
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
//Get the data back from the memory stream, and into a string
StringBuilder ret = new StringBuilder();
foreach(byte b in ms.ToArray())
{
//Format as hex
ret.AppendFormat("{0:X2}", b);
}
ret.ToString();
return ret.ToString();
}
// 解密的方法
public string Decrypt(string pToDecrypt, string sKey)
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
//Put the input string into the byte array
byte[] inputByteArray = new byte[pToDecrypt.Length / 2];
for(int x = 0; x < pToDecrypt.Length / 2; x++)
{
int i = (Convert.ToInt32(pToDecrypt.Substring(x * 2, 2), 16));
inputByteArray[x] = (byte)i;
}
//建立加密對(duì)象的密鑰和偏移量,此值重要,不能修改
des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
//Flush the data through the crypto stream into the memory stream
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
//Get the decrypted data back from the memory stream
//建立StringBuilder對(duì)象,CreateDecrypt使用的是流對(duì)象,必須把解密后的文本變成流對(duì)象
StringBuilder ret = new StringBuilder();
return System.Text.Encoding.Default.GetString(ms.ToArray());
}
}
}
說(shuō)明:注意上面的Guid是使用vs.net工具菜單里面的創(chuàng)建guid工具生成的,這個(gè)每個(gè)Com組件所必須的。輸入密匙的時(shí)候,必須使用英文字符,區(qū)分大小寫(xiě),且字符數(shù)量是8個(gè),不能多也不能少,否則出錯(cuò)。
然后使用vs.net的“Vsitual Studio .Net工具”-->Vistual Studio .Net命令提示符。在命令行內(nèi)打下cd c:\ <回車(chē)>
sn -k myKey.snk<回車(chē)>
這樣就在C盤(pán)根目錄下生成一個(gè)名叫myKey.snk的強(qiáng)名稱文件,然后將其拷貝到上述工程目錄中(與StringCrypt.cs文件同目錄)后關(guān)閉提示符窗口。
在vs.net的那個(gè)類(lèi)庫(kù)工程自動(dòng)生成的AssemblyInfo.cs文件內(nèi)
把[assembly: AssemblyKeyFile("")]改成[assembly: AssemblyKeyFile("../../myKey.snk ")]
把[assembly: AssemblyVersion("1.0.*")]改成[assembly: AssemblyVersion("1.0.0.0")] // 注意:這時(shí)你的Com組件版本為1.0.0.0版
然后按Shift + Ctrl + B鍵生成dll庫(kù)(使用Release模式),StringCrypt.dll。這時(shí)候,程序集就建立成功了。
二、注冊(cè)該程序集并創(chuàng)建一個(gè)類(lèi)型庫(kù)
仍然使用開(kāi)始菜單中的Visual Studio .Net命令提示符
進(jìn)入你的項(xiàng)目目錄,假設(shè)為D:\project\bin\Release
在對(duì)話框中輸入
d:<回車(chē)>
cd project\bin\release<回車(chē)>
然后輸入 dir 命令可以看見(jiàn)StringCrypt.dll文件
然后輸入:regasm StringCrypt.dll<回車(chē)>
然后就在這個(gè)目錄下生成了StringCrypt.tlb類(lèi)型庫(kù)文件。不要關(guān)閉此提示符窗口。
這時(shí)候,這個(gè).dll的.net程序集就變成一個(gè)標(biāo)準(zhǔn)的Com組件了,但是還不能用,必須讓它變成全局Com組件。
這個(gè)regasm 實(shí)用程序?qū)?chuàng)建一個(gè)類(lèi)型庫(kù)并在 Windows 注冊(cè)表中對(duì)其進(jìn)行注冊(cè),以便 COM Services可以訪問(wèn).NET組件。在使用regasm對(duì).NET進(jìn)行注冊(cè)之后,標(biāo)準(zhǔn)的Windows客戶就可以后期綁定組件中的類(lèi)。注冊(cè)組件的過(guò)程必須一次完成。在.NET組件被注冊(cè)之后,所有的COM 客戶都可以訪問(wèn)它。
三、將程序集添加到全局程序集緩存中
在使用.NET程序集之前,我們必須把程序集安裝到全局的高速緩存中。為此進(jìn)入Visual Studio .Net提示符窗口,輸入
gacutil /I StringCrypt.dll<回車(chē)>
這時(shí),你的這個(gè)dll就被復(fù)制到全局程序集緩存中了。也就是說(shuō)無(wú)論在這個(gè)電腦的哪個(gè)硬盤(pán)上都可以使用此dll組件了。
四、使用方法
1. 在source.aspx中生成加密串
using jonson;
…
jonson.StringCrypt crypt = new jonson.StringCrypt();
String tmpstr = username+"^"+password;
… …
strinfo = crypt.Encrypt(tmpstr,"fk58Fgju"); // fk58Fgju為密匙
Response.Redirect("target.asp?info="+strinfo);
2. 在target.asp頁(yè)面中接收并解密字符串
info = Request.QueryString(“info”)
set obj = Server.CreateObject("jonson.StringCrypt")
str1 = obj.Encrypt(info,"fk58Fgju") // 解密
本文的順利實(shí)現(xiàn),得到了網(wǎng)友TomMax(笑望人生)等人的大力幫助,在此表示衷心的感謝。
|