Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

Encoding / Decoding base64Binary with AES-128ECB

SimoneMilesi
Active Contributor

Hello all,

to follow hungarian new law about electronic invoice (NAV system) i'm fighting and struggling with encoding and decoding data... and i'm loosing, bad, such fight.

The scenario
Every communication and payload works on XML structure.

Using provided login, i ask for a token to the API and i got it as answer as base64Binary, crypted with AES-128 ECB algorithm.

I should decrypt the token and send it back in the data communication, in a field defined with the facet

What i did until now

  • I receive the token ( example from the last call: mrlWpOOkvG8uuFL05v/VPyCXxr86OoS/dPu5PFsz+mWB29p/kn1MxPsTqZ9MmNcBpEfI8RU3lzbxUu7t+U0tjg== )
  • Convert my token into XSTRING to be able to decrypt it us
 TYPES: BEGIN OF t_t,
                bin TYPE char100
             END OF t_t.
    DATA: t_bin TYPE TABLE OF  t_t WITH DEFAULT KEY,
          r_bin TYPE t_t,
          len TYPE i.
    r_bin-bin = i_data. "my token
    APPEND r_bin TO t_bin.    len = strlen( r_bin-bin ). "88
    CALL FUNCTION 'SCMS_BINARY_TO_XSTRING'
      EXPORTING
         input_length = len
      IMPORTING
        buffer       = e_hex
      TABLES
        binary_tab   = t_bin
      EXCEPTIONS
        failed       = 1
        OTHERS       = 2.
  • To decrypt the token, i got a string-type key, so i convert it to XSTRING too
 FUNCTION 'SCMS_STRING_TO_XSTRING'
     EXPORTING
       text = c_key "the string key
     IMPORTING
        buffer = xkey
     EXCEPTIONS
       failed = 1
       OTHERS = 2.
 mode = zcl_aes_utility=>mc_encryption_mode_ecb.zcl_aes_utility=>decrypt_xstring(            EXPORTING                  i_key = xkey                  i_data = i_token                  i_initialization_vector = iv "initial                 i_encryption_mode = mode           IMPORTING                 e_data = resultx ).
  • And finally i reconvert the decrypted token to Base64 again
CALL FUNCTION 'SCMS_BASE64_ENCODE'
 EXPORTING
 input = x_s
 input_length = len "XSTRING from decrypt / 2
 IMPORTING
 output = o_c
 EXCEPTIONS
 output_too_small = 1
 OTHERS = 2.
 IF sy-subrc <> 0.
* Implement suitable error handling here
 ENDIF.
 token = o_c.
  • And this lead to a token long like the first one ( ryyXiG2iEn+Y/NSeAyY4GAhlw3i6o9PaC0s3UhnPPGKjeRqmODXo6vXGW1TUoswfTn5rVLNm2Lgah6zmSDY7Cg== ), which violates the text50 facet.

i also tried to convert my XSTRING to String instead of Base64 using the proposed solution i found on SCN and around the net (i.e. http://www.samplecodeabap.com/convert-xstring-string/ ) but i discarded this solution because it ends up into generating a bunch of gibberish characters not unicode.

What i need?
i am sure i'm messing something into the whole procedure of take-the-unholy-token-and-decrypt-it, maybe messing with the strings sizes, but i'd really appreaciate if someone could slap my head and points me in the right direction.

Thanks a lot!

1 ACCEPTED SOLUTION

Tomas_Buryanek
Active Contributor

Hello Simone,

right from start you are mentioning the token you receiving (mrlWpOOkvG8uuFL05v/VPyCXxr86OoS/dPu5PFsz+mWB29p/kn1MxPsTqZ9MmNcBpEfI8RU3lzbxUu7t+U0tjg==).

But after that you are trying to convert it to xstring with SCMS_BINARY_TO_XSTRING.

I think that is the problem.

Because token looks like BASE64 string. So you need to decode this BASE64 string to xstring. Then you can decrypt this xstring and so no... rest of your steps seems OK.
TIP: I like to use CL_HTTP_UTILITY for decoding/encoding BASE64 🙂 There are 4 methods (2 for string and 2 for xstring)...

-- Tomas --
12 REPLIES 12

Tomas_Buryanek
Active Contributor

Hello Simone,

right from start you are mentioning the token you receiving (mrlWpOOkvG8uuFL05v/VPyCXxr86OoS/dPu5PFsz+mWB29p/kn1MxPsTqZ9MmNcBpEfI8RU3lzbxUu7t+U0tjg==).

But after that you are trying to convert it to xstring with SCMS_BINARY_TO_XSTRING.

I think that is the problem.

Because token looks like BASE64 string. So you need to decode this BASE64 string to xstring. Then you can decrypt this xstring and so no... rest of your steps seems OK.
TIP: I like to use CL_HTTP_UTILITY for decoding/encoding BASE64 🙂 There are 4 methods (2 for string and 2 for xstring)...

-- Tomas --

0 Kudos

Thanks Tomas!

i was pondering exactly that thing.

i replaced the FM SCMS_BINARY_TO_XSTRING with the code below but still i got a "decrypted token too long" (88 chars)

CALL FUNCTION 'SCMS_BASE64_DECODE_STR'
     EXPORTING
         input = i_data
     IMPORTING
         output = e_hex
     EXCEPTIONS
         failed = 1
         OTHERS = 2.
 IF sy-subrc = 0.
     EXIT.
 

0 Kudos

Functions SCMS_BASE64_DECODE_STR and SCMS_BASE64_ENCODE are not released. Try some other encode/decode ways (for example that class in my post).
But that won't be the problem probably...

There seems to be other problem. Are you sure that you are supposed to return that token encoded in BASE64? Shouldn't it be in plain text?

-- Tomas --

0 Kudos

Just to be sure, i try to switch to the class (that i discovered i used in an old project: i'm getting old and i forgot stuff!).

For plain text, i quote myself from the initial thread

i also tried to convert my XSTRING to String instead of Base64 using the proposed solution i found on SCN and around the net (i.e. http://www.samplecodeabap.com/convert-xstring-string/ ) but i discarded this solution because it ends up into generating a bunch of gibberish characters not unicode.

0 Kudos

Was it "gibberish" also when you tried decoding BASE64 token at the beginning?

Is your system unicode? You should always keep in my codepage when you convert binary data to string...

-- Tomas --

0 Kudos

Yes, i'm in Unicode system and all the requests are in UTF-8 as per suggestion in the documentation

The save to database and the response will always be UTF-8, regardless of the encoding specified in the inquiry, therefore it is advised to use this type of encoding in the query.

So i call

DATA lref_convt TYPE REF TO cl_abap_conv_in_ce.
 DATA lv_string TYPE string.
CALL METHOD cl_abap_conv_in_ce=>create EXPORTING input = resultx encoding = 'UTF-8' ignore_cerr = abap_true RECEIVING conv = lref_convt.
CALL METHOD lref_convt->read IMPORTING data = token.

and the token looks like

And i got as answer

An invalid XML character (Unicode: 0x1e) was found in the element content of the document

0 Kudos

Sorry, do not have any other ideas 😞
I would try combinations and double checks of each step. And clarify with somebody what is expected in that 50 char field...

-- Tomas --

0 Kudos

Thanks a lot Tomas for all the effort: i know it's something stupid i'm missing and this sends me crazy!

Anyway i contacted the hungarian IT for NAV and i'll wait the answer.

Funny thing: the standard SAP solution relying on SCP works fine so it's definitively something on my side!

Hi Tomas!

I found the solution: due an error in the documentation (well, more a "not so clear passage" in it), i was using the wrong key (i got 2 of them) to decode the token.

Once i managed to use the right key, i managed to convert the token in UTF-8 with the right format!

DATA: obj TYPE REF TO cl_abap_conv_in_ce.
TRY. CALL METHOD cl_abap_conv_in_ce=>create EXPORTING input = resultx encoding = 'UTF-8' replacement = '?' ignore_cerr = abap_true RECEIVING conv = obj. CATCH cx_parameter_invalid_range . CATCH cx_sy_codepage_converter_init . ENDTRY. len = xstrlen( resultx ). TRY. CALL METHOD obj->read EXPORTING n = len IMPORTING data = token. CATCH cx_sy_conversion_codepage . CATCH cx_sy_codepage_converter_init . CATCH cx_parameter_invalid_type . CATCH cx_parameter_invalid_range .
ENDTRY.

Thanks for all the time you put in this issue and and forgive me to have you wasting it!

0 Kudos

Glad you made it working 🙂

-- Tomas --

raviandela
Explorer
0 Kudos

Hi Simone,

Similar development was carried out by someone earlier and has some problems in de-crypting. This was discussed in one of the issues of AES library. Please look into the issue at the url

https://github.com/Sumu-Ning/AES/issues/15

Issue there is different but, it might give you an idea how it is handled there. Further, the code used there is an adapted version of the whole library but not the library itself.

Hope that helps..

0 Kudos

Hi Ravi,

it's not a decrypt issue: as i said, i used a couple of online tools and i got the same result as the class.

So i'm somehow confident it's not that the point 😕