Oracle PL/SQL/System Packages/DBMS OBFUSCATION TOOLKIT

Материал из SQL эксперт
Перейти к: навигация, поиск

DBMS_OBFUSCATION_TOOLKIT.des3decrypt

  
SQL>
SQL> CREATE OR REPLACE FUNCTION get_dec_val (
  2     p_in_val   VARCHAR2,
  3     p_key      VARCHAR2,
  4     p_iv       VARCHAR2 := NULL,
  5     p_which    NUMBER := 0
  6  )
  7     RETURN VARCHAR2
  8  IS
  9     l_dec_val   VARCHAR2 (2000);
 10     l_iv        VARCHAR2 (2000);
 11  BEGIN
 12     IF p_which = 0
 13     THEN
 14        IF LENGTH (p_key) < 16
 15        THEN
 16           raise_application_error(-20001,"Key length less than 16 for two-pass scheme");
 17        END IF;
 18     ELSIF p_which = 1
 19     THEN
 20        IF LENGTH (p_key) < 24
 21        THEN raise_application_error(-20002,"Key length less than 24 for three-pass scheme");
 22        END IF;
 23     ELSE
 24        raise_application_error (-20003,"Incorrect value of which "|| p_which|| "; must be 0 or 1");
 25     END IF;
 26
 27     l_iv := RPAD (p_iv, (8 * ROUND (LENGTH (p_iv) / 8, 0) + 8));
 28     l_dec_val :=
 29        DBMS_OBFUSCATION_TOOLKIT.des3decrypt(input_string      => UTL_RAW.cast_to_varchar2(HEXTORAW (p_in_val)),key_string => p_key,iv_string => l_iv,which => p_which);
 30     RETURN RTRIM (l_dec_val);
 31  END;
 32  /

Function created.
SQL>



DBMS_OBFUSCATION_TOOLKIT.des3encrypt

  
SQL> CREATE OR REPLACE FUNCTION get_enc_val (
  2     p_in_val   IN   VARCHAR2,
  3     p_key      IN   VARCHAR2,
  4     p_iv       IN   VARCHAR2 := NULL,
  5     p_which    IN   NUMBER := 0
  6  )
  7     RETURN VARCHAR2
  8  IS
  9     l_enc_val   VARCHAR2 (200);
 10     l_in_val    VARCHAR2 (200);
 11     l_iv        VARCHAR2 (200);
 12  BEGIN
 13     IF p_which = 0
 14     THEN
 15        IF LENGTH (p_key) < 16
 16        THEN
 17           raise_application_error
 18                                 (-20001,
 19                                  "Key length less than 16 for two-pass scheme"
 20                                 );
 21        END IF;
 22     ELSIF p_which = 1
 23     THEN
 24        IF LENGTH (p_key) < 24
 25        THEN
 26           raise_application_error
 27                               (-20002,
 28                                "Key length less than 24 for three-pass scheme"
 29                               );
 30        END IF;
 31     ELSE
 32        raise_application_error (-20003,
 33                                    "Incorrect value of which "
 34                                 || p_which
 35                                 || "; must be 0 or 1"
 36                                );
 37     END IF;
 38
 39     l_in_val := RPAD (p_in_val, (8 * ROUND (LENGTH (p_in_val) / 8, 0) + 8));
 40     l_iv := RPAD (p_iv, (8 * ROUND (LENGTH (p_iv) / 8, 0) + 8));
 41     l_enc_val :=
 42        DBMS_OBFUSCATION_TOOLKIT.des3encrypt (input_string      => l_in_val,
 43                                              key_string        => p_key,
 44                                              iv_string         => l_iv,
 45                                              which             => p_which
 46                                             );
 47     l_enc_val := RAWTOHEX (UTL_RAW.cast_to_raw (l_enc_val));
 48     RETURN l_enc_val;
 49  END;
 50  /

Function created.
SQL>



DBMS_OBFUSCATION_TOOLKIT.des3getkey

  
SQL> CREATE OR REPLACE FUNCTION get_key (
  2     p_seed    VARCHAR2 :=    "1234567890"
  3                           || "1234567890"
  4                           || "1234567890"
  5                           || "1234567890"
  6                           || "1234567890"
  7                           || "1234567890"
  8                           || "1234567890"
  9                           || "1234567890",
 10     p_which   NUMBER := 0
 11  )
 12     RETURN VARCHAR2
 13  IS
 14     l_seed   VARCHAR2 (80);
 15     l_ret    VARCHAR2 (2000);
 16  BEGIN
 17     l_seed := RPAD (p_seed, 80);
 18     l_ret := DBMS_OBFUSCATION_TOOLKIT.des3getkey (seed_string=> l_seed,which => p_which);
 19     l_ret := RAWTOHEX (UTL_RAW.cast_to_raw (l_ret));
 20     RETURN l_ret;
 21  END;
 22  /
Function created.
SQL>



dbms_obfuscation_toolkit.md5

  
SQL>
SQL>
SQL> set serveroutput on
SQL>     declare
  2          l_md varchar2(16);
  3      begin
  4          l_md := dbms_obfuscation_toolkit.md5(input_string => "911" );
  5          dbms_output.put_line( "MD5 of 911: " ||utl_raw.cast_to_raw( l_md ));
  6          --
  7          l_md := dbms_obfuscation_toolkit.md5(input_string => "411" );
  8          dbms_output.put_line( "MD5 of 411: " || utl_raw.cast_to_raw( l_md ));
  9    end;
 10   /
MD5 of 911: B56A18E0EACDF51AA2A5306B0F533204
MD5 of 411: 17D63B1625C816C22647A73E1482372B
PL/SQL procedure successfully completed.
SQL>
SQL>



DBMS_OBFUSCATION_TOOLKIT.md5 and RAWTOHEX

  
SQL>
SQL> CREATE OR REPLACE FUNCTION get_hash_val (p_in VARCHAR2)
  2     RETURN VARCHAR2
  3  IS
  4     l_hash   VARCHAR2 (2000);
  5  BEGIN
  6     l_hash :=RAWTOHEX(UTL_RAW.cast_to_raw(DBMS_OBFUSCATION_TOOLKIT.md5 (input_string=> p_in)));
  7     RETURN l_hash;
  8  END;
  9  /
Function created.
SQL>
SQL> select get_hash_val("12345") from dual;
GET_HASH_VAL("12345")
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
827CCB0EEA8A706C4C34A16891F84E7B
SQL>



Demonstrate DES3 encryption

  
SQL>
SQL> DECLARE
  2   input_string        VARCHAR2(160) := "password01";
  3   key_string          VARCHAR2(24);
  4
  5   encrypted_string            VARCHAR2(2048);
  6   decrypted_string            VARCHAR2(2048);
  7     error_in_input_buffer_length EXCEPTION;
  8     PRAGMA EXCEPTION_INIT(error_in_input_buffer_length, -28232);
  9     INPUT_BUFFER_LENGTH_ERR_MSG VARCHAR2(100) := "              ";
 10
 11  BEGIN
 12        input_string := input_string||input_string||input_string||input_string||input_string||input_string||input_string||input_string;
 13        dbms_output.put_line("input string              : " || input_string);
 14        dbms_obfuscation_toolkit.DES3GETKEY(which=>1, seed_string=>input_string,key=>key_string);
 15        dbms_obfuscation_toolkit.DESENCRYPT(input_string => input_string,key_string => key_string, encrypted_string => encrypted_string );
 16        dbms_output.put_line("encrypted string              : " ||encrypted_string);
 17        dbms_obfuscation_toolkit.DESDecrypt(input_string => encrypted_string,key_string => key_string, decrypted_string => decrypted_string);
 18        dbms_output.put_line("Decrypted output             : " ||decrypted_string);
 19        if input_string = decrypted_string THEN
 20           dbms_output.put_line("> DES Encryption and Decryption successful");
 21        END if;
 22     EXCEPTION
 23        WHEN error_in_input_buffer_length THEN dbms_output.put_line("> " || INPUT_BUFFER_LENGTH_ERR_MSG);
 24     END;
 25
 26  /

PL/SQL procedure successfully completed.
SQL>