2
Answers

COM Interop problem

Ask a question
Richard

Richard

15y
4.3k
1

I am a newbie to .NET (using 3.5) however have written a C# Class library that performs some decryption and need to be able to reference this class from Visual Basic 6.0.

I have attempted to do this using the following steps:

1. Set the assembly to "COM-Visible" from the project properties
2. Set "register for COM Interop" to tre in the "BUILD" properites of the project
3. Signed the assembly with a strong name
4. Built the assembly
5. Built the type library using the tlbexp tool
6. Registered the assembly using regasm tool
7. Registered the assembly in the GAC using gacutil

Although i need this to ultimately be called from VB6, our .NET Dev environment does not have VB6 so I am attempting to test the above using vbscript and receiving the following error "Active x component can't create object".

I have tried ensuring that the assembly and type library are present in the same directory as the wscript.exe file (although my understanding is that is not neccessary once the assembly has been installed in the GAC)

Any help would be much appreciated.

 


C# class:

 

using System;
using System.Security.Cryptography; //decryption class
using System.IO;    //for use of memory stream

namespace RBKDecrypt
{
    interface IDecrypt
    {
        string Key { set; }
        string EncString { set; }
        string DecryptString();
    }


    public class Decrypt:IDecrypt
    {

        private string sKey;
        private string sEncString;


        public Decrypt()
        {
            //default constructor needed to access from COM
        }

        public string Key
        {
            set{sKey = value;}
        }
        public string EncString
        {
            set { sEncString = value; }
        }

 


        public string DecryptString()
        {

            // Create the Crypto Service Provider
            DESCryptoServiceProvider desCrypto = new DESCryptoServiceProvider();

            // Set both the Key and the initialization vector to the key bytes
            desCrypto.Key = HexStringToByteArray(sKey);
            desCrypto.IV = HexStringToByteArray(sKey);

            // Decrypt the code
            return DESDecrypt(HexStringToByteArray(sEncString), desCrypto);
        }

 

        // Converts a HEX string to a byte array
        public static byte[] HexStringToByteArray(String hex)
        {
            int NumberChars = hex.Length;
            byte[] bytes = new byte[NumberChars / 2];
            for (int i = 0; i < NumberChars; i += 2)
                bytes[i / 2] = Convert.ToByte(hex.Substring(i, 2), 16);
            return bytes;
        }

        // Decrypt the byte array
        public static string DESDecrypt(byte[] CypherText, SymmetricAlgorithm symmAlgorithm)
        {
            // Create a memory stream to the passed buffer.
            MemoryStream ms = new MemoryStream(CypherText);

            // Create a CryptoStream using the memory stream and the CSP DES key.
            CryptoStream encStream = new CryptoStream(ms, symmAlgorithm.CreateDecryptor(), CryptoStreamMode.Read);

            // Create a StreamReader for reading the stream.
            StreamReader sr = new StreamReader(encStream);

            // Read the stream as a string.
            string val = sr.ReadLine();

            // Close the streams.
            sr.Close();
            encStream.Close();
            ms.Close();

            return val;
        }
    }
}

VBScript:

Dim obj
Set obj = CreateObject("RBKDecrypt.Decrypt")
obj.Key="A818163DD5E0DE87"
obj.EncString="FDA54C51E4BA3B0CB281CAF398F845BE"
msgbox obj.DecryptString()
Set obj = Nothing

 


Answers (2)