Old VB6 Encryption using c# to Decrypt

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
So we have a VB6 serialization library that we use to encrypt a string and save the string do a .dat file. We read this file for licensing. We are writing the libraries in c# now and I put all the same code in that vb6 uses but the results are different. The vb6 uses the advapi32.dll to do the following:
CryptAcquireContext
CryptReleaseContext
CryptCreateHash
CryptHashData
CryptDestroyHash
CryptEncrypt
CryptDecrypt
CryptDeriveKey
CryptDestroyKey
CryptGetProvParam
CryptGetUserKey
CryptGenKey
The Encrypt and Decrypt functions in vb6 are as follows:Private Function CryptoDecrypt(InputString As String, Password As String, ReturnString As String) As Boolean
*** START FAILSAFE *********
If FailSafe is enabled then Set up error handler and push stack
If Not fsDISABLED Then
On Error GoTo FailSafe_Error
fsPUSH "CSerialization2.cls", "Function CryptoDecrypt", "(" & "InputString=" & InputString & ", " & "Password=" & Password & ", " & "ReturnString=" & ReturnString & ")"
End If
*** STOP FAILSAFE **********

This function does a keyword decryption of a string using the RSA symetrical algorythm.

10 Dim nReturn As Long, iCounter As Integer
20 Dim lHCryptprov As Long, lHHash As Long, lHkey As Long
30 Dim sDecryptBuffer As String, sDecryptedText As String, sContainer As String, sProvider As String

40 sProvider = MS_DEF_PROV

50 nReturn = CryptAcquireContext(lHCryptprov, ByVal sContainer, ByVal sProvider, PROV_RSA_FULL, 0)
60 If nReturn = False Then
70 InitUser
80 nReturn = CryptAcquireContext(lHCryptprov, ByVal sContainer, ByVal sProvider, PROV_RSA_FULL, 0)
90 End If

100 If nReturn <> 0 Then
110 nReturn = CryptCreateHash(lHCryptprov, CALG_MD5, 0, 0, lHHash) create a hash
120 If nReturn <> 0 Then
130 nReturn = CryptHashData(lHHash, Password, Len(Password), 0) hash it out
140 If nReturn <> 0 Then
150 nReturn = CryptDeriveKey(lHCryptprov, ENCRYPT_ALGORITHM, lHHash, 0, lHkey) make a key with the hash
160 If nReturn <> 0 Then
170 For iCounter = 1 To Len(InputString) Step 255
180 sDecryptBuffer = Mid(InputString, iCounter, 255)
190 If CBool(CryptDecrypt(lHkey, 0, 1, 0, sDecryptBuffer, Len(sDecryptBuffer))) Then
200 sDecryptedText = sDecryptedText & sDecryptBuffer
210 CryptoDecrypt = True
220 Else
230 CryptoDecrypt = False
240 Exit For
250 End If
260 Next iCounter
270 ReturnString = sDecryptedText
280 End If
290 End If
300 End If

310 If lHHash <> 0 Then Call CryptDestroyHash(lHHash) destroy hash
320 If lHkey <> 0 Then Call CryptDestroyKey(lHkey) destroy the key
330 If lHCryptprov <> 0 Then Call CryptReleaseContext(lHCryptprov, 0) release the context

340 End If

*** START FAILSAFE **********
FailSafe_Exit:
If FailSafe is enabled then pop stack
If Not fsDISABLED Then fsPOP "CSerialization2.cls", "Function CryptoDecrypt", "(" & "InputString=" & InputString & ", " & "Password=" & Password & ", " & "ReturnString=" & ReturnString & ")"
Exit Function
FailSafe_Error:
Select Case Err
Case ... add code for errors you want to handle here
Case Else default is to let FailSafe handle errors
Select Case fsERROR("CSerialization2.cls", "Function CryptoDecrypt") returns 1 to 5
Case fsResume: Resume try the line again
Case fsResumeNext: Resume Next move to the next line
Case 4: End invalid in DLLs or controls, un-REM to enable
Case 5: Stop: Resume stop here, F8 moves to error line
End Select
End Select
Resume FailSafe_Exit default is to exit this procedure
*** STOP FAILSAFE **********
End Function

Private Function CryptoEncrypt(InputString As String, Password As String, ReturnString As String) As Boolean
*** START FAILSAFE *********
If FailSafe is enabled then Set up error handler and push stack
If Not fsDISABLED Then
On Error GoTo FailSafe_Error
fsPUSH "CSerialization2.cls", "Function CryptoEncrypt", "(" & "InputString=" & InputString & ", " & "Password=" & Password & ", " & "ReturnString=" & ReturnString & ")"
End If
*** STOP FAILSAFE **********

This function does a keyword encryption of a string using the RSA symetrical algorythm.

10 Dim nReturn As Long, iCounter As Integer
20 Dim lHCryptprov As Long, lHHash As Long, lHkey As Long
30 Dim sEncryptBuffer As String, sEncryptedText As String, sContainer As String, sProvider As String

40 sProvider = MS_DEF_PROV

50 nReturn = CryptAcquireContext(lHCryptprov, ByVal sContainer, ByVal sProvider, PROV_RSA_FULL, 0)
60 If nReturn = False Then
70 InitUser
80 nReturn = CryptAcquireContext(lHCryptprov, ByVal sContainer, ByVal sProvider, PROV_RSA_FULL, 0)
90 End If

100 If nReturn <> 0 Then
110 nReturn = CryptCreateHash(lHCryptprov, CALG_MD5, 0, 0, lHHash) create a hash
120 If nReturn <> 0 Then
130 nReturn = CryptHashData(lHHash, Password, Len(Password), 0) hash it out
140 If nReturn <> 0 Then
150 nReturn = CryptDeriveKey(lHCryptprov, ENCRYPT_ALGORITHM, lHHash, 0, lHkey) make a key with the hash
160 If nReturn <> 0 Then
170 For iCounter = 1 To Len(InputString) Step 255
180 sEncryptBuffer = Mid(InputString, iCounter, 255)
190 If CBool(CryptEncrypt(lHkey, 0, 1, 0, sEncryptBuffer, Len(sEncryptBuffer), (Len(sEncryptBuffer) * 2))) Then
200 sEncryptedText = sEncryptedText & sEncryptBuffer
210 CryptoEncrypt = True
220 Else
230 CryptoEncrypt = False
240 Exit For
250 End If
260 Next iCounter
270 ReturnString = sEncryptedText
280 End If
290 End If
300 End If

310 If lHHash <> 0 Then Call CryptDestroyHash(lHHash) destroy hash
320 If lHkey <> 0 Then Call CryptDestroyKey(lHkey) destroy the key
330 If lHCryptprov <> 0 Then Call CryptReleaseContext(lHCryptprov, 0) release the context

340 End If

*** START FAILSAFE **********
FailSafe_Exit:
If FailSafe is enabled then pop stack
If Not fsDISABLED Then fsPOP "CSerialization2.cls", "Function CryptoEncrypt", "(" & "InputString=" & InputString & ", " & "Password=" & Password & ", " & "ReturnString=" & ReturnString & ")"
Exit Function
FailSafe_Error:
Select Case Err
Case ... add code for errors you want to handle here
Case Else default is to let FailSafe handle errors
Select Case fsERROR("CSerialization2.cls", "Function CryptoEncrypt") returns 1 to 5
Case fsResume: Resume try the line again
Case fsResumeNext: Resume Next move to the next line
Case 4: End invalid in DLLs or controls, un-REM to enable
Case 5: Stop: Resume stop here, F8 moves to error line
End Select
End Select
Resume FailSafe_Exit default is to exit this procedure
*** STOP FAILSAFE **********
End Function
Now I am using the same dll and the same functions to do the same in c# /// <summary>
///
/// </summary>
/// <param name="InputString </param>
/// <param name="Password </param>
/// <param name="ReturnString </param>
/// <returns></returns>
private bool CryptoDecrypt(string InputString)
{
IntPtr Prov = new IntPtr(0);
IntPtr Key = new IntPtr(0);
IntPtr Hash = new IntPtr(0);
string DecryptBuffer = string.Empty, DecryptedText = string.Empty, Container = string.Empty;
bool ReturnValue = false;

bool Return = CryptAcquireContext(out Prov, Container, Constants.CSPNAME, Constants.PROV_RSA_FULL, 0);

if (!Return)
{
//if (Marshal.GetLastWin32Error() == Constants.NTE_BAD_KEYSET)
//{
//}
//else
//{
// return false;
//}
}

// Create the hash object.
if (!CryptCreateHash(Prov, ALG_ID.CALG_MD5, IntPtr.Zero, 0, out Hash))
{
return false;
}

// Has it out.
if (!CryptHashData(Hash, m_EncrytionKey, m_EncrytionKey.Length, 0))
{
return false;
}

// Make a key with the hash
if (!CryptDeriveKey(Prov, ALG_ID.CALG_RC4, Hash, 0, ref Key))
{
return false;
}



for (int i = 0; i < InputString.Length; i += 255)
{
string Portion = InputString.Substring(i, 255);
//byte[] Data = Encoding.Default.GetBytes(Portion);
int Length = Portion.Length;


if (CryptDecrypt(Key, IntPtr.Zero, true, 0, Portion, ref Length))
{

DecryptedText += Portion;
ReturnValue = true;
}
else
{
int Error = Marshal.GetLastWin32Error();
return false;
}
}

m_Serialization = DecryptedText;

return ReturnValue;
[DllImport("advapi32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool CryptAcquireContext(out IntPtr phProv, string pszContainer, string pszProvider, uint dwProvType, uint dwFlags);
[DllImport("advapi32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool CryptCreateHash(IntPtr hProv, ALG_ID Algid, IntPtr hKey, uint dwFlags, out IntPtr phHash);
[DllImport("advapi32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool CryptHashData(IntPtr hHash, string pbData, int dwDataLen, uint dwFlags);
[DllImport("advapi32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool CryptDeriveKey(IntPtr hProv, ALG_ID Algid, IntPtr hBaseData, uint dwFlags, ref IntPtr phKey);
[DllImport("advapi32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool CryptDestroyHash(IntPtr hHash);
[DllImport("advapi32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool CryptDecrypt(IntPtr hKey, IntPtr hHash, [MarshalAs(UnmanagedType.Bool)]bool Final, uint dwFlags, string pbData, ref int pdwDataLen);
[DllImport("advapi32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool CryptDestroyKey(IntPtr hKey);
[DllImport("advapi32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool CryptReleaseContext(IntPtr hProv, uint dwFlags);When I read the file that was generated in vb6 and try to decrypt it, it does not get the same results as the vb6 application. Any help would be appreciated

View the full article
 
Back
Top