[ABAP] Verschlüsselung mit AES256

TRY.
* get sap codepage by external name
    DATA(lv_codepage_utf8) = cl_abap_codepage=>sap_codepage( 'utf-8' ).

* AES-256 Encryption Key
    DATA(lv_key_xstr) = cl_sec_sxml_writer=>generate_key( algorithm = cl_sec_sxml_writer=>co_aes256_algorithm ).

* Plaintext
    DATA(lv_plain_str) = |Testtext_123|.

* STRING --> XSTRING
    DATA(lv_plain_xstr) = cl_bcs_convert=>string_to_xstring( iv_codepage = CONV #( lv_codepage_utf8 )
                                                             iv_string   = lv_plain_str ).

* 16 Bit-Initialisierungsvector für CBC-(Cypher Block Chaining)-Modes der AES Encryption
    DATA(lv_iv_xstr) = CONV xstring( '00000000000000000000000000000000' ).

* Encrypt data with Initialization Vector

    DATA: lv_encrypted_xstr TYPE xstring.

    cl_sec_sxml_writer=>encrypt_iv( EXPORTING plaintext  = lv_plain_xstr
                                              key        = lv_key_xstr
                                              iv         = lv_iv_xstr
                                              algorithm  = cl_sec_sxml_writer=>co_aes256_algorithm_pem
                                    IMPORTING ciphertext = lv_encrypted_xstr ).


* Decrypt data
    DATA: lv_decrypted_xstr TYPE xstring.

    cl_sec_sxml_writer=>decrypt( EXPORTING ciphertext = lv_encrypted_xstr
                                           key        = lv_key_xstr
                                           algorithm  = cl_sec_sxml_writer=>co_aes256_algorithm_pem
                                 IMPORTING plaintext  = lv_decrypted_xstr ).

* XSTRING --> STRING
    DATA(lv_decrypted_str) = cl_bcs_convert=>xstring_to_string( iv_cp   = lv_codepage_utf8
                                                                iv_xstr = lv_decrypted_xstr ).

* Ausgabe
    WRITE: / 'Plain:', lv_plain_str.
    WRITE: / 'Encrypted xstring:', lv_encrypted_xstr.
    WRITE: / 'Decrypted xstring:', lv_decrypted_xstr.
    WRITE: / 'Decrypted:', lv_decrypted_str.

  CATCH cx_root INTO DATA(e_txt).
    WRITE: / e_txt->get_text( ).
ENDTRY.