zoukankan      html  css  js  c++  java
  • ABAP-AES加解密

    https://blogs.sap.com/2019/08/26/aes-encryption-in-abap/

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

    https://blog.csdn.net/u012232542/article/details/103184183

    介绍

    最近,我们的组织要求对从 SAP 到外部系统的所有数据传输实施加密,以增加额外的安全层。要求是对系统之间共享的信息进行 AES256 加密和 Base64 编码。加密/解密使用在 SAP 中生成并通过系统自动电子邮件共享的公共密钥完成。

    用于流程的 SAP 类/功能模块:

    • CL_SEC_SXML_WRITER 用于实现生成 AES 密钥和信息加解密的逻辑。
    • SCMS_BASE64_<EN/DE>CODE_STR FM 用于 Base64 编码/解码信息。

    高级流程

    以下是我们用于加密/解密的步骤和示例代码。

    生成加密密钥

    我们使用以下逻辑生成用于加密的密钥,该密钥存储在表中,然后与外部系统共享。

    *Sample Code to generate Key:
    data: random          type xstring, wa_bench_config type   zhr_bench_config.
      call method cl_sec_sxml_writer=>generate_key
          exporting
            algorithm = cl_sec_sxml_writer=>co_aes256_algorithm
          receiving
            key       = random.
        data(lr_conv_key) = cl_abap_conv_out_ce=>create( ).
    
        lr_conv_key->write( data = random ).
        e_key = lr_conv_key->get_buffer( ).

    解密

    外部系统发送 AES 加密和 Base64 编码的数据,在 SAP 中,我们使用以下逻辑来解密文本。

     data:  i_key_xstring type xstring, i_iv type xstring.
    i_iv = '00000000000000000000000000000000'.
     if i_text is not initial.
          call function 'SCMS_BASE64_DECODE_STR'
            exporting
              input  = i_text
    *         UNESCAPE       = 'X'
            importing
              output = i_xstring
    *       EXCEPTIONS
    *         FAILED = 1
    *         OTHERS = 2
            .
          if sy-subrc <> 0.
    * Implement suitable error handling here
          endif.
    
        endif.
        if i_xstring is not initial.
    * For CL_SEC_SXML_WRITER to work with external application we need to add 16 bit 
    * extra padding before decryption
          concatenate   i_iv(16) i_xstring into i_xstring  in byte mode.
          try.
              cl_sec_sxml_writer=>decrypt(
                exporting
                  ciphertext = i_xstring
                  key =        i_key_xstring
                  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 = e_text_dec ).
            catch cx_sec_sxml_encrypt_error into data(oref). .
          endtry.
        endif.

    加密:

    SAP 处理信息并使用以下逻辑发回加密响应:

     data(lr_conv_sec) = cl_abap_conv_out_ce=>create( ).
          lr_conv_sec->write( data = i_text ).
          " encrypt using AES256
          i_xstring = lr_conv_sec->get_buffer( ).
      i_iv = '00000000000000000000000000000000'.
    
          cl_sec_sxml_writer=>encrypt_iv(
             exporting
               plaintext  = i_xstring
               key        = i_key_xstring
               iv         = i_iv
               algorithm  = cl_sec_sxml_writer=>co_aes256_algorithm_pem
             importing
               ciphertext = data(lv_message)  ).
    
          data: lr_conv    type ref to cl_abap_conv_in_ce,
                lr_xstring type xstring,
                lr_string  type string.
    *Before sending encrypted information to external system, remove the extra 
    *16 bit padding from the xstring
          lr_xstring = lv_message+16.
    
    
          data: lt_data type tsfixml, l_len type i.
          call function 'SCMS_BASE64_ENCODE_STR'
            exporting
              input  = lr_xstring
            importing
              output = e_text_enc.
    
        endif.

    示例输出:

    EXAMPLE:
    Text: Test AES@CBC#PKCS$5
    Encrypted Text : B8Q1+w5vH9jG3V/ejYg5igeGNgfX6nvqUGrDnogyDdo=
    After Decryption : Test AES@CBC#PKCS$5
    

    结论

    该博客文章提供了有关如何在 SAP 中加密和解密信息以及如何规划与外部系统的集成的信息。此处的示例代码适用于 AES256/CBC/PKCS5 Padding 算法,但 CL_SEC_SXML_WRITER 类也有其他 AES 加密算法。

    请注意,除了加密密钥外,我们还需要共享 16 位十六进制字符串 ('0000000000000000') 的 IV 密钥。

  • 相关阅读:
    下拉选择框:数字从n到m
    event.target返回事件的目标节点(触发该事件的节点)
    nextSibling使用注意(html的注释)
    mysql导出 csv文件
    ie打印去掉页眉页脚
    jsp标签循环标签体
    String.format字符串格式化方法
    [**奇文共赏**补充问题] 据说看五遍能懂的人智商 > 200
    [转] ARP的处理办法!
    [转] 一张废手机卡的作用大全 (没试过)
  • 原文地址:https://www.cnblogs.com/ricoo/p/15239558.html
Copyright © 2011-2022 走看看