1. mike S
  2. PowerBuilder
  3. Wednesday, 25 October 2023 17:26 PM UTC

I need to decrypt a password in c# that i both encrypt and decrypt in PB.  It took a while to figure out and the following works. 

the value to decrypt is ls_encrypted (base64 string) and the decryption key is: is_symkey

 

 


PB:

lblb_pwrdencypted = lnv_CoderObject.Base64decode( ls_encrypted )
lblb_key = blob(is_symkey, encodingansi!)
lblb_pwrd = lnv_CrypterObject.SymmetricDEcrypt(AES!, lblb_pwrdencypted, lblb_key )
ls_unencrypted = string(lblb_pwrd, EncodingANSI!)

 

c#:
byte[] pwdencrypted, symkey;
string ls_unencrypted
pwdencrypted = Convert.FromBase64String(ls_encrypted);
symkey = Encoding.ASCII.GetBytes(is_symkey); //ASCII = ANSI ???

using (var aes = System.Security.Cryptography.Aes.Create())
{
  aes.Mode = CipherMode.ECB;
  aes.Key = symkey;  //aes.IV ??? PB - zero filled by default and ingnored for ECB
  aes.Padding = PaddingMode.PKCS7; //PB is PKCS5 which is said to be the same as PKCS7 since it doesn't make sense otherwise for ECB?
  //DefaultPadding! – (Default) Default padding scheme. DefaultPadding! means PKCSPadding! for ECB or CBC mode. Otherwise, NoPadding! for modes like CFB, OFB, and CTR.

  ICryptoTransform decryptor = aes.CreateDecryptor(aes.Key, aes.IV);

  using (MemoryStream msDecrypt = new MemoryStream(pwdencrypted))
  {
    using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
    {
      using (StreamReader srDecrypt = new StreamReader(csDecrypt))
      {
        ls_unencrypted = srDecrypt.ReadToEnd(); // Read the decrypted bytes from the decrypting stream and place them in a string.
      }
    }
  }
}



There are no replies made for this question yet.
However, you are not allowed to reply to this question.