SET serveroutput on;declare input_string VARCHAR2 (32): = ' 12345678 '; Ikey INTEGER: = 100001; Input_raw Raw (+): = 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 ========= '); --Encryption 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); --Decrypt 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 DECR Yption successful '); END IF; end;/
Shown below:
> ========= 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
Oracle 3DES Encryption Example