ABAP-AES加密解密

  1 REPORT ytestdzp.
  2 
  3 FORM b .
  4 
  5   DATA:lv_encrypt TYPE string.     "加密前的string类型
  6   DATA:lv_encrypt_x TYPE xstring.  "加密前的xstring类型
  7 
  8   DATA:lv_encrypt_after TYPE string.    "加密后的string类型
  9   DATA:lv_encrypt_after_x TYPE xstring. "加密后的xstring类型
 10 
 11   DATA:lv_key TYPE string VALUE 'y5red17ocg5voImq', "key  密钥
 12        lv_iv  TYPE string VALUE '0000000000000000'. "iv   向量
 13 
 14   DATA : lv_key_x TYPE xstring.
 15   DATA : lv_iv_x  TYPE xstring.
 16 
 17   lv_encrypt = '5d1ceafcbd05470ca2fe969bed2e6151'.  "加密前的数据
 18 
 19   "string --> xstring
 20   CALL FUNCTION 'SCMS_STRING_TO_XSTRING'
 21     EXPORTING
 22       text   = lv_key
 23     IMPORTING
 24       buffer = lv_key_x
 25     EXCEPTIONS
 26       failed = 1.
 27 
 28   "string --> xstring
 29   CALL FUNCTION 'SCMS_STRING_TO_XSTRING'
 30     EXPORTING
 31       text   = lv_iv
 32     IMPORTING
 33       buffer = lv_iv_x
 34     EXCEPTIONS
 35       failed = 1.
 36 
 37   "string --> xstring
 38   CALL FUNCTION 'SCMS_STRING_TO_XSTRING'
 39     EXPORTING
 40       text   = lv_encrypt
 41     IMPORTING
 42       buffer = lv_encrypt_x
 43     EXCEPTIONS
 44       failed = 1.
 45 
 46   " encrypt using AES256
 47   zcl_aes_utility=>encrypt_xstring(
 48     EXPORTING
 49       i_key                   = lv_key_x
 50       i_data                  = lv_encrypt_x
 51       i_initialization_vector = lv_iv_x
 52       i_padding_standard      = zcl_byte_padding_utility=>mc_padding_standard_pkcs_7 "字节填充模式
 53       i_encryption_mode       = zcl_aes_utility=>mc_encryption_mode_cbc
 54     IMPORTING
 55       e_data                  = lv_encrypt_after_x
 56   ).
 57 
 58   WRITE:'加密前:', lv_encrypt.
 59 
 60   CALL FUNCTION 'SCMS_BASE64_ENCODE_STR'
 61     EXPORTING
 62       input  = lv_encrypt_after_x
 63     IMPORTING
 64       output = lv_encrypt_after.
 65 
 66   WRITE:/ '加密后:', lv_encrypt_after.
 67 
 68 ENDFORM .
 69 
 70 FORM a .
 71 
 72   DATA:lv_decrypt   TYPE string,      "解密前的string类型
 73        lv_decrypt_x TYPE xstring.     "解密前的xstring类型
 74 
 75   DATA:lv_decrypt_after   TYPE string,   "解密后的string类型
 76        lv_decrypt_after_x TYPE xstring.  "解密后的xstring类型
 77 
 78   DATA:lv_key TYPE string VALUE 'IGcHiC4QUCXUlOx0', "key 设置对称算法的密钥
 79        lv_iv  TYPE string VALUE '0000000000000000'. "iv  设置对称算法的初始化向量
 80 
 81   DATA lv_key_x TYPE xstring .
 82   DATA lv_iv_x  TYPE xstring .
 83 
 84 
 85   lv_decrypt = 'GQ3225d7mwmj3pbHoBRsL5piGEdiYWy9pB9YwpkLkNdKMuPOd5etReMf3hgES0zdCxGsHoOjCnPLru BgeXPz1tLEXGlp3' &&
 86   'q+Xk2K3Re+PMMs+qK1RAmpJpLB5w2pKMhqTKXeh2nKlGFgyHn6tgjRiAzwTM0nWM' &&
 87   'cYFoDJZPw6kJOHLEF40zv6FSfnk7NvFrG44TdF/B/NAR4FGOKC+AmJhtYxUnXlvkt3wDxqGjHML3//b B1xLWmluBEANj5NXveq+'.
 88 
 89   " base64 --> xstring
 90   CALL FUNCTION 'SSFC_BASE64_DECODE'
 91     EXPORTING
 92       b64data                  = lv_decrypt
 93     IMPORTING
 94       bindata                  = lv_decrypt_x
 95     EXCEPTIONS
 96       ssf_krn_error            = 1
 97       ssf_krn_noop             = 2
 98       ssf_krn_nomemory         = 3
 99       ssf_krn_opinv            = 4
100       ssf_krn_input_data_error = 5
101       ssf_krn_invalid_par      = 6
102       ssf_krn_invalid_parlen   = 7.
103 
104   "string --> xstring
105   CALL FUNCTION 'SCMS_STRING_TO_XSTRING'
106     EXPORTING
107       text   = lv_key
108     IMPORTING
109       buffer = lv_key_x
110     EXCEPTIONS
111       failed = 1.
112 
113   "string --> xstring
114   CALL FUNCTION 'SCMS_STRING_TO_XSTRING'
115     EXPORTING
116       text   = lv_iv
117     IMPORTING
118       buffer = lv_iv_x
119     EXCEPTIONS
120       failed = 1.
121 
122   " AES解密
123   zcl_aes_utility=>decrypt_xstring(
124   EXPORTING
125     i_key                   = lv_key_x
126     i_data                  = lv_decrypt_x
127     i_initialization_vector = lv_iv_x
128     i_encryption_mode       = zcl_aes_utility=>mc_encryption_mode_cbc
129     i_padding_standard = zcl_byte_padding_utility=>mc_padding_standard_pkcs_7  "字节填充模式
130   IMPORTING
131     e_data                  = lv_decrypt_after_x ).
132 
133   WRITE: / '解密前:' , lv_decrypt.
134 
135   "Xstring --> string
136   lv_decrypt_after = cl_abap_codepage=>convert_from(
137   EXPORTING
138     source = lv_decrypt_after_x
139   ).
140 
141   WRITE: / '解密后:' , lv_decrypt_after.
142 
143 ENDFORM .

使用前需要用ABAPGIT导入加密解密的类

点击下载

posted @ 2023-09-22 14:57  TigerMan  阅读(342)  评论(0编辑  收藏  举报