oracle DES加密解密包含Java DES加密解密
Posted 程序工厂
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了oracle DES加密解密包含Java DES加密解密相关的知识,希望对你有一定的参考价值。
oracle DES加密函数
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;
oracle DES解密函数
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;
Java DES加密解密
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.binary.Hex;
import javax.crypto.*;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.IvParameterSpec;
import javax.xml.bind.DatatypeConverter;
@Slf4j
public class DESUtil {
public static final String originKey = "密匙";
public static void main(String[] args) throws Exception {
// // 明文
String plainText = "测试";
log.info("明文---{}---" , plainText);
String a = encrypt(plainText);
log.info("加密---{}---" , a);
log.info("解密---{}---" , decryptValue(a));
}
/**
* 加密
* @param plainText
* @return
* @throws Exception
*/
public static String encrypt(String plainText) throws Exception{
DESKeySpec desKey;
SecretKey securekey;
Cipher cipher;
try {
desKey = new DESKeySpec(originKey.getBytes());
securekey = SecretKeyFactory.getInstance("DES").generateSecret(desKey);
cipher = Cipher.getInstance("DES/CBC/NoPadding");
cipher.init(Cipher.ENCRYPT_MODE, securekey, new IvParameterSpec(new byte[8]));
byte[] inBytes = new byte[((int) (plainText.length() / 8) + 1) * 8];
for (int i = 0; i < plainText.length(); i++) {
inBytes[i] = plainText.getBytes()[i];
}
byte[] enBytes = cipher.doFinal(inBytes);
String hexStr = DatatypeConverter.printHexBinary(enBytes);
return hexStr;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
/**
* 解密
* @param encode
* @return
* @throws Exception
*/
public static String decryptValue(String encode){
DESKeySpec desKey;
SecretKey securekey;
Cipher cipher;
try {
desKey = new DESKeySpec(originKey.getBytes());
securekey = SecretKeyFactory.getInstance("DES").generateSecret(desKey);
cipher = Cipher.getInstance("DES/CBC/NoPadding");
cipher.init(Cipher.DECRYPT_MODE, securekey, new IvParameterSpec(new byte[8]));
byte[] decryptBytes = cipher.doFinal(Hex.decodeHex(encode.toCharArray()));
return new String(decryptBytes).trim();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}
以上是关于oracle DES加密解密包含Java DES加密解密的主要内容,如果未能解决你的问题,请参考以下文章