Translating VB6 dll call to C# -- Help!!!

S

shazrul

Hi,

This is my first time coding in C#. I am trying to implement a call to
a particular function from a dll I downloaded off the internet.

I have managed to do it fine in VB6. Unfortunately I'm struggling in
C#. I can't make the function work. I've tried so many things, i'm
completely at a lost.

------------- VB6 code --------------------

Here is how I've implemented it in VB6 (taken from example of the
author).

Private m_SourceBytes() As Byte
Private m_TargetBytes() As Byte
Private m_KeyBytes() As Byte
Private m_CertificateBytes() As Byte

Private m_SourceLength As Long
Private m_TargetLength As Long
Private m_KeyLength As Long
Private m_CertificateLength As Long

Private Declare Function RSA_encrypt Lib "rsa_dll.dll" _
(ByRef m_Source As Byte, ByVal m_Source_size As Long, _
ByRef m_Target As Byte, ByVal m_Target_size As Long, _
ByRef m_Certificate As Byte, ByVal m_Certificate_size As
Long) As Long

The call,

lResult = RSA_encrypt(m_SourceBytes(0), m_SourceLength,
m_TargetBytes(0), m_TargetLength, m_CertificateBytes(0),
m_CertificateLength)

---------------- C# code ------------------

In C#,

byte[] ba_secretText, ba_publicKey, ba_cipherText;
ba_cipherText = new byte[256];
ba_secretText = new byte[7];
ba_publicKey = new byte[418];

// RSA_Encrypt - Encrypts a string
[DllImport(@"C:\Apps\BatchAdminPassword\rsa_dll.dll")]
public extern static long RSA_encrypt(
[MarshalAs(UnmanagedType.LPArray)] byte[] m_Source,
long m_Source_size,
out IntPtr m_Target,
out long m_Target_size,
[MarshalAs(UnmanagedType.LPArray)] byte[] m_Certificate,
long m_Certificate_size
);

The call,

lRetVal = ShellCrypto.RSA_encrypt(ba_secretText,ba_secretText.GetLength(0),
out bufPtr, out lcipherText_len,
ba_publicKey,ba_publicKey.GetLength(0));


------------ dumpbin of the dll -------------

C:\Apps\BatchAdminPassword>dumpbin -exports rsa_dll.dll
Microsoft (R) COFF Binary File Dumper Version 6.00.8168
Copyright (C) Microsoft Corp 1992-1998. All rights reserved.


Dump of file rsa_dll.dll

File Type: DLL

Section contains the following exports for HMaxFRSA.dll

0 characteristics
3BF28825 time date stamp Wed Nov 14 23:05:09 2001
0.00 version
1 ordinal base
6 number of functions
6 number of names

ordinal hint RVA name

6 0 00001C10 HMaxF_RSA_version
3 1 00002190 RSA_decrypt
4 2 00002380 RSA_encrypt
5 3 000013D0 RSA_generate_keys
2 4 00001F30 RSA_sign
1 5 00001D40 RSA_verify

Summary

5000 .data
3000 .rdata
1000 .reloc
10000 .text

----- part of VB6 project commented out by original author -----

Don't know if this will help, but the author of the dll left the
following code commented out in VB6. It looks like the original C++
code.

'unsigned long __stdcall RSA_encrypt(
' unsigned char *source, unsigned long source_size,
' unsigned char *result, unsigned long result_size,
' unsigned char *pubkey_content, unsigned long
pubkey_content_size
' );


THanks in advance.
 
G

Guest

If you are simply trying to do encryption, .NET provides it's own
implementation for all common algorithms, and support to create your own
easily.

If that isn't going to work, send me the VB6 code DLL you are using and I
will write an interop for it.

shazrul said:
Hi,

This is my first time coding in C#. I am trying to implement a call to
a particular function from a dll I downloaded off the internet.

I have managed to do it fine in VB6. Unfortunately I'm struggling in
C#. I can't make the function work. I've tried so many things, i'm
completely at a lost.

------------- VB6 code --------------------

Here is how I've implemented it in VB6 (taken from example of the
author).

Private m_SourceBytes() As Byte
Private m_TargetBytes() As Byte
Private m_KeyBytes() As Byte
Private m_CertificateBytes() As Byte

Private m_SourceLength As Long
Private m_TargetLength As Long
Private m_KeyLength As Long
Private m_CertificateLength As Long

Private Declare Function RSA_encrypt Lib "rsa_dll.dll" _
(ByRef m_Source As Byte, ByVal m_Source_size As Long, _
ByRef m_Target As Byte, ByVal m_Target_size As Long, _
ByRef m_Certificate As Byte, ByVal m_Certificate_size As
Long) As Long

The call,

lResult = RSA_encrypt(m_SourceBytes(0), m_SourceLength,
m_TargetBytes(0), m_TargetLength, m_CertificateBytes(0),
m_CertificateLength)

---------------- C# code ------------------

In C#,

byte[] ba_secretText, ba_publicKey, ba_cipherText;
ba_cipherText = new byte[256];
ba_secretText = new byte[7];
ba_publicKey = new byte[418];

// RSA_Encrypt - Encrypts a string
[DllImport(@"C:\Apps\BatchAdminPassword\rsa_dll.dll")]
public extern static long RSA_encrypt(
[MarshalAs(UnmanagedType.LPArray)] byte[] m_Source,
long m_Source_size,
out IntPtr m_Target,
out long m_Target_size,
[MarshalAs(UnmanagedType.LPArray)] byte[] m_Certificate,
long m_Certificate_size
);

The call,

lRetVal = ShellCrypto.RSA_encrypt(ba_secretText,ba_secretText.GetLength(0),
out bufPtr, out lcipherText_len,
ba_publicKey,ba_publicKey.GetLength(0));


------------ dumpbin of the dll -------------

C:\Apps\BatchAdminPassword>dumpbin -exports rsa_dll.dll
Microsoft (R) COFF Binary File Dumper Version 6.00.8168
Copyright (C) Microsoft Corp 1992-1998. All rights reserved.


Dump of file rsa_dll.dll

File Type: DLL

Section contains the following exports for HMaxFRSA.dll

0 characteristics
3BF28825 time date stamp Wed Nov 14 23:05:09 2001
0.00 version
1 ordinal base
6 number of functions
6 number of names

ordinal hint RVA name

6 0 00001C10 HMaxF_RSA_version
3 1 00002190 RSA_decrypt
4 2 00002380 RSA_encrypt
5 3 000013D0 RSA_generate_keys
2 4 00001F30 RSA_sign
1 5 00001D40 RSA_verify

Summary

5000 .data
3000 .rdata
1000 .reloc
10000 .text

----- part of VB6 project commented out by original author -----

Don't know if this will help, but the author of the dll left the
following code commented out in VB6. It looks like the original C++
code.

'unsigned long __stdcall RSA_encrypt(
' unsigned char *source, unsigned long source_size,
' unsigned char *result, unsigned long result_size,
' unsigned char *pubkey_content, unsigned long
pubkey_content_size
' );


THanks in advance.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top