1. Antony Xavier
  2. PowerBuilder
  3. Thursday, 21 April 2022 06:17 AM UTC

Hi All,

How I can check encoded string is correct, which is converted blob data using Base64.

We are uploading encoded (using base64 Encoding) Blob data to "Docuware", some time it is not accessible from "Docuware", so before uploading, want to double check.

I had read few articles to solve above issue, compare with regexp ("^([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)?$") but no hope.

And at last I converted to blob (base64 Decoding) again and comparing with length of the blob, but after conversion length is miss matching, can you advice.

Please find the attached object for reference n_cryptapi (thanks to rolland and Chris)

using 

of_base64encode

ULong lul_MsgLen, lul_BufLen
string ls_temp

setnull(ls_temp)
// determine size of the encoded buffer
lul_MsgLen = Len(ablb_message)
If Not CryptBinaryToString(ablb_message, lul_MsgLen, &
CRYPT_STRING_BASE64, ls_temp, lul_BufLen) Then
LastErrCode = of_GetLastError(LastErrText)
LastFunction = "CryptBinaryToString"
//messagebox( 'LastErrCode 1', LastErrCode )
Return False
End If

// allocate encoded buffer
as_encoded = Space(lul_BufLen)

// encode the binary data as Base64 string
If Not CryptBinaryToString(ablb_message, lul_MsgLen, &
CRYPT_STRING_BASE64 + CRYPT_STRING_NOCRLF, &
as_encoded, lul_BufLen) Then
LastErrCode = of_GetLastError(LastErrText)
LastFunction = "CryptBinaryToString"
//messagebox( 'LastErrCode 2', LastErrCode )
Return False
End If
//
Return True

and 

of_base64decode

// Declarations.
ULong lul_len
ULong lul_buflen
ULong lul_skip
ULong lul_pflags
Boolean lb_rc


lul_len = Len ( as_encoded )
lul_buflen = lul_len
ablb_value = Blob (Space (lul_len))

lb_rc = CryptStringToBinary ( as_encoded , lul_len, &
CRYPT_STRING_BASE64, ablb_value , &
lul_buflen, lul_skip, lul_pflags )
Return lb_rc

 

Regards

Antony

Attachments (1)
Chris Pollach @Appeon Accepted Answer Pending Moderation
  1. Thursday, 21 April 2022 18:55 PM UTC
  2. PowerBuilder
  3. # 1

Hi Anthony;

   Have a look at my updated Base64 Example. It catches Corrupt/Incomplete BASE64.txt files and also files with the wrong encoding. I updated this example last night but just refreshed it again this afternoon after testing the corrupt file scenario. Looks like all you may need to do is try to import the Base64 data stream again after you produce it and then check for the appropriate return codes from the "of_base64_stringtobinary" command, as follows:

 

For more information:  http://chrispollach.blogspot.com/2022/04/base64.html

Note: I will be officially releasing this code later tonight after completing W11 testing. W10 looks great.  :-)

Regards ... Chris

 

Comment
  1. Antony Xavier
  2. Friday, 22 April 2022 05:14 AM UTC
Hi Chris,

Thank you very much.

I will download PB version 2021, and test the code and will come back



Regards

Antony



  1. Helpful
There are no comments made yet.
Antony Xavier Accepted Answer Pending Moderation
  1. Thursday, 21 April 2022 11:07 AM UTC
  2. PowerBuilder
  3. # 2

Hi Arnd Schmidt,

 

It was not working in PowerServer 2017 & 2019. That is why using the wrapper.

Regards

Antony

 

 

Comment
  1. Arnd Schmidt
  2. Thursday, 21 April 2022 11:53 AM UTC
OK, the problem seems to be the

ablb_value = Blob (Space (lul_len))



You have to calculate the length of the buffer (blob). Remember the Blob holds binary data.

https://docs.microsoft.com/en-us/windows/win32/api/wincrypt/nf-wincrypt-cryptstringtobinarya

Or

remove "unused" binary data at the end of the blob. Something like:

ablb_value = BlobMid(ablb_value , 1, lul_buflen)
  1. Helpful
  1. Antony Xavier
  2. Friday, 22 April 2022 04:55 AM UTC
Thank you very much for your advice
  1. Helpful
There are no comments made yet.
Arnd Schmidt Accepted Answer Pending Moderation
  1. Thursday, 21 April 2022 10:56 AM UTC
  2. PowerBuilder
  3. # 3

Hi Antony,

I would use the built-in CoderObject and its encoding/decoding functions (if you use PB 2017 an up)

https://docs.appeon.com/pb2017r3/powerscript_reference/ch10s38.html

hth

Arnd

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.