Link to home
Start Free TrialLog in
Avatar of ROBERT MECHLER
ROBERT MECHLERFlag for United States of America

asked on

Visual Basic 6: Code needed for TripleDES Encryption/Decryption

Below is code from VB.NET that does the same thing.
The VB6 application has 60 modules and I don't have the luxury of rewriting it. The VB6 program runs on Windows 10.
Upgrading first to Visual Studio VB.NET 8 does not work so that approach turned out to be a dead end.

Imports System.Security.Cryptography
Imports System.IO
Imports System.Text
Public Class Encrypt
    Private _Transform As ICryptoTransform
    Public Sub New()
        _Transform = Nothing
        Dim tdes As New TripleDESCryptoServiceProvider
        Dim bKey As Byte() = Convert.FromBase64String("knpQXScPNUl2ha0BanyJI2voWif8eSlp")
        Dim bIV As Byte() = Convert.FromBase64String("OTpHDm/9aak=")
        _Transform = tdes.CreateEncryptor(bKey, bIV)
    End Sub
    Public Function EncryptData(ByVal Data As String) As String
        Dim outStream As MemoryStream = New  MemoryStream
        Dim encStream As New CryptoStream(outStream, _Transform, CryptoStreamMode.Write)
        Dim DataArray As Byte() = Encoding.ASCII.GetBytes(Data)
        encStream.Write(DataArray, 0, DataArray.Length)
        encStream.Close()
        Return Convert.ToBase64String(outStream.ToArray)
    End Function
End Class

Open in new window

vb.net Decrypt example
Imports System.Security.Cryptography
Imports System.Text
Imports System.IO

Public Class Decrypt
    Private _Transform As ICryptoTransform
    Public Sub New()
        Dim tdes As New TripleDESCryptoServiceProvider
        _Transform = Nothing

        Dim bKey as Byte()=Convert.FromBase64String("knpQXScPNUl2ha0BanyJI2voWif8eSlp")
        Dim bIV as Byte()=Convert.FromBase64String("OTpHDm/9aak=")
        _Transform = tdes.CreateDecryptor(bKey,bIV)
    End Sub
    Public Function StrToByteArray(str As String) As Byte()
       Dim encoding As New System.Text.ASCIIEncoding()
       Return encoding.GetBytes(str)
    End Function 'StrToByteArray
    Public Function DecryptData(ByVal Data As String) As String
        Try
            Dim inStream As MemoryStream = New MemoryStream(Convert.FromBase64String(Data))
            Dim cryptoStream As cryptoStream = New cryptoStream(inStream, _Transform, CryptoStreamMode.Read)

            Dim outStream As MemoryStream = New MemoryStream
            Dim writer As StreamWriter = New StreamWriter(outStream)
            Dim reader As StreamReader = New StreamReader(cryptoStream)

            writer.Write(reader.ReadToEnd())

            reader.Close()
            writer.Close()
            cryptoStream.Close()
            Return Encoding.ASCII.GetString(outStream.ToArray())
        Catch ex As Exception
            Diagnostics.Trace.WriteLine(ex.ToString)
            Return Nothing
        End Try
    End Function
End Class

Open in new window

Avatar of Robberbaron (robr)
Robberbaron (robr)
Flag of Australia image

you need to find VB6 TripleDES library as can use .net libraries in vb6.

cant help much as i have upgraded all my VB6 apps to .net now and now longer have VB6 available.

http://www.freevbcode.com/ShowCode.asp?ID=3779  may be useful.
I've been used Ken Isaacs's library for a long time.  I've even contributed to the code source.
https://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=74645&lngWId=1
Avatar of ROBERT MECHLER

ASKER

None of the above would run on my Windows XP machine. Need to support Windows XP through Windows 10. All my other VB code works fine over that range of op systems.
None of the above would run on my Windows XP machine.
What kind of error message are you getting?
Crypto_API said Run-time error 429. Activex Component can't create object.
which of those two packages are you testing?
did you install all the components (modules and classes)?
which object was it trying to instantiate when it failed?
Crypto_API Demo.  v1.7  Compiled the Crypto-Api.vbp and all the other vbp in other sub-directories. Still same situation.

TripleDES doesn't seem to have a VB6 equivalent. It's in Visual Studio Architect 2003 where it first shows up from what I'm reading. I've got that code.

What I think I need to do now is see if I can create a Standard DLL in VB.NET  that VB6 can reference to do the call for encryption of the string.

The current VB.NET Solution I'm using is set up as 5 projects that all compile into DLL's for use in the bin folder of a website.

When I Declared the DLL from this VB.NET solution in VB6 and tried to run it, it comes back with the error that says it can't find the Dll's entry point.

It seems as though a .NET dll  is not the same as a "Standard DLL" aka C, C++ and VB6 since it doesn't have an entry point.

Obviously I've only dealt with standard DLL's useable by VB6, PowerBasic and the like.

I might have to withdraw the question until I can call a .NET DLL from Visual Basic in General.
ASKER CERTIFIED SOLUTION
Avatar of aikimark
aikimark
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I've never written a COM Dll before. I have no starting point to understand your answer.  I need to start with other questions that will give me the background to use a COM DLL written in Visual Studio 2003 and used by a VB6 program. The encryption routine code is in a Visual Studio 2003 VB.NET program.

I did get as far as creating a very simple msgbox function and compiled using a COM Class in Visual Studio 2003. I then referenced the TLB in a VB6 very simple form with a button that called the function in the COM Class. That worked fine.

When I tried to move the exe and DLL to another machine it errored out saying the ActiveX could not make the object. Very similar to the issue I encountered earilier. I'm assuming there is something additional I need to do, but don't actually know what that is yet.
I don't think you need to write the COM wrapper.  It has already been written for the system.cryptography assembly.  You just need to consume it from your VB6 application.
Through responses I began to understand what I needed to know and that I needed to start with simpler questions.
Mostly I have dealt with Desktop apps in VB6 and PowerBasic and VB.NET and C# based Web Sites.
Right, I just need to know how to move the COM DLL to another machine, so I can be sure it will work at a customer's site. The consuming part works fine on the machine both programs were compiled on. I've generated a new question that isn't as broad in scope.
Please post the link to your new question.