waelmohamedziw2013
waelmohamedziw2013

Reputation: 11

Why Encode a string in Base64 (based on the Microsoft Crypto API) using PowerBuilder produce a result differ than Convert.ToBase64String() in .Net?

I have a text contains a hexadecimal bytes as follows:

01 1b 42 61 6b 68 73 68 5f 48 6f 73 70 69 74 61 6c 5f 41 6c 2d 53 68 61 72 61 66 69 61 02 0f 33 30 30 31 37 34 37 36 39 35 31 30 30 30 33 03 10 30 32 2f 30 31 2f 31 37 5f 30 35 3a 31 32 5f 50 04 05 31 34 2e 30 30 05 01 30

, when I am encrypting it to BASE64 using the of_encode64() function in powerbuilder winsock user object, I got the below result:

MDExQjQyNjE2QjY4NzM2ODVGNDg2RjczNzA2OTc0NjE2QzVGNDE2QzJENTM2ODYx NzI2MTY2Njk2MTAyMEYzMzMwMzAzMTM3MzQzNzM2MzkzNTMxMzAzMDMwMzMwMzEw MzAzMjJGMzAzMTJGMzEzNzVGMzAzNTNBMzEzMjVGNTAwNDA1MzEzNDJFMzAzMDA1 MDEzMA==

, but when my friend encodes it using the Convert.ToBase64String() in Dot Net, he got a totally different result as below:

ARtCYWtoc2hfSG9zcGl0YWxfQWwtU2hhcmFmaWECDzMwMDE3NDc2OTUxMDAwMwMQMDIvMDEvMTdfMDU6MTJfUAQFMTQuMDAFATA= , I played with all possible parameters: CRYPT_STRING_BASE64HEADER, CRYPT_STRING_BASE64REQUESTHEADER, CRYPT_STRING_BASE64X509CRLHEADER but not help!

The of_encode64() function code as below:

[local function declaration]
Function boolean CryptBinaryToString ( &
    Blob pbBinary, &
    ulong cbBinary, &
    ulong dwFlags, &
    Ref string pszString, &
    Ref ulong pcchString &
    ) Library "crypt32.dll" Alias For "CryptBinaryToStringW"
[instance variables]
// Base64, with certificate beginning and ending headers
CONSTANT Ulong CRYPT_STRING_BASE64HEADER = 0

// Base64, without headers
CONSTANT Ulong CRYPT_STRING_BASE64 = 1

// Base64, with request beginning and ending headers
CONSTANT Ulong CRYPT_STRING_BASE64REQUESTHEADER = 3

// Base64, with X.509 CRL beginning and ending headers
CONSTANT Ulong CRYPT_STRING_BASE64X509CRLHEADER = 9

Encode a String to Base64:

// -----------------------------------------------------------------------------
// SCRIPT:     n_winsock.of_Encode64
//
// PURPOSE:    This function converts binary data to a Base64 encoded string.
//
//                  Note: Requires Windows XP or Server 2003
//
// ARGUMENTS:  ablob_data - Blob containing data
//
// RETURN:     String containing encoded data
//
// DATE        PROG/ID      DESCRIPTION OF CHANGE / REASON
// ----------  --------     -----------------------------------------------------
// 08/20/2010   RolandS     Initial coding
// 09/27/2010   RolandS     Changed to remove trailing CRLF characters
// 03/17/2012   RolandS     Changed argument to read-only
// -----------------------------------------------------------------------------

String ls_encoded
ULong lul_len, lul_buflen
Boolean lb_rtn

lul_len = Len(ablob_data)

lul_buflen = lul_len * 3
//lul_buflen = lul_len * 2

ls_encoded = Space(lul_buflen)

lb_rtn = CryptBinaryToString(ablob_data, lul_len, &
                    CRYPT_STRING_BASE64, ls_encoded, lul_buflen)
If lb_rtn Then
    ls_encoded = Left(ls_encoded, lul_buflen - 2)
Else
    SetNull(ls_encoded)
End If

Return ls_encoded

I am calling it using this code: Where as_data is the Hexadecimal Bytes String above

Return of_Encode64(Blob(as_data, EncodingAnsi!))

The full code for Encode/decode a string in Base64 in PowerBuilder using the winsock user object in this below URL: https://www.rgagnon.com/pbdetails/pb-0258.html

Thank you in advance. Keep Safe.

Upvotes: 0

Views: 337

Answers (0)

Related Questions