参考了http://www.80diy.com/home/20040201/20/2693224.html 

http://www.i-gou.com/bbs/dispbbs.asp?boardID=16&ID=1230&page=1

这里只是写的更详细一些

从COM组件调用.NET组件编程实战   
    
作者:朱学武

需求:asp.net主站有些信息使用DES方式加解密。分站是asp的信息使用md5加密。现在要统一成使用DES方式加密。

分析,这就涉及到asp调用vs2005写的dll问题。

步骤:

1.使用vs2005编写DLL(在一台机器上:windows2003+vs2005)。

新建一个c#类库工程。//文件名:StringCrypt.cs  

asp访问c#创建的加密解密dll(封装DESCryptoServiceProvider)using System;
asp访问c#创建的加密解密dll(封装DESCryptoServiceProvider)
using System.Runtime.InteropServices;
asp访问c#创建的加密解密dll(封装DESCryptoServiceProvider)
using System.Security.Cryptography;
asp访问c#创建的加密解密dll(封装DESCryptoServiceProvider)
using System.IO;
asp访问c#创建的加密解密dll(封装DESCryptoServiceProvider)
using System.Text;
asp访问c#创建的加密解密dll(封装DESCryptoServiceProvider)
asp访问c#创建的加密解密dll(封装DESCryptoServiceProvider)
namespace jonson   

说明:注意上面的Guid是使用vs.net工具菜单里面的创建guid工具生成的,这个每个Com组件所必须的。在vs2005中点击“工具-创建GUID”,在GUID Format中选择第4项Registry Format (ie. xxxxxxxx-xxxx...xxxx),点击“New GUID”,在“Reslut”中即可看到新的GUID。点击Copy,即被粘贴到剪切板上。

2.生成snk文件

使用vs.net的“Vsitual Studio .Net工具”-->Vistual Studio .Net命令提示符。在命令行内打下cd   c:\   <回车>  
   
  sn   -k   myKey.snk<回车>  
   
  这样就在C盘根目录下生成一个名叫myKey.snk的强名称文件,然后将其拷贝到上述工程目录中(与StringCrypt.cs文件同目录)后关闭提示符窗口。  
   
  在vs.net的那个类库工程自动生成的AssemblyInfo.cs文件内   
 把   

asp访问c#创建的加密解密dll(封装DESCryptoServiceProvider)[assembly: ComVisible(false)]

改成

asp访问c#创建的加密解密dll(封装DESCryptoServiceProvider)[assembly: ComVisible(true)]

  把[assembly:   AssemblyKeyFile("")]改成[assembly:   AssemblyKeyFile("..\\..\\myKey.snk   ")]   
    
  然后按Shift   +   Ctrl   +   B键生成dll库(使用Release模式),StringCrypt.dll。这时候,程序集就建立成功了。  

3.注册该程序集并创建一个类型库(在另外一台机器上:windows2003+framework2.0。注意这台机器是asp开放环境,没有安装vs2005,只安装了interDev6)

把“C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727”加入到系统环境变量path中。(右键点击“我的电脑-属性-高级-环境变量”在中系统环境变量中找到path,双击,在变量值的最前面加分号;然后再分号的前面,把上面的路径拷贝进去。确定。)

把上面生成的dll文件和pdb文件拷贝到这台机器上的一个目录中。打开cmd。把路径设置为该目录

 在cmd中,输入

asp访问c#创建的加密解密dll(封装DESCryptoServiceProvider)regasm StringCrypt.dll /tlb:StringCrypt.tlb /CodeBase

cmd提示:

Microsoft (R) .NET Framework Assembly Registration Utility 2.0.50727.42
Copyright (C) Microsoft Corporation 1998-2004.  All rights reserved.

Types registered successfully
Assembly exported to 'C:\StringCrypt\bin\Release\StringCrypt.tlb', and the type
library was registered successfully

然后再目录中产生了一个文件:StringCrypt.tlb

4.在asp页面中调用

asp内容:

asp访问c#创建的加密解密dll(封装DESCryptoServiceProvider)<%
asp访问c#创建的加密解密dll(封装DESCryptoServiceProvider)
set obj = Server.CreateObject("jonson.StringCrypt")
asp访问c#创建的加密解密dll(封装DESCryptoServiceProvider)Response.write(obj.Encrypt(
"ab","fk58Fgju"))
asp访问c#创建的加密解密dll(封装DESCryptoServiceProvider)Response.write(
"<br>")
asp访问c#创建的加密解密dll(封装DESCryptoServiceProvider)response.write(obj.Decrypt(
"B7C0E7C545D29078","fk58Fgju"))
asp访问c#创建的加密解密dll(封装DESCryptoServiceProvider)
%>

把该文件放到iis站点下面。

浏览即可,看到结果:

B7C0E7C545D29078
ab

更新dll:

由于需要扩展dll到功能,需要在dll中增加一些方法。如果重新到目标机器上注册,就需要把原来注册的dll注销。

asp访问c#创建的加密解密dll(封装DESCryptoServiceProvider)regasm /u StringCrypt.dll /tlb:StringCrypt.tlb /CodeBase

注销之后,就可以重新在注册新的dll。

相关文章:

  • 2021-10-01
  • 2022-12-23
  • 2021-09-02
  • 2022-01-29
  • 2021-07-09
  • 2021-11-16
  • 2022-12-23
猜你喜欢
  • 2021-11-20
  • 2022-12-23
  • 2022-02-21
  • 2022-12-23
  • 2021-07-14
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案