Encrypt in VB6, decrypt in VB.NET? (and vice versa)

I use DES Encrypt and Decrypt routines in VB.Net2003, using System.Security.Cryptography - something like:


    Public Shared Function DESEncrypt(ByVal strKey As String, ByVal strData As String) As String
        Dim strValue As String = ""
        If strKey <> "" Then
            ' convert key to 16 characters for simplicity
            Select Case Len(strKey)
                Case Is < 16
                    strKey = strKey & Left("XXXXXXXXXXXXXXXX", 16 - Len(strKey))
                Case Is > 16
                    strKey = Left(strKey, 16)
            End Select
            ' create encryption keys
            Dim byteKey() As Byte = Encoding.UTF8.GetBytes(Left(strKey, 8))
            Dim byteVector() As Byte = Encoding.UTF8.GetBytes(Right(strKey, 8))
            ' convert data to byte array
            Dim byteData() As Byte = Encoding.UTF8.GetBytes(strData)
            ' encrypt
            Dim objDES As New DESCryptoServiceProvider
            Dim objMemoryStream As New MemoryStream
            Dim objCryptoStream As New CryptoStream(objMemoryStream, objDES.CreateEncryptor(byteKey, byteVector), CryptoStreamMode.Write)
            objCryptoStream.Write(byteData, 0, byteData.Length)
            objCryptoStream.FlushFinalBlock()
            ' convert to string and Base64 encode
            strValue = Convert.ToBase64String(objMemoryStream.ToArray())
        Else
            strValue = strData
        End If
        Return strValue
    End Function


 



I'd like to be able use the same routines from VB6 code, so that I can encrypt/decrypt strings in legacy apps that share data with my VB.Net app.
Is this possible using the same routines or are there compatible classes I can reference in VB6 somewhere



Answer this question

Encrypt in VB6, decrypt in VB.NET? (and vice versa)

  • THHNO

    Oh, OK.  In that case, if it was me, I'd build a COM dll in C++ to distribute with both apps.  Or VB6, if you don't know C++, but there's a lot of encryption code out there in C++ you could use.  It's better to call the framework methods, and failing that, look for some API methods you could call, but I'd certainly prefer both apps to call the exact same routine if it was me, just to feel sure it was always going to return the same results.


  • CHTHONIC

    Thanks Kelly.

    I will check it out.

    Duane


  • GianniAb

    I have the same issue (encrypt in vb.net and now I need to decrypt in vb6).  I am trying to make my cryptography project from vb.net into a com dll and use it in vb6.  When I include the dll in VB6, the methods are not accessible or visible.  I checked the "register for com interop" checkbox  and set my public functions for <comvisible(true)>.  Any ideas as to why this isn't working

  • chaser7016

    I have had the same problem before while attempting to export any shared function to COM.  All the other functions are there, but none of the ones marked as "shared".

    If anyone knows how to get shared functions to be visible when working with COM-interop from .NET, I would love to know!

  • kenc2005

    Hello,

    If you don't mind going the 3rd party route, you could try my VB6 dll, VBCorLib. It supports all of the encryption routines mentioned and is compatible with the .NET versions. It's on Source Forge if you wish to try it. http://sourceforge.net/projects/vbcorlib/

    Kelly


  • RonDesta

    Why not write a COM dll in VB.NET and call it from VB6 It seems the only way to be sure the exact same functions are called.

    I would then call the same DLL from both places, so that your code has a common point of call.

  • InvertedAerials

    I am running into the same requirement.

    I need to be able to use CryptoAPI in a VB6 application that produces the same encrypted output that a VB.NET application does using the TripleDESCryptoServiceProvider.

    Seems like a lot of folks would like to be able to do this. Are there any good simple examples of this If you have any, please post or provide a link where they can be found.

    I like the poster above would like to use the CryptoAPI on the VB6 side as opposed to interop with the .NET DLL I created if possible.

    Thanks


  • Discoman

    Personally I would use the System.Security.Cryptography namespace in VB.NET as you're doing and then use the CryptoAPI from my VB6 code or use a 3rd-party encryption package that hopefully wraps up the CryptoAPI to make it more approachable. (The CryptoAPI is fairly arcane, IMHO.)

    BTW - I wouldn't store anything too sensitive using DES as it is fairly easily cracked using today's computing power. Triple-DES (aka 3DES) is much more secure. Even better algorithms would be Rijndael (pronounced rain-doll), AES or Blowfish.

    A good intro to .NET cryptography is:
    http://msdn.microsoft.com/msdnmag/issues/02/06/Crypto/default.aspx

    You can find a .NET implementation of AES in this article:
    http://msdn.microsoft.com/msdnmag/issues/03/11/AES/

    You can find info on Blowfish here:
    http://www.schneier.com/blowfish-products.html


  • Chandra B

    Yeah that what I thought I'd have to do. But it's not my desirable solution as it means a machine that runs the vb6 app needs to have the dotnet framework installed, which I can't neccesarily guarentee will be there. The DotNet version of the code isn't (neccesarily) run on the same box. I thought VB6 might have a reference or some built in routine for access DES encryption through the system somehow.
  • Encrypt in VB6, decrypt in VB.NET? (and vice versa)