开发者

Oracle: How to call an overloaded procedure?

How to properly call DBMS_OBFUSCATION_TOOLKIT.DESEncrypt? (without using PL/SQL if possible)

select DBMS_OBFUSCATION_TOOLKIT.DESEncrypt('x','y') from dual;

doesn't work because DESEncrypt is overloaded:

ORA-06553: PLS-307: Too many declarations of "DESENCRYPT" match this call
06553. 00000 -  "PL开发者_Python百科S-%s: %s"
*Cause:    
*Action:

Is there a way to choose one implementation of DESENCRYPT (possibly the VARCHAR2 variant) to avoid this error?


In Oracle 11G you can use named notation like this:

select DBMS_OBFUSCATION_TOOLKIT.DESEncrypt(input_string=>'x',key_string=>'y')
from dual;

I don't think it is possible to call these functions unambiguously in earlier versions of Oracle, except by creating a wrapper function and calling that instead.


here you go, just let it know which overload to use by supplying the param names!

select DBMS_OBFUSCATION_TOOLKIT.DesEncrypt(INPUT_STRING=>'11112abc',KEY_STRING=>'4578ccde') 
from dual ;

returns

M5��w5Z

note, your key needs to be at least 8 bytes:

ORA-28234: key length too short ORA-06512: at "SYS.DBMS_OBFUSCATION_TOOLKIT_FFI", line 21 ORA-06512: at "SYS.DBMS_OBFUSCATION_TOOLKIT", line 126 28234. 00000 - "key length too short" *Cause: The key specified is too short for the algorithm. DES requires a key of at least 8 bytes. Triple DES requires a key of least 16 bytes in two-key mode and 24 bytes in three-key mode. *Action: Specify a longer key.


You may always try it with a wrapper function (as tony suggested)

create or replace
function DesEncrypt(pinputString IN VARCHAR2 , pKeyString in VARCHAR2) RETURN varchar2
IS
BEGIN
return DBMS_OBFUSCATION_TOOLKIT.DesEncrypt(INPUT_STRING=>INPUTSTRING,KEY_STRING=>KEYSTRING);
END DesEncrypt;
/
select DesEncrypt('11112abc' , '4578ccde') from dual ;

Since you are on 10g, you may want to use the DBMS_CRYPTO package http://www.stanford.edu/dept/itss/docs/oracle/10g/network.101/b10773/apdvncrp.htm


here's a crypt/decrypt using the older dbms_obfuscation_toolkit:

create or replace function crypt(p_str in varchar2, p_key in varchar2) return varchar2
as
  l_data varchar2(255);
begin
  l_data := rpad(p_str, (trunc(length(p_str)/8)+1)*8,chr(0));
  dbms_obfuscation_toolkit.DESEncrypt
  (input_string=>l_data,
  key_string=>p_key,
  encrypted_string=>l_data);

  return l_data;
end;

And for decrypt:

create or replace function decrypt(p_str in varchar2, p_key in varchar2) return varchar2
as
  l_data varchar2(255);
begin
  dbms_obfuscation_tookit.DESDecrypt
  (input_string=>p_str,
  key_string=>p_key,
  decrypted_string=>l_data);

  return rtrim(l_data,chr(0));
end;

And usage:

declare

  l_data varchar2(100);
  l_key varchar2(100);
  l_encrypted varchar2(100);
  l_decrypted varchar2(100);

begin
  l_data := 'This is secret!!!';
  l_key := 'My secret key';
  dbms_output.put_line(l_data);

  l_encrypted := crypt(l_data, l_key);
  dbms_output.put_line(l_encrypted);

  l_decrypted := decrypt(l_encrypted, l_key);
  dbms_output.put_line(l_decrypted);

end;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜