一、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中对应的加密、解密方法
1public class Utils { 2 3#密钥 4private static String key = "test#5&124*!de"; 5 6 /** 7 * 加密 8 * @param inStr 9 * @return 10 */ 11 public static String ENCRYPT_DES(String inStr) { 12 DESKeySpec desKey; 13 SecretKey securekey; 14 Cipher cipher; 15 try { 16 17 desKey = new DESKeySpec(key.getBytes()); 18 securekey = SecretKeyFactory.getInstance("DES").generateSecret(desKey); 19 cipher = Cipher.getInstance("DES/CBC/NoPadding"); 20 cipher.init(Cipher.ENCRYPT_MODE, securekey, new IvParameterSpec(new byte[8])); 21 byte[] inBytes = new byte[((int) (inStr.length() / 8) + 1) * 8]; 22 for (int i = 0; i < inStr.length(); i++) { 23 inBytes[i] = inStr.getBytes()[i]; 24 } 25 byte[] enBytes = cipher.doFinal(inBytes); 26 String hexStr = DatatypeConverter.printHexBinary(enBytes); 27 return hexStr; 28 29 } catch (Exception e) { 30 // TODO Auto-generated catch block 31 e.printStackTrace(); 32 } 33 34 return null; 35 36 } 37 38 /** 39 * 解密 40 * @param encryptStr 41 * @return 42 */ 43 public static String DECRYPT_DES(String encryptStr) { 44 DESKeySpec desKey; 45 SecretKey securekey; 46 Cipher cipher; 47 try { 48 desKey = new DESKeySpec(key.getBytes()); 49 securekey = SecretKeyFactory.getInstance("DES").generateSecret(desKey); 50 cipher = Cipher.getInstance("DES/CBC/NoPadding"); 51 cipher.init(Cipher.DECRYPT_MODE, securekey, new IvParameterSpec(new byte[8])); 52 byte[] decryptBytes = cipher.doFinal(Hex.decodeHex(encryptStr.toCharArray())); 53 return new String(decryptBytes).trim(); 54 55 } catch (Exception e) { 56 // TODO Auto-generated catch block 57 e.printStackTrace(); 58 } 59 60 return null; 61 62 } 63 64 public static void main(String[] args) { 65 System.out.println("加密:"+ENCRYPT_DES("18693157906")); 66 System.out.println("解密:"+DECRYPT_DES("FAD42A3BB2A4B9A5B36847714A56FE65")); 67} 68 69 70}
三、运行代码得到结果

可以看到加密后的密文是FAD42A3BB2A4B9A5B36847714A56FE65
解密后的明文是18693157906
跟数据库加密一致