InstallShield调用.net的dll实现des加密功能

来源:互联网 发布:外汇黄金走势软件 编辑:程序博客网 时间:2024/05/22 05:01

环境:Installshield 12,win xp,Vs2005(用vs2010试了好几天出错,出现函数不可调用),在is 12中建了installshield installscript  msi工程


INSTALLSHIELD 中的主要代码:

prototype STRING DESInterface.DESEncrypt(STRING);  //这个是对dl中l函数的申明

function OnFirstUIAfter()
    STRING szTitle, szMsg1, szMsg2, szOption1, szOption2;
    NUMBER bOpt1, bOpt2; 
    STRING szFeatureName1,szFeatureName2,szFeatureName3;   
    STRING szfilename,szFolder; 
NUMBER nresult; 
STRING szmsg1,szmsg2; 
STRING xmlPath; 
STRING  szDLLName;   
OBJECT ObjInvokeDll; 
STRING strEncryptSERVER;
STRING strEncryptPWD; 
STRING TEMP;
begin

Disable(STATUSEX);
bOpt1  = FALSE;
bOpt2  = FALSE;
if(NeedWriteXml=TRUE) then 
xmlPath=INSTALLDIR^"\\ParamSet\\DataBaseInfo.xml";
   szDLLName = SUPPORTDIR ^ "DESInterface.dll";   
set ObjInvokeDll = CoCreateObjectDotNet(szDLLName, "DESInterface.DES");  //这个是加载dll对象
        TEMP= ObjInvokeDll.DESEncrypt(PWD)                                             //这个是调用函数

    endif;

end;


.net c#中的代码(用vs2005写的)

特别提醒要把工程中的Properties下的AssemblyInfo.cs中的[assembly: ComVisible(false)修改成[assembly: ComVisible(true)]]

using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;
namespace DESInterface //注意这个命名空间和is 的对应
{
    public class DES
    {
        public string keyString = "123456";//默认的密钥
        public string DESEncrypt(string toEncryptString)//注意参数的类型
        {
            using (DESCryptoServiceProvider des = new DESCryptoServiceProvider())
            {
                byte[] toEncryptBytes = Encoding.UTF8.GetBytes(toEncryptString);
                des.Key = ASCIIEncoding.ASCII.GetBytes(keyString);
                des.IV = ASCIIEncoding.ASCII.GetBytes(keyString);
                System.IO.MemoryStream ms = new System.IO.MemoryStream();
                using (CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write))
                {
                    cs.Write(toEncryptBytes, 0, toEncryptBytes.Length);
                    cs.FlushFinalBlock();
                    cs.Close();
                }
                string encryptedString = Convert.ToBase64String(ms.ToArray());
                ms.Close();
                return encryptedString;
            }
        }


        public string DESDecrypt(string toDecryptString)
        {
            byte[] toDecryptBytes = Convert.FromBase64String(toDecryptString);
            using (DESCryptoServiceProvider des = new DESCryptoServiceProvider())
            {
                des.Key = ASCIIEncoding.ASCII.GetBytes(keyString);
                des.IV = ASCIIEncoding.ASCII.GetBytes(keyString);
                System.IO.MemoryStream ms = new System.IO.MemoryStream();
                using (CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write))
                {
                    cs.Write(toDecryptBytes, 0, toDecryptBytes.Length);
                    cs.FlushFinalBlock();
                    cs.Close();
                }
                string decryptedString = Encoding.UTF8.GetString(ms.ToArray());
                ms.Close();
                return decryptedString;
            }
        } 
    }
}


如果还有问题一起探讨一下:qq 1009099161

原创粉丝点击