cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Encryption and decryption

Catherine3
Explorer
0 Likes
599

Hi Experts,

I have created below sample program for encrypt and decrypt. It works fine. However, my requirement is to create two separate program modules one for encrypt and the other decrypt. The decrypt value will be passed from SAP service gateway. Once I separated into two modules. It fails to decrypt with short dump message Error when decrypting XML data. Why?

 

REPORT ztest1.

DATA lv_message_string TYPE string.
DATA: lv_in_string TYPE string VALUE '20241001|T520|30000001|0018014695|20240903174922'.

WRITE:/ lv_in_string.

" create message
DATA(lr_conv_sec) = cl_abap_conv_out_ce=>create( ).
lr_conv_sec->write( data = lv_in_string ).

" create key
DATA(lr_conv_key) = cl_abap_conv_out_ce=>create( ).
lr_conv_key->write( data = 'MySymmetricKey' ).

" encrypt using AES256
cl_sec_sxml_writer=>encrypt(
  EXPORTING
    plaintext =  lr_conv_sec->get_buffer( )
    key =        lr_conv_key->get_buffer( )
    algorithm =  cl_sec_sxml_writer=>co_aes256_algorithm
  IMPORTING
    ciphertext = DATA(lv_message) ).


WRITE lv_message.

DATA: lv_message1 TYPE xstring.

*lv_message1 = lv_message.


DATA: ls_bin  TYPE c LENGTH 255,
      lv_itab LIKE TABLE OF ls_bin.

DATA len TYPE i.
*
CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'
  EXPORTING
    buffer        = lv_message
*   APPEND_TO_TABLE       = ' '
  IMPORTING
    output_length = len
  TABLES
    binary_tab    = lv_itab.

CALL FUNCTION 'SCMS_BINARY_TO_STRING'
  EXPORTING
    input_length = len
*   FIRST_LINE   = 0
*   LAST_LINE    = 0
*   MIMETYPE     = ' '
*   ENCODING     =
  IMPORTING
    text_buffer  = lv_message_string
*   OUTPUT_LENGTH       =
  TABLES
    binary_tab   = lv_itab
  EXCEPTIONS
    failed       = 1
    OTHERS       = 2.
IF sy-subrc <> 0.
* Implement suitable error handling here
ENDIF.

WRITE:/ lv_message_string.

CALL FUNCTION 'SCMS_STRING_TO_XSTRING'
  EXPORTING
    text   = lv_message_string
*   mimetype = space
*   encoding =
  IMPORTING
    buffer = lv_message1
*  EXCEPTIONS
*   failed = 1
*   others = 2
  .
IF sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*   WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.

WRITE:/ lv_message1.

" decrypt message

cl_sec_sxml_writer=>decrypt(
  EXPORTING
    ciphertext = lv_message1
    key =        lr_conv_key->get_buffer( )
    algorithm =  cl_sec_sxml_writer=>co_aes256_algorithm "_pem
  IMPORTING
    plaintext =  DATA(lv_message_decrypted) ).

" convert xstring to string for output
cl_abap_conv_in_ce=>create( input = lv_message_decrypted )->read( IMPORTING data = lv_message_string ).
*
*" output secret message
WRITE lv_message_string.

 

Accepted Solutions (0)

Answers (0)