I saw this nifty article which describes how to add the .NET framwork itself
as a reference to your VB6 project. (Guys don't jump on me, this is a VB6,
not a .NET question - please see code below.)
Anyway, I'm trying to use the .NET framework to do 3DES encryption inside my
VB6 application, but I'm running into a couple of snags: (BTW, this function
works properly in the equivalent .NET application, but I need to duplicate
this in VB6 since we're providing both VB6 and .NET clients.)
First of all, this statement doesn't compile:
zEncrypt = Convert.ToBase64String(DESEncrypt.TransformFinalBlock(Buffer,
0, UBound(Buffer)))
The error is Function or interface marked as restricted, or the function
uses an automation type not supported in Visual Basic. The method parameters
are byte[], int and int so I'm not sure what the problem is.
The second problem is that this statement does something really funky at
runtime: (I compiled by commenting out the offending statement above.) At
runtime (in the IDE) this statement simply exits the function. No Err is
tripped, just bingo, back to the next statement in the calling function.
DES.Key = hashMD5.ComputeHash(ASCIIEnc.ASCII.GetBytes("12345678"))
If you have a solution or if you can propose a better way for me to do 3DES
encryption in VB6, that will help me immensely.
Thanks!
- Joe Geretz -
Private Function zEncrypt(ByVal Plaintext As String) As String
Dim DES As TripleDESCryptoServiceProvider
Dim hashMD5 As MD5CryptoServiceProvider
Dim DESEncrypt As ICryptoTransform
Dim Buffer() As Byte
Dim ASCIIEnc As ASCIIEncoding
Set DES = New TripleDESCryptoServiceProvider
Set hashMD5 = New MD5CryptoServiceProvider
Set ASCIIEnc = New ASCIIEncoding
Set DES.Key = hashMD5.ComputeHash(ASCIIEnc.ASCII.GetBytes("12345678"))
DES.Mode = CipherMode_ECB
Set DESEncrypt = DES.CreateEncryptor
Buffer = ASCIIEnc.ASCII.GetBytes(Plaintext)
zEncrypt = Convert.ToBase64String(DESEncrypt.TransformFinalBlock(Buffer,
0, UBound(Buffer)))
End Function
Ken Halter - 14 Sep 2005 23:10 GMT
>I saw this nifty article which describes how to add the .NET framwork
>itself as a reference to your VB6 project. (Guys don't jump on me, this is
>a VB6, not a .NET question - please see code below.)
Of course, there's going to be some testing to make sure the .Net code's
returning the correct encrypted strings (notice I said "the .Net code" <g>)
EzCryptoEngineOCX
http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=50290&lngWId=1
EzCryptoAPI. Encrypt/Decrypt with 3DES112,3DES,DES,RC2, SHA,MD5,and RC4!
http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=26955&lngWId=1

Signature
Ken Halter - MS-MVP-VB - http://www.vbsight.com
DLL Hell problems? Try ComGuard - http://www.vbsight.com/ComGuard.htm
Please keep all discussions in the groups..
Michael C - 15 Sep 2005 01:19 GMT
>I saw this nifty article which describes how to add the .NET framwork
>itself as a reference to your VB6 project.
So how do you do that?
Michael
Joseph Geretz - 15 Sep 2005 05:15 GMT
I'm sorry. I forgot to include the link.
http://msdn.microsoft.com/vbrun/vbfusion/usingnet/default.aspx
It's not as easy as it sounds though. There's practically no Intellisense
and classes need to be explicitly instantiated even if you just want to call
static function.
(Plus you get the odd quirky behavior which I'm seeing. I can see merit on
either side of the issue; remain with VB6 or migrate toward .NET. But I
can't say that my first glimpse of framework access from VB6 is very
favorable.)
- Joe Geretz -
>>I saw this nifty article which describes how to add the .NET framwork
>>itself as a reference to your VB6 project.
>
> So how do you do that?
>
> Michael
Joseph Geretz - 15 Sep 2005 05:20 GMT
Hmm, now that I'm browsing, there's another fusion link which ostensibly
deals with accessing framework encryption from VB6. I'll give this a shot
tomorrow and report back.
http://msdn.microsoft.com/vbrun/vbfusion/smallandunreadable1/
- Joe Geretz -
> I'm sorry. I forgot to include the link.
>
[quoted text clipped - 17 lines]
>>
>> Michael
Joseph Geretz - 16 Sep 2005 01:07 GMT
This 'fusion' approach is bogus.
A much better approach is to implement the function as a .NET assembly and
use interop to access it from VB6.
- Joe Geretz -
> Hmm, now that I'm browsing, there's another fusion link which ostensibly
> deals with accessing framework encryption from VB6. I'll give this a shot
[quoted text clipped - 25 lines]
>>>
>>> Michael
Michael C - 19 Sep 2005 05:37 GMT
> This 'fusion' approach is bogus.
>
> A much better approach is to implement the function as a .NET assembly and
> use interop to access it from VB6.
After looking at it I agree, although a much better approach is to write the
whole thing in c# :-)
Michael
Joseph Geretz - 19 Sep 2005 16:42 GMT
> After looking at it I agree, although a much better approach is to write
> the whole thing in c# :-)
I guess, for a post-2002 project. The software I'm dealing with predates
.NET for some years, so any foray into .NET will involve some degree of
interop, at least in the initial migration stages.
- Joe Geretz -
>> This 'fusion' approach is bogus.
>>
[quoted text clipped - 5 lines]
>
> Michael
Michael C - 20 Sep 2005 04:52 GMT
> I guess, for a post-2002 project. The software I'm dealing with predates
> .NET for some years, so any foray into .NET will involve some degree of
> interop, at least in the initial migration stages.
I'm in the same boat but have been avoiding a hybrid because I can't see how
it would work well with an MDI app.
Michael
Joseph Geretz - 20 Sep 2005 18:48 GMT
Hi Michael,
Setting the user interface aside, you could proceed to convert the business
processing / data access portions of your application first and leave the UI
for last. Of course you'll be in a better position to proceed if your
lower-level processing is presently abstracted from your forms into separate
class libraries.
- Joe Geretz -
>> I guess, for a post-2002 project. The software I'm dealing with predates
>> .NET for some years, so any foray into .NET will involve some degree of
[quoted text clipped - 4 lines]
>
> Michael