1. Kiat Chun
  2. PowerBuilder
  3. Monday, 12 August 2019 08:16 AM UTC

Hi team,

Need some advice how to encrypt text to SHA1 using PowerBuilder.

I have a PHP code below that SHA1-encrypts a string:

$password = "2ctobeR2018";
$password = sha1($password);
echo $password;
?>

yields:

f49182154133c2a2d9b4098cede92642b013110c

 

However trying to achieve the same using PowerBuilder:

String ls_data
ls_data = "2ctobeR2018"  //string from sle_ control

CrypterObject lnv_CrypterObject
lnv_CrypterObject = CREATE CrypterObject
Blob lblb_pwd
lblb_pwd = lnv_CrypterObject.SHA (SHA1!, Blob (ls_data, EncodingANSI!))

MessageBox ("Encrypted Password", String (lblb_pwd, EncodingANSI!))

yields the text below, which is different from PHP output above.

Need to know what is wrong; if I'm doing it incorrectly, wrong function, or am I missing something ?

Also tried other encoding, and but always getting extended characters.

Any advice appreciated. Thanks

 

Kiat Chun Accepted Answer Pending Moderation
  1. Tuesday, 13 August 2019 08:15 AM UTC
  2. PowerBuilder
  3. # 1

Can anyone help me translate this to PowerBuilder ?

$customerId = 108;
$user = "351130";
$password = "2ctobeR2018";
$aes_key = "NzQ3MGIyOWEyMDk0MzI3Y2RiNzlkMThjZGY5YTJmY2YzNzI1OTQxMw";
date_default_timezone_set("Asia/Singapore");
$password = sha1($password);
$ticket = $customerId.$user.$password.date('Ymd');
$IVsize = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_CBC);
$IV = substr(str_pad($aes_key, $IVsize, $aes_key), 0, $IVsize);
$keySize = mcrypt_get_key_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_CBC);
$aes_key = substr(str_pad($aes_key, $keySize, $aes_key), 0, $keySize);
$BlockSize = mcrypt_get_block_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_CBC);
$StringLength = strlen($ticket);
$Padding = $BlockSize - ($StringLength % $BlockSize);
$ticket .= str_repeat(chr($Padding), $Padding);
$ticket = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $aes_key, $ticket, MCRYPT_MODE_CBC, $IV);
$ticket = base64_encode($ticket);
$ticket = strtr($ticket, '+/=', '-_,');
echo "Ticket = '$ticket'";
?>

PHP output (changes according to date):

Ticket = 'pBFPX50um4iBVtdL1GhgMiB0RfVQYg1oHddcGngPmDVfPbOi7S96IKnVNFXG02FbFAZw4QtRj9H-7U_zRdn2FQ,,'

The password encode to SHA1 worked as advised by Arthur.

The codes in between, I believe are just string manipulations, (substr, str_pad, strlen, etc.)

But now I am stuck at these lines:

$ticket = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $aes_key, $ticket, MCRYPT_MODE_CBC, $IV);
$ticket = base64_encode($ticket);

Any help appreciated. I'm not getting the expected PHP output as above.

Powerbuilder:

Blob lblb_data
lblb_data = lnv_CrypterObject.SymmetricEncrypt (AES!, Blob (ls_ticket_pad, EncodingANSI!), Blob (ls_aes, EncodingANSI!), OperationModeCBC!, Blob (ls_iv, EncodingANSI!), PKCSPadding!)

String ls_hex
ls_hex = lnv_CoderObject.HexEncode (lblb_data)

String ls_base64str
ls_base64str = lnv_CoderObject.Base64Encode (Blob (ls_hex, EncodingANSI!))

 

Any help appreciated. Thanks.

Regards, Kiat

 

Comment
There are no comments made yet.
Kiat Chun Accepted Answer Pending Moderation
  1. Tuesday, 13 August 2019 06:51 AM UTC
  2. PowerBuilder
  3. # 2

Thanks Arthur, that password hashing worked...

 

Reason asking is because I am also translating this piece of code..

$ticket = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $aes_key, $ticket, MCRYPT_MODE_CBC, $IV);

$ticket = base64_encode($ticket);

Is the PHP output above also in Hex ?

 

PowerBuilder:

Blob lblb_data
lblb_data = lnv_CrypterObject.SymmetricEncrypt (AES!, Blob (ls_ticket, EncodingANSI!), Blob (ls_aes, EncodingANSI!), OperationModeCBC!, Blob (ls_iv, EncodingANSI!), PKCSPadding!)

 

String ls_hex
ls_hex = lnv_CoderObject.HexEncode (lblb_data)

ls_base64str = lnv_CoderObject.Base64Encode (Blob (ls_hex, EncodingANSI!))

 

Or do I convert data to Base64 first, then translate to Hex ?

(not getting same output currently. Not sure if the SymmetricEncrypt output correctly becausse data is non-readable)

 

Thanks

 

Regards,

Kiat

Comment
  1. Kiat Chun
  2. Tuesday, 13 August 2019 08:02 AM UTC
Thanks René,

Basically I am trying to translate this PHP code to PowerBuilder,

ot;;

$aes_key = "NzQ3MGIyOWEyMDk0MzI3Y2RiNzlkMThjZGY5YTJmY2YzNzI1OTQxMw";

date_default_timezone_set("Asia/Singapore");

$password = sha1($password);

$ticket = $customerId.$user.$password.date('Ymd');

$IVsize = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_CBC);

$IV = substr(str_pad($aes_key, $IVsize, $aes_key), 0, $IVsize);

$keySize = mcrypt_get_key_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_CBC);

$aes_key = substr(str_pad($aes_key, $keySize, $aes_key), 0, $keySize);

$BlockSize = mcrypt_get_block_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_CBC);

$StringLength = strlen($ticket);

$Padding = $BlockSize - ($StringLength % $BlockSize);

$ticket .= str_repeat(chr($Padding), $Padding);

$ticket = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $aes_key, $ticket, MCRYPT_MODE_CBC, $IV);

$ticket = base64_encode($ticket);

$ticket = strtr($ticket, '+/=', '-_,');

echo "Ticket = '$ticket'";

?>"
  1. Helpful
  1. Kiat Chun
  2. Tuesday, 13 August 2019 08:18 AM UTC
Hi Rene,

regarding .. "MCRYPT_RIJNDAEL_128 with a 32-byte key ..."

Do you have any example how to do this?
  1. Helpful
  1. René Ullrich
  2. Tuesday, 13 August 2019 08:54 AM UTC
Hi Kiat,

Sorry. I just found this comment. :-(
  1. Helpful
There are no comments made yet.
Arthur Hefti Accepted Answer Pending Moderation
  1. Monday, 12 August 2019 11:55 AM UTC
  2. PowerBuilder
  3. # 3

Hi

SHA1 is no encryption but a hashing algorithm.

The output from your PHP script is Hex encoded.

So PB should be instead of showing the password blob

CoderObject lnv_CoderObject
lnv_CoderObject = Create CoderObject

ls_Base64Str = lnv_CoderObject.HexEncode (lblb_pwd)
MessageBox ("Encrypted Password", ls_Base64Str)

Regards
Arthur

Comment
There are no comments made yet.
  • Page :
  • 1


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