背景
接口开发中,上游系统需要加密数据,SAP负责解密,经过研究决定使用AES加密,不了解加密算法的可以查看
https://www.cnblogs.com/sochishun/p/7028056.html
原理
使用Github开源项目AES algorithm implementation by ABAP, 地址如下
https://github.com/Sumu-Ning/AES
貌似通过标准类CL_SEC_SXML_WRITER也能实现,我还没有测试,可参考
https://stackoverflow.com/questions/49981557/how-to-use-encryption-in-sap
步骤
通过abapgit安装项目代码,激活开发类如下
示例代码
解密
data key type xstring .
data iv type xstring .
data mode type zcl_aes_utility=>mty_encryption_mode .
data plaintext type xstring .
data ciphertext type xstring .
data test type xstring .
data:lv_key_string type string value '12345', "key
lv_iv_string type string value '12345'. "iv
mode = zcl_aes_utility=>mc_encryption_mode_cbc.
data:lv_decrypt type xstring,
lv_decrypted_xstring type xstring.
data(lv_envrypted_string) = 'O05w/bjAtEEYi/eyiz3Ieg=='.
" base64 --> xstring
call function 'SSFC_BASE64_DECODE'
exporting
b64data = conv string( lv_envrypted_string )
importing
bindata = lv_decrypted_xstring
exceptions
ssf_krn_error = 1
ssf_krn_noop = 2
ssf_krn_nomemory = 3
ssf_krn_opinv = 4
ssf_krn_input_data_error = 5
ssf_krn_invalid_par = 6
ssf_krn_invalid_parlen = 7.
"string --> xstring
call function 'SCMS_STRING_TO_XSTRING'
exporting
text = lv_key_string
importing
buffer = key
exceptions
failed = 1.
"string --> xstring
call function 'SCMS_STRING_TO_XSTRING'
exporting
text = lv_iv_string
importing
buffer = iv
exceptions
failed = 1.
" AES解密
zcl_aes_utility=>decrypt_xstring(
exporting
i_key = key
i_data = lv_decrypted_xstring
i_initialization_vector = iv
i_encryption_mode = mode
i_padding_standard = zcl_byte_padding_utility=>mc_padding_standard_pkcs_7 "字节填充模式
importing
e_data = lv_decrypt ).
write: / '解密前' , lv_envrypted_string.
"Xstring --> string
data(lv_decrypt_string) = cl_abap_codepage=>convert_from(
exporting
source = lv_decrypt
).
write: / '解密后' , lv_decrypt_string.