请你参考将.net程序集转化为COM接口的文章
提示一下,可以用Regasm.exe这个工具

解决方案 »

  1.   

    我使用 Regasm c:\test.dll 能注册成功,但在ASP中就是不能用。另外我使用“组件服务”
    来注册时,注册老是失败。???
      

  2.   

    帮你找一个贴子:
    从COM组件调用.NET组件编程实战作者:朱学武 在我的编程实践中,需要从.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.csusing 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(笑望人生)等人的大力帮助,在此表示衷心的感谢。
      

  3.   

    regasm StringCrypt.dll<回车>
    在目录下生成了怎么不生成StringCrypt.tlb类型库文件呀?这个类型库有什么作用?