1.首先在vs2008中,新建项目选择c#项目->类库

  编写一下代码

using System;
using System.IO;
using System.Text;
using System.Security.Cryptography;
using System.Runtime.InteropServices;

namespace JCCT.Security
{
    /*应用于VB6的dll调用*/
   
        //首先建立接口,这个是Com必须使用的
    [ComVisible(true)]
    [Guid("4B6FD86C-B096-4e6a-AC7F-442A51FA0776")]
        public interface IEncrypt
        {
            string AESEncrypt(string plainStr);
            string AESDecrypt(string encryptStr);
        }

        //写接口的实现
    [ComVisible(true)]
    [Guid("367BF149-DBB3-4184-9A5C-978CC8C10787")]
    [ProgId("ClassLibrary1.AES")]
        public class AES : IEncrypt
        {

            /// <summary>
            /// 获取密钥
            /// </summary>
            private static string Key
            {
                get { return @"HJ(S<?l78242^&WL%#jCD8dkiI39(euw"; }
            }

            /// <summary>
            /// 获取向量
            /// </summary>
            private static string IV
            {
                get { return @"mailto:L@uDe4Kf&amp;amp;amp;amp;amp;#^j8E53"; }
            }

            /// <summary>
            /// AES加密
            /// </summary>
            /// <param name="plainStr">明文字符串</param>
            /// <returns>密文</returns>
            public string AESEncrypt(string plainStr)
            {
                byte[] bKey = Encoding.UTF8.GetBytes(Key);
                byte[] bIV = Encoding.UTF8.GetBytes(IV);
                byte[] byteArray = Encoding.UTF8.GetBytes(plainStr);

                string encrypt = null;
                Rijndael aes = Rijndael.Create();
                try
                {
                    using (MemoryStream mStream = new MemoryStream())
                    {
                        using (CryptoStream cStream = new CryptoStream(mStream, aes.CreateEncryptor(bKey, bIV), CryptoStreamMode.Write))
                        {
                            cStream.Write(byteArray, 0, byteArray.Length);
                            cStream.FlushFinalBlock();
                            encrypt = Convert.ToBase64String(mStream.ToArray());
                        }
                    }
                }
                catch { }
                aes.Clear();

                return encrypt;
            }

            /// <summary>
            /// AES解密
            /// </summary>
            /// <param name="encryptStr">密文字符串</param>
            /// <returns>明文</returns>
            public string AESDecrypt(string encryptStr)
            {
                string decrypt = null;
                Rijndael aes = Rijndael.Create();
                try
                {
                    byte[] bKey = Encoding.UTF8.GetBytes(Key);
                    byte[] bIV = Encoding.UTF8.GetBytes(IV);
                    byte[] byteArray = Convert.FromBase64String(encryptStr);
                    using (MemoryStream mStream = new MemoryStream())
                    {
                        using (CryptoStream cStream = new CryptoStream(mStream, aes.CreateDecryptor(bKey, bIV), CryptoStreamMode.Write))
                        {
                            cStream.Write(byteArray, 0, byteArray.Length);
                            cStream.FlushFinalBlock();
                            decrypt = Encoding.UTF8.GetString(mStream.ToArray());
                        }
                    }
                }
                catch
                {
                    decrypt = encryptStr;
                }
                aes.Clear();

                return decrypt;
            }
        }
        
}

2.在文件AssemblyInfo.cs中,设置[assembly: ComVisible(true)],默认是false的,需要设置成true

 3.选择项目属性,在生成标签卡中,勾选为com互操作注册.

 4.生成项目dll文件。

  Guid生成:在工具->创建Guid生成,格式选择第四种,点复制之后需要把两边的{}除掉.

 5.注册.regasm ClassLibrary1.dll /tlb:ClassLibrary1.tlb
如果点项目右键属性:





可以编译后自动生成tlb,不需要再用vs.net命令生成。注意要以管理员身份运行vs.net.

 

 6.在vb6 中引用ClassLibrary1.tlb文件,注意不是ClassLibrary1.dll文件

引用的话,在vb6编写时可以看到ClassLibrary1的类的方法。

    Dim obj As Object
    Set obj = CreateObject("ClassLibrary1.AES")
    Form1.Text2.Text = obj.AESDecrypt(Form1.Text1.Text) 

当用VB6生成的应用程序,拷贝到其它机上,又不能运行了,提示:ACTIVEX 不能创建对象。后终于找到解决办法:
用命令:regasm path +ClassLibrary.dll /tlb:ClassLibrary.tlb /codebase



如果用vb6生成dll文件在c#访问方法:
方法很简单 只需要生成dll, 然后在C#中引用就行了。