Home >Backend Development >PHP Tutorial >Oracle defines examples of DES encryption, decryption and MD5 encryption functions

Oracle defines examples of DES encryption, decryption and MD5 encryption functions

高洛峰
高洛峰Original
2017-01-24 11:26:552150browse

(1) DES encryption function

create or replace function
encrypt_des(p_text varchar2, p_key varchar2) return varchar2 is
v_text varchar2(4000);
v_enc varchar2(4000);
raw_input RAW(128) ;
key_input RAW(128) ;
decrypted_raw RAW(2048);
begin
v_text := rpad( p_text, (trunc(length(p_text)/8)+1)*8, chr(0));
raw_input := UTL_RAW.CAST_TO_RAW(v_text);
key_input := UTL_RAW.CAST_TO_RAW(p_key);
dbms_obfuscation_toolkit.DESEncrypt(input => raw_input,key => key_input,encrypted_data =>decrypted_raw);
v_enc := rawtohex(decrypted_raw);
dbms_output.put_line(v_enc);
return v_enc;
end;

(2) DES decryption function

create or replace function decrypt_des(p_text varchar2,p_key varchar2) return varchar2 is
v_text varchar2(2000);
begin
dbms_obfuscation_toolkit.DESDECRYPT(input_string => UTL_RAW.CAST_TO_varchar2(p_text),key_string =>p_key, decrypted_string=> v_text);
v_text := rtrim(v_text,chr(0));
dbms_output.put_line(v_text);
return v_text;
end;

(3) MD5 encryption function

CREATE OR REPLACE FUNCTION MD5(passwd IN VARCHAR2)
RETURN VARCHAR2
IS
retval varchar2(32);
BEGIN
retval := utl_raw.cast_to_raw(DBMS_OBFUSCATION_TOOLKIT.MD5(INPUT_STRING => passwd)) ;
RETURN retval;
END;

(4) Function usage example

DES encryption: update tb_salarysign_staff s set s.staffpwd =encrypt_des(s.staffpwd, 'test#5&124*!de');

DES decryption: select decrypt_des(s.staffpwd, 'test#5&124*!de') from tb_salarysign_staff s

MD5 encryption: update tb_salarysign_staff s set s.staffpwd = md5(s.staffpwd);

For more Oracle-defined DES encryption and decryption and MD5 encryption function examples, please pay attention to the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn