1. Suhas Shravagi
  2. PowerBuilder
  3. Wednesday, 24 June 2020 11:10 AM UTC

Hi,

I am trying to implement the encryption-decryption with TDES algorithm. Below is the piece of code I have written:

//Instance Variables

CONSTANT STRING ICS_KEY = "pP?g$L#C%a)*N^A*dX&Ao1j4"
CONSTANT STRING ICS_IV = "?Im#9-0$"

//Encryption function:

Blob lb_data, lb_key, lb_iv, lb_encrypt
String ls_input, ls_encrypt

ls_input = "ENCRYPT_THIS"
lb_data = Blob(ls_input, EncodingUTF8!)
lb_key = Blob(ICS_KEY, EncodingUTF8!)
lb_iv = Blob(ICS_IV, EncodingUTF8!)

CrypterObject lnv_CrypterObject
lnv_CrypterObject = Create CrypterObject

lb_encrypt = lnv_CrypterObject.SymmetricEncrypt(TDES!, lb_data, lb_key, OperationModeCBC!, lb_iv, PKCSPadding!)

ls_encrypt = String(lb_encrypt, EncodingUTF8!)

st_encrypted.Text = ls_encrypt

-----------------------------------------------------------------------------------------------------------------------------------

//Decryption function:

Blob lb_data, lb_key, lb_iv, lb_decrypt
String ls_input, ls_decrypt

ls_input = st_encrypted.Text

lb_data = Blob(ls_input, EncodingUTF8!)
lb_key = Blob(ICS_KEY, EncodingUTF8!)
lb_iv = Blob(ICS_IV, EncodingUTF8!)

CrypterObject lnv_CrypterObject
lnv_CrypterObject = Create CrypterObject

lb_decrypt = lnv_CrypterObject.SymmetricDecrypt(TDES!, lb_data, lb_key, OperationModeCBC!, lb_iv, PKCSPadding!)

ls_decrypt = String(lb_decrypt, EncodingUTF8!)

st_decrypted.Text = ls_decrypt

 

It is supposed to give me the original input string "ENCRYPT_THIS" after decryption, but it is not. The encrypted string and the decrypted string gives me random values. It works completely fine when I use encoding format as EncodingANSI!. Can someone help on this?

 

--Thanks.

Accepted Answer
René Ullrich Accepted Answer Pending Moderation
  1. Thursday, 25 June 2020 05:27 AM UTC
  2. PowerBuilder
  3. # Permalink

Hi Suhas,

I have tested your code. The reason for your problems are definitly the conersions from blob to string.

The encrpytion and decryption works well if you don't convert the encrypted blob and use this encrypted blob directly as input for decryption.

In next step I replaced the string conversion with Base64Encode. And it also works well. (You may also use Hexencode or Base32Encode.)

Here is my code: (I have all in one script)

CONSTANT STRING ICS_KEY = "pP?g$L#C%a)*N^A*dX&Ao1j4"
CONSTANT STRING ICS_IV = "?Im#9-0$"

Blob lb_data, lb_key, lb_iv, lb_encrypt, lb_decrypt
String ls_input, ls_encrypt, ls_decrypt

CrypterObject lnv_CrypterObject
CoderObject lnv_CoderObject


//Encryption
ls_input = "ENCRYPT_THIS"
lb_data = Blob(ls_input, EncodingUTF8!)
lb_key = Blob(ICS_KEY, EncodingUTF8!)
lb_iv = Blob(ICS_IV, EncodingUTF8!)

lnv_CrypterObject = Create CrypterObject
lnv_CoderObject = Create CoderObject

lb_encrypt = lnv_CrypterObject.SymmetricEncrypt(TDES!, lb_data, lb_key, OperationModeCBC!, lb_iv, PKCSPadding!)

//ls_encrypt = String(lb_encrypt, EncodingUTF8!) // this will not work
ls_encrypt = lnv_CoderObject.Base64Encode(lb_encrypt)


//Decryption
//lb_data = Blob(ls_encrypt, EncodingUTF8!) // this will not work
//lb_data = lb_encrypt // this works well
lb_data = lnv_CoderObject.Base64Decode(ls_encrypt) // this works well
lb_key = Blob(ICS_KEY, EncodingUTF8!)
lb_iv = Blob(ICS_IV, EncodingUTF8!)


lb_decrypt = lnv_CrypterObject.SymmetricDecrypt(TDES!, lb_data, lb_key, OperationModeCBC!, lb_iv, PKCSPadding!)

ls_decrypt = String(lb_decrypt, EncodingUTF8!)

MessageBox ("Test", ls_decrypt)

 

Regards,

René

Comment
  1. Suhas Shravagi
  2. Thursday, 25 June 2020 11:13 AM UTC
Thanks René, it worked for me as well. Also any idea which exception does CrypterObject and CoderObject throws?



--Thanks.
  1. Helpful
  1. René Ullrich
  2. Thursday, 25 June 2020 13:52 PM UTC
Seems to be a RuntimeError exception
  1. Helpful
There are no comments made yet.
Suhas Shravagi Accepted Answer Pending Moderation
  1. Wednesday, 24 June 2020 14:31 PM UTC
  2. PowerBuilder
  3. # 1

Hello René

If this is the case, it should not have worked with EncodingANSI! but it was working. Also I checked the coderobject, but in some cases the blob is having 0 length data and so it is failing.

--Thanks.

Comment
There are no comments made yet.
René Ullrich Accepted Answer Pending Moderation
  1. Wednesday, 24 June 2020 13:19 PM UTC
  2. PowerBuilder
  3. # 2

Hi Suhas,

I think you can't convert encrypted BLOB to String and back using String method. There may be conversion losts.

Use Base64Encode and Base64Decode functions of CoderObject instead.

HTH,

René

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.
We use cookies which are necessary for the proper functioning of our websites. We also use cookies to analyze our traffic, improve your experience and provide social media features. If you continue to use this site, you consent to our use of cookies.