ABAP 实现AES加密

1.Github下载工具类https://github.com/Sumu-Ning/AES,利用abapGit上传至SAP系统

2.AES加密需要四个参数 ①密钥②加密模式③字节填充模式④偏移量

3.将明文转XSTRING,密钥BASE64解码,调用zcl_aes_utility=>encrypt_xstring方法

  METHOD aes_encrypt.

    DATA:lv_content_xstr  TYPE xstring,
         lv_key_xstr      TYPE xstring,
         lv_response_xstr TYPE xstring.

    "#明文
    CALL FUNCTION 'SCMS_STRING_TO_XSTRING'
      EXPORTING
        text   = iv_content
      IMPORTING
        buffer = lv_content_xstr
      EXCEPTIONS
        failed = 1.
    "#密钥
    CALL FUNCTION 'SCMS_BASE64_DECODE_STR'
      EXPORTING
        input  = gc_aes_key
      IMPORTING
        output = lv_key_xstr
      EXCEPTIONS
        failed = 1
        OTHERS = 2.
    IF sy-subrc <> 0.
* Implement suitable error handling here
    ENDIF.
    zcl_aes_utility=>encrypt_xstring(
      EXPORTING
        i_key                   = lv_key_xstr
        i_data                  = lv_content_xstr
        i_encryption_mode       = zcl_aes_utility=>mc_encryption_mode_ecb
        i_padding_standard      = zcl_byte_padding_utility=>mc_padding_standard_pkcs_7  "字节填充模式
      IMPORTING
        e_data                  = lv_response_xstr ).

    CALL FUNCTION 'ZBC_CONVERT_XSTRING_TO_BYTE'
      EXPORTING
        iv_xstring     = lv_response_xstr
      IMPORTING
        ev_byte_string = ev_response.

  ENDMETHOD.

 

 

  METHOD aes_encrypt.

    DATA:lv_content_xstr  TYPE xstring,
         lv_key_xstr      TYPE xstring,
         lv_response_xstr TYPE xstring.

    "#明文
    CALL FUNCTION 'SCMS_STRING_TO_XSTRING'
      EXPORTING
        text   iv_content
      IMPORTING
        buffer lv_content_xstr
      EXCEPTIONS
        failed 1.
    "#密钥
    CALL FUNCTION 'SCMS_BASE64_DECODE_STR'
      EXPORTING
        input  gc_aes_key
      IMPORTING
        output lv_key_xstr
      EXCEPTIONS
        failed 1
        OTHERS 2.
    IF sy-subrc <> 0.
* Implement suitable error handling here
    ENDIF.
    zcl_aes_utility=>encrypt_xstring(
      EXPORTING
        i_key                   lv_key_xstr
        i_data                  lv_content_xstr
        i_encryption_mode       zcl_aes_utility=>mc_encryption_mode_ecb
        i_padding_standard      zcl_byte_padding_utility=>mc_padding_standard_pkcs_7  "字节填充模式
      IMPORTING
        e_data                  lv_response_xstr ).

    CALL FUNCTION 'ZBC_CONVERT_XSTRING_TO_BYTE'
      EXPORTING
        iv_xstring     lv_response_xstr
      IMPORTING
        ev_byte_string ev_response.

  ENDMETHOD.
posted @ 2023-09-04 19:28  码农小夏  阅读(354)  评论(0编辑  收藏  举报