1. 程式人生 > >oracle 9i 使用dbms_obfuscation_toolkit加密解密

oracle 9i 使用dbms_obfuscation_toolkit加密解密

create or replace package body PG_ENCRYPT_DECRYPT is
  function getkey return varchar2 is
    v_type varchar2(1) := '';
    v_rtn  varchar2(40);
    v_key  varchar2(20) := 'AAAA1234';
  begin
    if (v_type = '1') then
      select to_char(sysdate, 'yyyy') into v_key from dual;
    elsif (v_type = '2') then
      select to_char(sysdate, 'yyyyQ') into v_key from dual;
    elsif (v_type = '3') then
      select to_char(sysdate, 'yyyymm') into v_key from dual;
    elsif (v_type = '4') then
      select to_char(sysdate, 'yyyyWW') into v_key from dual;
    elsif (v_type = '5') then
      select to_char(sysdate, 'yyyymmdd') into v_key from dual;
    end if;
    for i in 1 .. length(v_key) loop
      v_rtn := v_rtn || to_char(ascii(substr(v_key, i, 1)));
    end loop;
    return v_rtn;
  end;
  /* Creating function DECRYPT*/
  function encrypt(p_in varchar2) return varchar2 is
    v_in  varchar2(320);
    v_out varchar2(1000);
  begin
    v_in  := rpad(p_in, (trunc(length(p_in) / 8) + 1) * 16, chr(0));
    v_out := UTL_RAW.CAST_TO_RAW(dbms_obfuscation_toolkit.desencrypt(input_string => v_in,
                                                                     key_string   => getkey()));
    return v_out;
  end;
  /*Creating function ENCRYPT*/
  function decrypt(p_in varchar2) return varchar2 is
    v_out varchar2(1000);
  begin
    dbms_obfuscation_toolkit.DESDECRYPT(input_string     => UTL_RAW.CAST_TO_varchar2(p_in),
                                        key_string       => getkey(),
                                        decrypted_string => v_out);
    v_out := rtrim(v_out, chr(0));
    return v_out;
  end;