参考了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
using
System;
using
System.Runtime.InteropServices;
using
System.Security.Cryptography;
using
System.IO;
using
System.Text;

namespace
jonson

...
{
// 首先建立接口,这个是Com必须使用的
[Guid("E11358E9-4E6F-4c97-A7C6-AE1B725007BE")]
public interface IEncrypt

...{
string Encrypt(string pToEncrypt, string sKey);
string Decrypt(string pToDecrypt, string sKey);
}

//接口的实现
[Guid("FEED14C8-11D4-4f18-B115-77F826814F52")]
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组件所必须的。在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文件内
把
[assembly: ComVisible(
false
)]
改成
[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中,输入
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内容:
<%
set
obj
=
Server.CreateObject(
"
jonson.StringCrypt
"
)
Response.write(obj.Encrypt(
"
ab
"
,
"
fk58Fgju
"
))
Response.write(
"
<br>
"
)
response.write(obj.Decrypt(
"
B7C0E7C545D29078
"
,
"
fk58Fgju
"
))
%>
把该文件放到iis站点下面。
浏览即可,看到结果:
B7C0E7C545D29078
ab
更新dll:
由于需要扩展dll到功能,需要在dll中增加一些方法。如果重新到目标机器上注册,就需要把原来注册的dll注销。
regasm
/
u StringCrypt.dll
/
tlb:StringCrypt.tlb
/
CodeBase
注销之后,就可以重新在注册新的dll。