Oracle 3DES加密示例
SET SERVEROUTPUT ON; DECLARE input_string VARCHAR2(32) := '12345678'; ikey INTEGER := 100001; input_raw RAW(32) := UTL_RAW.CAST_TO_RAW(input_string); key_raw RAW(32); encrypted_raw RAW(32); encry_key VARCHAR2(32); decrypted_raw RAW(32); BEGIN key_raw := utl_raw.cast_from_binary_integer(ikey) || utl_raw.bit_complement(utl_raw.cast_from_binary_integer(ikey)); key_raw := key_raw || utl_raw.bit_xor(key_raw, hextoraw('F0F0F0F0F0F0F0F0')); dbms_output.put_line('> ========= Get Key Bytes ========='); dbms_output.put_line('> Input String: ' || input_string); dbms_output.put_line('> Key String: ' || key_raw); dbms_output.put_line('> Key length: ' || UTL_RAW.length(key_raw)); dbms_output.put_line('> ========= BEGIN TEST Encrypt ========='); --加密 encrypted_raw := dbms_obfuscation_toolkit.DES3Encrypt(input => input_raw, key => key_raw, iv => hextoraw('0000000000000000')); dbms_output.put_line('> encrypted_raw output: ' || encrypted_raw); encry_key := utl_raw.cast_to_varchar2(utl_encode.base64_encode(encrypted_raw)); dbms_output.put_line('> encry_key output: ' || encry_key); --解密 decrypted_raw := dbms_obfuscation_toolkit.DES3Decrypt(input => encrypted_raw, key => key_raw, iv => hextoraw('0000000000000000')); dbms_output.put_line('> decrypted_raw output: ' || utl_raw.cast_to_varchar2(decrypted_raw)); IF input_string = utl_raw.cast_to_varchar2(decrypted_raw) THEN dbms_output.put_line('> String DES Encyption and Decryption successful'); END IF; END; /
显示如下:
> ========= Get Key Bytes =========
> Input String: 12345678
> Key String: 000186A1FFFE795EF0F176510F0E89AE
> Key length: 16
> ========= BEGIN TEST Encrypt =========
> encrypted_raw output: 9D2A9569D15A07DB
> encry_key output: nSqVadFaB9s=
> decrypted_raw output: 12345678
> String DES Encyption and Decryption successful
PL/SQL procedure successfully completed