ABAP字符串的加密与解密

FIEB_PASSWORD_DECRYPT:字符串解密;
FIEB_PASSWORD_ENCRYPT:字符串加密。
旧版本的可以用。

 1 PARAMETERS:str1 type char32 OBLIGATORY,
 2             str2 TYPE char32,
 3             str3 TYPE char32.
 4 INITIALIZATION.
 5 clear:str2,str3.
 6 AT SELECTION-SCREEN output.
 7   if not str1 is INITIAL.
 8   CALL FUNCTION 'FIEB_PASSWORD_ENCRYPT'
 9     EXPORTING
10        im_decrypted_password        = STR1
11    IMPORTING
12       EX_ENCRYPTED_PASSWORD         = STR2 .
13   CALL FUNCTION 'FIEB_PASSWORD_DECRYPT'
14     EXPORTING
15        im_encrypted_password        = STR2
16    IMPORTING
17       EX_DECRYPTED_PASSWORD         = STR3   .
18 endif.


新版中可以使用类接口CL_HARD_WIRED_ENCRYPTOR的静态方法实现。

 1 DATA:   o_encryptor        TYPE REF TO cl_hard_wired_encryptor,
 2         o_cx_encrypt_error TYPE REF TO cx_encrypt_error.
 3 DATA:   v_ac_string  TYPE string VALUE 'Welcome to ABAP',
 4         v_ac_xstring TYPE xstring,
 5         v_en_string  TYPE string,
 6         v_en_xstring TYPE xstring,
 7         v_de_string  TYPE string,
 8         v_de_xstring TYPE xstring,
 9         v_error_msg  TYPE string.
10 START-OF-SELECTION.
11   CREATE OBJECT o_encryptor.
12   WRITE / 'Encryption - String to String'.
13   TRY.
14       CALL METHOD o_encryptor->encrypt_string2string
15         EXPORTING
16             the_string = v_ac_string
17           RECEIVING
18             result       = v_en_string.
19     CATCH cx_encrypt_error INTO o_cx_encrypt_error.
20       CALL METHOD o_cx_encrypt_error->if_message~get_text
21           RECEIVING
22             result = v_error_msg.
23       MESSAGE v_error_msg TYPE 'E'.
24   ENDTRY.
25   WRITE:/ 'Actual String: ', v_ac_string.
26   WRITE:/ 'Encrypted String: ', v_en_string.
27   SKIP.
28   WRITE / 'Decryption - String to String'.
29   TRY.
30       CALL METHOD o_encryptor->decrypt_string2string
31         EXPORTING
32             the_string = v_en_string
33           RECEIVING
34             result       = v_de_string.
35     CATCH cx_encrypt_error INTO o_cx_encrypt_error.
36       CALL METHOD o_cx_encrypt_error->if_message~get_text
37           RECEIVING
38             result = v_error_msg.
39       MESSAGE v_error_msg TYPE 'E'.
40   ENDTRY.
41 
42   WRITE:/ 'Encrypted String: ', v_en_string.
43   WRITE:/ 'Decrypted String: ', v_de_string.
44   SKIP. 

-----------------------------------------------------------------------------

 1 report ztest1 MESSAGE-ID F4.
 2 DATA: o_encryptor        TYPE REF TO cl_hard_wired_encryptor,
 3       o_cx_encrypt_error TYPE REF TO cx_encrypt_error.
 4 
 5 DATA: v_ac_string  TYPE string VALUE 'Welcome to ABAP',
 6       v_ac_xstring TYPE xstring,
 7       v_en_string  TYPE string,
 8       v_en_xstring TYPE xstring,
 9       v_de_string  TYPE string,
10       v_de_xstring TYPE string,
11       v_error_msg  TYPE string.
12 
13 START-OF-SELECTION.
14 
15 * Create object for Encryption
16   CREATE OBJECT o_encryptor.
17 * *&---------------------------------------------------------------------*
18 * Encryption - String to String
19 *&---------------------------------------------------------------------*
20 WRITE / 'Encryption - String to String'.
21   TRY.
22       CALL METHOD o_encryptor->encrypt_string2string
23         EXPORTING
24           the_string = v_ac_string
25         RECEIVING
26           result     = v_en_string.
27     CATCH cx_encrypt_error INTO o_cx_encrypt_error.
28       CALL METHOD o_cx_encrypt_error->if_message~get_text
29         RECEIVING
30           result = v_error_msg.
31       MESSAGE v_error_msg TYPE 'E'.
32   ENDTRY.
33 
34   WRITE:/ 'Actual String: ', v_ac_string.
35   WRITE:/ 'Encrypted String: ', v_en_string.
36   SKIP.
37 
38 *&---------------------------------------------------------------------*
39 * Decryption - String to String
40 *&---------------------------------------------------------------------*
41 WRITE / 'Decryption - String to String'.
42   TRY.
43       CALL METHOD o_encryptor->decrypt_string2string
44         EXPORTING
45           the_string = v_en_string
46         RECEIVING
47           result     = v_de_string.
48     CATCH cx_encrypt_error INTO o_cx_encrypt_error.
49       CALL METHOD o_cx_encrypt_error->if_message~get_text
50         RECEIVING
51           result = v_error_msg.
52       MESSAGE v_error_msg TYPE 'E'.
53   ENDTRY.
54 
55   WRITE:/ 'Encrypted String: ', v_en_string.
56   WRITE:/ 'Decrypted String: ', v_de_string.

 

posted @ 2017-01-17 13:32  明大叔  阅读(1216)  评论(0编辑  收藏  举报