How to convert windev function code to C#

132 views Asked by At

I want to convert a function of a windev program to C# .net core 6 web api project controller function.

Example code:

sLic is string ANSI = sAPPLICENSEID+";"+SAI_License_Number;
sRes is string ANSI;
sLic = License_Crypt_String(sLic);
sRes = License_Check(sLic);
IF sRes="E100" OR sRes = "E101" OR sRes="E102" OR sRes="E103" THEN
gstLicense.bIsValid=False;
Info("Erreur "+sRes);
ELSE
sRes = Replace(sRes,CR,"");
gstLicense = License_Decrypt(sRes);
SAI_License_Owner = gstLicense.sOwner;
SAI_LimitDate = gstLicense.dLimit;
SAI_NumberOfUsers = gstLicense.nNumberOfUsers;
INIWrite("<LS>","<info>",sRes,fDataDirUser()+"\login.ini");
Database_Change_Connexion();
IF NOT InTestMode THEN
Check_DB();
END
// Info("Success : description = "+gstLicense.sDescription);
END
2

There are 2 answers

0
Dinushe Jayasekera On BEST ANSWER

Finally I was able to convert the code.

public List<object> ValidateLicense(string SAI_License_Number)
{
    List<object> serialInfor = new List<object>();

    Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
    Encoding wind1252 = Encoding.GetEncoding(1252);
    //byte[] wind1252bytes = wind1252.GetBytes(SAI_License_Number);

    string validity = string.Empty;

    string sAPPLICENSEID = "some code";
    //byte[] wind1252bytessAPPLICENSEID = wind1252.GetBytes(sAPPLICENSEID);

    string sLic = sAPPLICENSEID + ";" + SAI_License_Number;

    //byte[] semi = wind1252.GetBytes(";");
    //byte[] sLic2 = wind1252bytessAPPLICENSEID.Concat(semi).ToArray(); // concat with semi
    //byte[] sLic3 = sLic2.Concat(wind1252bytes).ToArray();

    //byte[] wind1252sLic = wind1252.GetBytes(sLic);
    //byte[] wind1252sLic = wind1252.GetBytes(sLic3);

    byte[] encryptedsLic = null;
    encryptedsLic = License_Crypt_String(sLic);
    sLic = Convert.ToBase64String(encryptedsLic);
    License_CheckRequest req = new License_CheckRequest();
    req.sLic = sLic;
    TP_Licensing_WSSOAPPortTypeClient client = new TP_Licensing_WSSOAPPortTypeClient();
    client.Open();
    var result = client.License_Check(req);
    client.Close();

    if(result.License_CheckResult.Contains("E100") || result.License_CheckResult.Contains("E102") || result.License_CheckResult.Contains("E103"))
    {
        serialInfor.Add(result.License_CheckResult);
        return serialInfor;
    }
    else
    {
        sLic = result.License_CheckResult;
        sLic = sLic.Trim();
        sLic = sLic.Replace(" ", "");
        sLic = License_Decrypt_String(sLic);
        SerialInfor info = new SerialInfor();
        info = serial_data_extractor(sLic);
        serialInfor.Add(info);
        return serialInfor;
    }

    //return "invalid key";
}

and encryption code as below

private byte[] License_Crypt_String(string str) 
{
    Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
    Encoding wind1252 = Encoding.GetEncoding(1252);

    byte[] bufEncryptedResult;
    byte[] bufACrypter = wind1252.GetBytes(str); 
    byte[] bufCléMD5;
    byte[] bufClé = wind1252.GetBytes("some code here"); // encryption key for 3DES


    // encryption key converted in to MD5 hash

    using (MD5 md5 = MD5.Create())
    {
        bufCléMD5 = md5.ComputeHash(bufClé);
    }

    //string testString = wind1252.GetString(bufCléMD5).Trim(); //for testing converted MD5 code

    byte[] first3bytes = bufCléMD5.Take(8).ToArray(); // first 8 bytes of MD5 hash byte array
    bufCléMD5 = bufCléMD5.Concat(first3bytes).ToArray(); // join first 8 bytes with original hash byte array key

    var MyTripleDESCryptoService  = new TripleDESCryptoServiceProvider();

    MyTripleDESCryptoService.Key = bufCléMD5;

    MyTripleDESCryptoService.Mode = CipherMode.ECB;

    MyTripleDESCryptoService.Padding = PaddingMode.PKCS7;

    var MyCrytpoTransform = MyTripleDESCryptoService
       .CreateEncryptor();

    bufEncryptedResult = MyCrytpoTransform
       .TransformFinalBlock(bufACrypter, 0,
       bufACrypter.Length);

    MyTripleDESCryptoService.Clear();

    return bufEncryptedResult;
}

Hope this will help someone in future

2
Brando Zhang On

A lot of methods, asp.net core will not work inside the asp.net core. If you still want to use them, you should find the method by yourself.

Codes like this view model:

   public class LicenseModel
  {
      public string ApplLicenseId { get; set; }
      public string LicenseNumber { get; set; }
     


  }

Controller:

  [HttpPost]
  public IActionResult ValidateLicense([FromBody] LicenseModel licenseModel)
  {
     
          string sLic = $"{licenseModel.ApplLicenseId};{licenseModel.LicenseNumber}";
          sLic = LicenseCryptString(sLic);
          string sRes = LicenseCheck(sLic);

          if (sRes == "E100" || sRes == "E101" || sRes == "E102" || sRes == "E103")
          {
              gstLicense.bIsValid = false;
              // here you could set hte logger to add the notification
          }

          sRes = sRes.Replace(Environment.NewLine, "");

        
          gstLicense = LicenseDecrypt(sRes);

          SAI_License_Owner = gstLicense.sOwner;
          SAI_LimitDate = gstLicense.dLimit;
          SAI_NumberOfUsers = gstLicense.nNumberOfUsers;


           // you need implement the INIWrite method by yourself
          INIWrite("<LS>", "<info>", sRes, Path.Combine(fDataDirUser(), "login.ini"));

          //I don't know what's the same method inside the asp.net core, you need find it by yourself
          Database_Change_Connexion();
          if (!InTestMode)
          {
              Check_DB();
          }

          return Ok("Success : description = " + gstLicense.sDescription);
       
  }