一、oracle中的加密函数encrypt_des
create or replace function encrypt_des(vi_data varchar2) return varchar2 is --加密 vr_data varchar2(4000); vr_enc varchar2(4000); raw_input RAW(128); key_input RAW(128); decrypted_raw RAW(2048); vr_key varchar2(64); begin if vi_data is null then return null; end if; select MOBILEKEY into vr_key from CENKEY; vr_data := rpad(vi_data, (trunc(length(vi_data) / 8) + 1) * 8, chr(0)); raw_input := UTL_RAW.CAST_TO_RAW(vr_data); key_input := UTL_RAW.CAST_TO_RAW(vr_key); dbms_obfuscation_toolkit.DESEncrypt(input => raw_input, key => key_input, encrypted_data => decrypted_raw); vr_enc := rawtohex(decrypted_raw); dbms_output.put_line(vr_enc);
return vr_enc; end;
下图是加密后的结果
将18693157906加密后的密文是 FAD42A3BB2A4B9A5B36847714A56FE65
二、java中对应的加密、解密方法
public class Utils {
#密钥
private static String key = "test#5&124*!de";
/**
* 加密
* @param inStr
* @return
*/
public static String ENCRYPT_DES(String inStr) {
DESKeySpec desKey;
SecretKey securekey;
Cipher cipher;
try {
desKey = new DESKeySpec(key.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) (inStr.length() / 8) + 1) * 8];
for (int i = 0; i < inStr.length(); i++) {
inBytes[i] = inStr.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 encryptStr
* @return
*/
public static String DECRYPT_DES(String encryptStr) {
DESKeySpec desKey;
SecretKey securekey;
Cipher cipher;
try {
desKey = new DESKeySpec(key.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(encryptStr.toCharArray()));
return new String(decryptBytes).trim();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
public static void main(String[] args) {
System.out.println("加密:"+ENCRYPT_DES("18693157906"));
System.out.println("解密:"+DECRYPT_DES("FAD42A3BB2A4B9A5B36847714A56FE65"));
}
}
三、运行代码得到结果
可以看到加密后的密文是FAD42A3BB2A4B9A5B36847714A56FE65
解密后的明文是18693157906
跟数据库加密一致