Generate a License key with VB.net

 

Hi, I need to develop a license generator in vb.net that allows me to create license keys that I will subsequently decode using the CryptEncode MQL function.

Anyone can help me ?

I used this VB.NET function to generate the license

 Public Function EncryptString(ByVal inputString As String) As String

        Dim memStream As MemoryStream = Nothing
        Try
            Dim byteInput As Byte() = Encoding.ASCII.GetBytes(inputString)
            Dim encryptKey As String = "4c85a221" ' MUST be 8 characters
            Dim key As Byte() = {}
            key = Encoding.ASCII.GetBytes(encryptKey)

            Dim provider As DESCryptoServiceProvider = New DESCryptoServiceProvider()
            memStream = New MemoryStream()
            Dim transform As ICryptoTransform = provider.CreateEncryptor(key, Nothing)
            Dim CryptoStream As CryptoStream = New CryptoStream(memStream, transform, CryptoStreamMode.Write)
            CryptoStream.Write(byteInput, 0, byteInput.Length)
            CryptoStream.FlushFinalBlock()

            Return (System.Text.ASCIIEncoding.ASCII.GetString(memStream.ToArray()))

        Catch ex As Exception
            Console.WriteLine(ex.Message)
            Return Nothing
        End Try

    End Function


But when I try to dectript with the following MQL function, I can not get any results and after many attempts I'm really in trouble !!!

Can anyone of you to help me or point me a way to solve my problem?

string ValidateLicense (string key){
      uchar dst[], seedArray[];
      uchar keyArray[];
      string seed="4c85a221";
   
      

      ArrayResize (keyArray, StringLen (key) / 2);
      for (int i = 0; i < StringLen (key); i+=2){
         string b = StringSubstr (key, i, 2); 
         keyArray[i/2] = (uchar)HexToInteger (b);
      }
     
     
      StringToCharArray(seed, seedArray);
      int res=CryptDecode(CRYPT_DES, keyArray, seedArray, dst);
      
   
         return( CharArrayToString (dst));
         
 }  
 

You should post the function "HexToInteger" code.

 
Leon Lam:

You should post the function "HexToInteger" code.

Oh!  you got it, I forgot about it, here it is...

Hope you can help

int HexToInteger(string str){
   int result=0;
   int power=0;
   for(int pos=StringLen(str)-1; pos>=0; pos--){
      int c=StringGetChar( str, pos);
      int value = 0; 
      if(c>='0' && c<='9')
         value=c-'0';
      else if(c>='a' && c<='f')
         value=c-'a'+10;   
      else if(c>='A' && c<='F')
         value=c-'A'+10;      
      result += value*MathPow(16.0, power);
      power++;   
   }   
   return(result);
}

me

 
yes
 
Riccardo Fornasiero:
Does anyone can help me about this problem ?
 

Strings in MQL are in UTF, your VB code uses ASCII encoding.
The first thing I'd check is that conversion between strings and byte arrays in MQL and VB contain the same values. Print out MQL uchar and VB byte arrays and compare.

Reason: