從COM組件調用.NET組件編程實戰 - 中國WEB開發者網絡 (http://www.webasp.net) -- 技術教程 (http://www.webasp.net/article/) --- 從COM組件調用.NET組件編程實戰 (http://www.webasp.net/article/9/8352.htm) |
| -- 作者:未知 -- 發佈日期: 2004-03-19 |
| 在我的編程實踐中,需要從.NET的Web Form頁面傳遞加密的字符串信息(如用戶名和密碼等)到ASP頁面,然後在該頁面對該加密字符串進行解密。如果傳遞的不是加密串,通過GET或POST的方式就可以直接傳遞並在ASP頁面中接收,但問題是在.NET的Web Form頁面中加了密的字符串如何才能在ASP中進行解密呢?這主要由於ASP並不能直接訪問由.NET提供的托管類和組件。這時我們就只能借助於COM組件來實現了,通過COM的互操作我們可通過.NET生成COM組件,然後在ASP頁面中訪問該COM組件就可以了。
本文實現的是將加密的用戶名與密碼從.aspx頁面傳遞到.asp頁面,下面就來介紹這些操作的具體步驟: 一、製作具有加密、解密字符串的.NET程序集(VS.NET類庫工程) 這個程序集將會變成COM組件,使用DES對稱加密代碼,可以加密碼,可以加密解密,支持中文! //文件名:StringCrypt.cs using System; using System.Runtime.InteropServices; using System.Security.Cryptography; using System.IO; using System.Text; namespace jonson { // 首先建立接口,這個是Com必須使用的 [Guid("BF6F9C17-37FA-4ad9-9601-C11AD5316F2C")] public interface IEncrypt { string Encrypt(string pToEncrypt,string sKey); string Decrypt(string pToDecrypt,string sKey); } //接口的實現 [Guid("3FBDBB63-3C36-4602-89E1-73EDB0F167D0")] public class StringCrypt : IEncrypt { // 加密的方法 public string Encrypt(string pToEncrypt, string sKey) { DESCryptoServiceProvider des = new DESCryptoServiceProvider(); //把字符串放到byte數組中 byte[] inputByteArray = Encoding.Default.GetBytes(pToEncrypt); //byte[] inputByteArray=Encoding.Unicode.GetBytes(pToEncrypt); //建立加密對象的密鑰和偏移量 //原文使用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; } //建立加密對象的密鑰和偏移量,此值重要,不能修改 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對象,CreateDecrypt使用的是流對象,必須把解密後的文本變成流對像 StringBuilder ret = new StringBuilder(); return System.Text.Encoding.Default.GetString(ms.ToArray()); } } } 說明:注意上面的Guid是使用vs.net工具菜單裡面的創建guid工具生成的,這個每個Com組件所必須的。輸入密匙的時候,必須使用英文字符,區分大小寫,且字符數量是8個,不能多也不能少,否則出錯。 然後使用vs.net的「Vsitual Studio .Net工具」-->Vistual Studio .Net命令提示符。在命令行內打下cd c:\ <回車> sn -k myKey.snk<回車> 這樣就在C盤根目錄下生成一個名叫myKey.snk的強名稱文件,然後將其拷貝到上述工程目錄中(與StringCrypt.cs文件同目錄)後關閉提示符窗口。 在vs.net的那個類庫工程自動生成的AssemblyInfo.cs文件內 把[assembly: AssemblyKeyFile("")]改成[assembly: AssemblyKeyFile("../../myKey.snk ")] 把[assembly: AssemblyVersion("1.0.*")]改成[assembly: AssemblyVersion("1.0.0.0")] // 注意:這時你的Com組件版本為1.0.0.0版 然後按Shift + Ctrl + B鍵生成dll庫(使用Release模式),StringCrypt.dll。這時候,程序集就建立成功了。 二、註冊該程序集並創建一個類型庫 仍然使用開始菜單中的Visual Studio .Net命令提示符 進入你的項目目錄,假設為D:\project\bin\Release 在對話框中輸入 d:<回車> cd project\bin\release<回車> 然後輸入 dir 命令可以看見StringCrypt.dll文件 然後輸入:regasm StringCrypt.dll<回車> 然後就在這個目錄下生成了StringCrypt.tlb類型庫文件。不要關閉此提示符窗口。 這時候,這個.dll的.net程序集就變成一個標準的Com組件了,但是還不能用,必須讓它變成全局Com組件。 這個regasm 實用程序將創建一個類型庫並在 Windows 註冊表中對其進行註冊,以便 COM Services可以訪問.NET組件。在使用regasm對.NET進行註冊之後,標準的Windows客戶就可以後期綁定組件中的類。註冊組件的過程必須一次完成。在.NET組件被註冊之後,所有的COM 客戶都可以訪問它。 三、將程序集添加到全局程序集緩存中 在使用.NET程序集之前,我們必須把程序集安裝到全局的高速緩存中。為此進入Visual Studio .Net提示符窗口,輸入 gacutil /I StringCrypt.dll<回車> 這時,你的這個dll就被複製到全局程序集緩存中了。也就是說無論在這個電腦的哪個硬盤上都可以使用此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頁面中接收並解密字符串 info = Request.QueryString(「info」) set obj = Server.CreateObject("jonson.StringCrypt") str1 = obj.Encrypt(info,"fk58Fgju") // 解密 本文的順利實現,得到了網友TomMax(笑望人生)等人的大力幫助,在此表示衷心的感謝。 |
| webasp.net |