Difference between revisions of "SAP ABAP ENCRIPTACION"
From SapWiki
| Line 154: | Line 154: | ||
encoded = p_string_base64. | encoded = p_string_base64. | ||
ENDFORM. | ENDFORM. | ||
| + | |||
| + | [[Category:ABAP]] | ||
Revision as of 17:48, 26 March 2020
Ejemplo AES 256 mode CBC
*&---------------------------------------------------------------------* *& Report YENCRIPT2_DO *&---------------------------------------------------------------------* *& *&---------------------------------------------------------------------* REPORT yencript2_do. * equivale a AES 256 mode CBC * basado en CL_SEC_SXML_WRITER=>CRYPT_AES_CTR * para test https://www.devglan.com/online-tools/aes-encryption-decryption DATA: blocksize TYPE i , keysize TYPE i , cipher TYPE xstring , block TYPE xstring , rest TYPE i , offset TYPE i , l_iv TYPE xstring , emptyiv TYPE xstring , counter(4) TYPE x , ctroffset TYPE i . DATA iv TYPE xstring. DATA key TYPE xstring. DATA input TYPE xstring. DATA result TYPE xstring. DATA l_plaintext TYPE string. DATA l_plaintext_x TYPE xstring. DATA l_key TYPE string. DATA l_key_x TYPE xstring. DATA lv_message TYPE xstring. DATA lv_message_decrypted TYPE xstring. DATA lr_xstring TYPE xstring. DATA l_base64 TYPE string. DATA lv_message_string TYPE string. DATA i_iv TYPE xstring. DATA: lf_bindata TYPE xstring.
*--------------------------------------------------------------------*
*
*--------------------------------------------------------------------*
* ejemplo: archivo JSON o XML
PARAMETERS gf_xfile TYPE string LOWER CASE.
*--------------------------------------------------------------------*
AT SELECTION-SCREEN ON VALUE-REQUEST FOR gf_xfile.
*--------------------------------------------------------------------*
DATA: window_title TYPE string.
window_title = 'Archivo de entrada'.
cl_secxml_helper=>file_f4(
EXPORTING window_title = window_title
IMPORTING filename = gf_xfile ).
*--------------------------------------------------------------------*
START-OF-SELECTION.
*--------------------------------------------------------------------*
*--------------------------------------------------------------------*
* read xml data
*--------------------------------------------------------------------*
IF gf_xfile IS NOT INITIAL.
cl_secxml_helper=>upload_file(
EXPORTING filename = gf_xfile
IMPORTING bindata = lf_bindata ).
ENDIF.
blocksize = 16.
keysize = 32.
ctroffset = 12.
emptyiv = '00000000000000000000000000000000'.
i_iv = emptyiv.
l_key = '12345678901234567890123456789012'.
l_key_x = cl_abap_hmac=>string_to_xstring( l_key ).
IF lf_bindata IS INITIAL.
l_plaintext = 'Texto a encriptar'.
l_plaintext_x = cl_abap_hmac=>string_to_xstring( l_plaintext ).
ELSE.
MOVE lf_bindata TO l_plaintext_x.
ENDIF.
iv = i_iv.
key = l_key_x.
input = l_plaintext_x.
IF xstrlen( iv ) NE blocksize OR xstrlen( key ) NE keysize.
WRITE:/ 'error en llave o IV'.
RETURN.
ENDIF.
rest = xstrlen( input ).
IF rest < 1.
RETURN. "nothing to encrypt
ENDIF.
*--------------------------------------------------------------------*
* encrypt
*--------------------------------------------------------------------*
CALL METHOD cl_sec_sxml_writer=>encrypt_iv(
EXPORTING
plaintext = input
key = key
iv = iv
algorithm = cl_sec_sxml_writer=>co_aes256_algorithm_pem
IMPORTING
ciphertext = cipher ).
* padding
lr_xstring = cipher+blocksize.
*--------------------------------------------------------------------*
* codificar archivo encriptado en BASE64
*--------------------------------------------------------------------*
PERFORM encode_base_64x USING lr_xstring CHANGING l_base64.
*--------------------------------------------------------------------*
* decrypt message
*--------------------------------------------------------------------*
cl_sec_sxml_writer=>decrypt(
EXPORTING
ciphertext = cipher
key = l_key_x
algorithm = cl_sec_sxml_writer=>co_aes256_algorithm_pem
IMPORTING
plaintext = 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.
*----------------------------------------------------------------------*
* FORM .......
*----------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM encode_base_64x USING p_xstring TYPE xstring
CHANGING p_string_base64 TYPE string.
DATA: l_http_utility TYPE REF TO cl_http_utility.
DATA: l_string TYPE string.
CREATE OBJECT l_http_utility.
CALL METHOD l_http_utility->encode_x_base64
EXPORTING
unencoded = p_xstring
RECEIVING
encoded = p_string_base64.
ENDFORM.