Corporation Wechat with SAP ABAP AES encode

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

In this project, SAP will do interface with Chinese Tencent Company Corporation Wechat APP.

It is not easy to access this. This blog will save the experice about this,Hope it is helpful for you.

You can copy below source code into SE38 and then run it.

Any question, fell free to contact my public wechat hot583.

 

sytitle ‘企业微信和SAP ABAP接口开发之AES加密解密’.
*企业微信的开发文档
*https://developer.work.weixin.qq.com/devtool/introduce?id=36388
*假设在服务商管理端为某个套件有如下配置参数:
*corpId = “wx5823bf96d3bd56c7”
*token = “QDG6eK”
*encodingAesKey = “jWmYm7qr5nMoAUwZRjGtBxmz3KA1tkAj3ykkR6q2B2C”
*
*收到来自企业微信的回调为:
*xml请求示例:
*POST /cgi-bin/wxpush?msg_signature=477715d11cdb4164915debcba66cb864d751f3e6&timestamp=1409659813&nonce=1372623149 HTTP/1.1
*Host: qy.weixin.qq.com
*Content-Length: 603
*<xml>
*<ToUserName><![CDATA[wx5823bf96d3bd56c7]]></ToUserName>
*<Encrypt><![CDATA[RypEvHKD8QQKFhvQ6QleEB4J58tiPdvo+rtK1I9qca6aM/wvqnLSV5zEPeusUiX5L5X/0lWfrf0QADHHhGd3QczcdCUpj911L3vg3W/sYYvuJTs3TUUkSUXxaccAS0qhxchrRYt66wiSpGLYL42aM6A8dTT+6k4aSknmPj48kzJs8qLjvd4Xgpue06DOdnLxAUHzM6+kDZ+HMZfJYuR+LtwGc2hgf5gsijff0ekUNXZi
*qATP7PF5mZxZ3Izoun1s4zG4LUMnvw2r+KqCKIw+3IQH03v+BCA9nMELNqbSf6tiWSrXJB3LAVGUcallcrw8V2t9EL4EhzJWrQUax5wLVMNS0+rUPA3k22Ncx4XXZS9o0MBH27Bo6BpNelZpS+/uh9KsNlY6bHCmJU9p8g7m3fVKn28H3KDYA5Pl/T8Z1ptDAVe0lXdQ2YoyyH2uyPIGHBZZIs2pDBS8R07+qN+E7Q==]]></Encrypt>
*<AgentID><![CDATA[218]]></AgentID>
*</xml>

**********************************************************************
*json请求示例:
*注意这里的 tousername,encrypt,agentid均为小写
*POST /cgi-bin/wxpush?msg_signature=477715d11cdb4164915debcba66cb864d751f3e6&timestamp=1409659813&nonce=1372623149 HTTP/1.1
*Host: qy.weixin.qq.com
*Content-Length: 364
*{
*  “tousername”: “wx5823bf96d3bd56c7”,
*  “encrypt”:
“No8isRLoXqFMhLlpe7R/DA7UbJ88DKJxDhJH/UVG3o1ib0Fhzdd3qWYHH/KL1mITv5qOCp2FbyILqfI7zazrp/ARgSHR177OCrv8O9UrMHWdnOaMXaz+mLd5X5VWm5r2J3Qpm+NdTQRPhHbce88frKF3wqTaZunKW7ae87bRZUfaq5tLFnyTsf6aiy0su3SsQ06dQGKPcHfYHY3upB881008Q9t9xeAZ/uqfXpYQgSLQfaX+fk/K/FQEl4QpLk
*94eD1YjluFY3uLnKp40zDyxgeWRAmgTtmx1eLwediVqZ8=”,
*  “agentid”: “218”
*}

*第一步:准备相关参数

*AESKey = Base64_Decode(EncodingAESKey + “=”)
*signature = “477715d11cdb4164915debcba66cb864d751f3e6”;
*timestamps = “1409659813”;
*nonce = “1372623149”;
*msg_encrypt =
*”RypEvHKD8QQKFhvQ6QleEB4J58tiPdvo+rtK1I9qca6aM/wvqnLSV5zEPeusUiX5L5X/0lWfrf0QADHHhGd3QczcdCUpj911L3vg3W/sYYvuJTs3TUUkSUXxaccAS0qhxchrRYt66wiSpGLYL42aM6A8dTT+6k4aSknmPj48kzJs8qLjvd4Xgpue06DOdnLxAUHzM6+kDZ+HMZfJYuR+LtwGc2hgf5gsijff0ekUNXZiqATP7PF5mZxZ3Izou
*n
*1s4zG4LUMnvw2r+KqCKIw+3IQH03v+BCA9nMELNqbSf6tiWSrXJB3LAVGUcallcrw8V2t9EL4EhzJWrQUax5wLVMNS0+rUPA3k22Ncx4XXZS9o0MBH27Bo6BpNelZpS+/uh9KsNlY6bHCmJU9p8g7m3fVKn28H3KDYA5Pl/T8Z1ptDAVe0lXdQ2YoyyH2uyPIGHBZZIs2pDBS8R07+qN+E7Q==”;

TYPES:BEGIN OF ty_par,
name  TYPE string,
value TYPE string,
END OF ty_par.
DATA:lt_par TYPE TABLE OF ty_par,
ls_par TYPE ty_par.

DATA lv_cr TYPE string.
*EncodingAESKey:用于消息体的加密,长度固定为43个字符,从a-z, A-Z, 0-9共62个字符中选取,是AESKey的Base64编码。解码后即为32字节长的AESKey
DATA:lv_encodingaeskey_43 TYPE string VALUE ‘jWmYm7qr5nMoAUwZRjGtBxmz3KA1tkAj3ykkR6q2B2C’“EncodingAESKey

*AESKey:AES算法的密钥,长度为32字节。编码后为64字节。AES采用CBC模式,数据采用PKCS#7填充至32字节的倍数;IV初始向量大小为16字节,取AESKey前16字节
DATA lv_x_aes_key32 TYPE xstring.

DATA:lv_token TYPE string VALUE ‘QDG6eK’.

*msg_signature: 消息签名,用于验证请求是否来自企业微信(防止攻击者伪造)。
DATA lv_msg_signature  TYPE string VALUE ‘477715d11cdb4164915debcba66cb864d751f3e6’.

DATA:lv_aeskey_x        TYPE xstring.
DATA:lv_sort_str        TYPE string.
DATA ef_hashstring      TYPE string.
DATA ef_hashxstring     TYPE xstring.
DATA ef_hashb64string   TYPE string.
DATA ef_hashx           TYPE xstring.
DATA lv_msg_encrypt     TYPE string.
DATA lv_aes_msg_x       TYPE xstring.
DATA:lv_decrypt_after   TYPE string.
DATA:lv_decrypt_after_x TYPE xstring.

DATA:lv_iv   TYPE string,
lv_iv_x TYPE xstring.

lv_iv lv_encodingaeskey_43+0(16).

CLEAR ls_par.
ls_parname ‘token’.
ls_parvalue lv_token.
APPEND ls_par TO lt_par.

ls_parname ‘timestamp’.
ls_parvalue ‘1409659813’.
APPEND ls_par TO lt_par.

ls_parname ‘nonce’.
ls_parvalue ‘1372623149’.
APPEND ls_par TO lt_par.
ls_parname ‘echostr’.

ls_parvalue =
‘RypEvHKD8QQKFhvQ6QleEB4J58tiPdvo+rtK1I9qca6aM/wvqnLSV5zEPeusUiX5L5X/0lWfrf0QADHHhGd3QczcdCUpj911L3vg3W/sYYvuJTs3TUUkSUXxaccAS0qhxchrRYt66wiS’
&& ‘pGLYL42aM6A8dTT+6k4aSknmPj48kzJs8qLjvd4Xgpue06DOdnLxAUHzM6+kDZ+HMZfJYuR+LtwGc2hgf5gsijff0ekUNXZiqATP7PF5mZxZ3Izoun’
&& ‘1s4zG4LUMnvw2r+KqCKIw+3IQH03v+BCA9nMELNqbSf6tiWSrXJB3LAVGUcallcrw8V2t9EL4EhzJWrQUax5wLVMNS0+rUPA3k22Ncx4XXZS9o0MBH2’
&& ‘7Bo6BpNelZpS+/uh9KsNlY6bHCmJU9p8g7m3fVKn28H3KDYA5Pl/T8Z1ptDAVe0lXdQ2YoyyH2uyPIGHBZZIs2pDBS8R07+qN+E7Q==’
.
APPEND ls_par TO lt_par.

SORT lt_par BY value.

“拼接为一个字符串
CLEAR lv_sort_str.
LOOP AT lt_par INTO ls_par.
lv_sort_str lv_sort_str &&  ls_parvalue.
ENDLOOP.

DATA lv_hash      TYPE hash160.
DATA lv_hashlen   TYPE .
DATA lv_hashx     TYPE hash160x.
DATA lv_hashxlen  TYPE i.
DATA lv_len       TYPE i.

DATA lv_aes TYPE string.

PERFORM frm_x2string USING ‘乱码’ ‘1E00’ CHANGING lv_cr.
TRY.
cl_abap_message_digest=>calculate_hash_for_char(
EXPORTING
if_algorithm     ‘SHA1’
if_data          lv_sort_str
if_length        0
IMPORTING
ef_hashstring    ef_hashstring
ef_hashxstring    ef_hashxstring
ef_hashb64string ef_hashb64string
ef_hashx         ef_hashx
).
DATA lv_sign_calcu TYPE string.
lv_sign_calcu ef_hashstring.
TRANSLATE lv_sign_calcu TO LOWER CASE.
IF lv_msg_signature <> lv_sign_calcu.
WRITE‘Error:签名不正确’lv_msg_signature.
RETURN.

ENDIF.
CATCH cx_abap_message_digest .
ENDTRY.
*—————————————————————————签名验证

“对密文进行解码
READ TABLE lt_par INTO ls_par WITH KEY name ‘echostr’.
IF sysubrc 0.
lv_msg_encrypt ls_parvalue.
ENDIF.
DATA lv_str TYPE string.

CALL FUNCTION ‘SCMS_BASE64_DECODE_STR’
EXPORTING
input  lv_msg_encrypt
*   UNESCAPE       = ‘X’
IMPORTING
output lv_aes_msg_x
*       EXCEPTIONS
*   FAILED = 1
*   OTHERS = 2
.
WRITE:/.
WRITE:‘已知明文内容:’.
PERFORM frm_write USING lv_msg_encrypt.

WRITE:/.
WRITE:/.
PERFORM frm_x2string USING  ‘SCMS_BASE64_DECODE_STR已经被企业微信加密的内容’ lv_aes_msg_x CHANGING lv_str.

CALL FUNCTION ‘SCMS_STRING_TO_XSTRING’
EXPORTING
text     lv_encodingaeskey_43
*   MIMETYPE = ‘ ‘
encoding ‘8400’
IMPORTING
buffer   lv_aeskey_x
EXCEPTIONS
failed   1
OTHERS   2.
IF sysubrc <> 0.
ENDIF.

CALL FUNCTION ‘SCMS_BASE64_DECODE_STR’
EXPORTING
input  lv_encodingaeskey_43
*   UNESCAPE       = ‘X’
IMPORTING
output lv_x_aes_key32
*       EXCEPTIONS
*   FAILED = 1
*   OTHERS = 2
.
WRITE:/.
WRITE:/.
WRITE:‘lv_x_aes_key32’,lv_x_aes_key32.
PERFORM frm_x2string USING  ‘SCMS_BASE64_DECODE_STR后lv_x_aes_key32的内容’ lv_x_aes_key32 CHANGING lv_str.

CALL FUNCTION ‘SCMS_STRING_TO_XSTRING’
EXPORTING
text     lv_iv
*   MIMETYPE = ‘ ‘
encoding ‘8400’
IMPORTING
buffer   lv_iv_x
EXCEPTIONS
failed   1
OTHERS   2.
IF sysubrc <> 0.
* Implement suitable error handling here
ENDIF.
WRITE:/.
WRITE:/.
WRITE:‘lv_iv’,lv_iv.
WRITE:‘lv_iv_x’,lv_iv_x.
PERFORM frm_x2string USING  ‘SCMS_STRING_TO_XSTRING后lv_iv_x的内容’ lv_iv_x CHANGING lv_str.

PERFORM frm_wechat_aes USING  lv_x_aes_key32  lv_aes_msg_x lv_iv_x
CHANGING lv_decrypt_after_x.

DATA lv_res TYPE string.
WRITE:/.
WRITE:/.
PERFORM frm_x2string USING ‘XML解密去乱码后结果:’ lv_decrypt_after_x CHANGING lv_res.

*&———————————————————————*
*& Form FRM_X2STRING
*&———————————————————————*
*& XSTR转换成STRING。
*&———————————————————————*
FORM frm_x2string  USING    uv_text uv_xstr TYPE xstring
CHANGING cv_result TYPE string.

DATA lt_bin TYPE STANDARD TABLE OF char20.
DATA lv_i TYPE i.
CALL FUNCTION ‘SCMS_XSTRING_TO_BINARY’
EXPORTING
buffer        uv_xstr
*     APPEND_TO_TABLE       = ‘ ‘
IMPORTING
output_length lv_i
TABLES
binary_tab    lt_bin.
DATA lv_tmp_str TYPE string.
CALL FUNCTION ‘SCMS_BINARY_TO_STRING’
EXPORTING
input_length lv_i
*     FIRST_LINE   = 0
*     LAST_LINE    = 0
*     MIMETYPE     = ‘ ‘
encoding     ‘8400’
IMPORTING
text_buffer  lv_tmp_str
*     OUTPUT_LENGTH       =
TABLES
binary_tab   lt_bin[]
* EXCEPTIONS
*     FAILED       = 1
*     OTHERS       = 2
.
IF sysubrc <> 0.
* Implement suitable error handling here
ENDIF.
cv_result lv_tmp_str.
WRITE:‘======================’,uv_text.

PERFORM frm_write USING cv_result.

ENDFORM.

*&———————————————————————*
*& Form FRM_WRITE
*&———————————————————————*
*& 格式化输出。
*&———————————————————————*
FORM frm_write  USING    cv_result.

DATA lv_c TYPE LENGTH 9000.
lv_c =  cv_result.
DATA lv_c200 TYPE LENGTH 100.
DATA lv_tab  TYPE VALUE cl_abap_char_utilities=>newline .
DATA lv_tab2 TYPE VALUE cl_abap_char_utilities=>cr_lf .
DATA lv_tab3 TYPE c  .

lv_tab3  lv_cr.

DO 200 TIMES.
IF  lv_c IS INITIAL.
EXIT.
ENDIF.
lv_c200 lv_c.
REPLACE ALL OCCURRENCES OF lv_tab IN lv_c200 WITH space.
REPLACE ALL OCCURRENCES OF lv_tab2 IN lv_c200 WITH space.
REPLACE ALL OCCURRENCES OF lv_tab3 IN lv_c200 WITH space.
WRITE:/ lv_c200.
SHIFT lv_c LEFT BY 100 PLACES.

ENDDO.

ENDFORM.

我的微信公众号【 hot583 】 或者在这个链接联系我

本文链接 http://www.hot583.com/2022/11/12/corporation-wechat-with-sap-abap-interface-aes-decode-encode-experience

本文为作者原创,如果这篇文章对您有用,欢迎转载,

转载请务必注明来源《今日热点在看》http://www.hot583.com/

微信原文《全文完》

今日热点在看SAP HOT583