在oracle 11g中使用javasource
首先定义javaSource
create or replace and compile java source named "Encrypt1" as
import java.security.SecureRandom;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
public class Encrypt1 {
private static final byte[] DES_KEY = { 21, 1, -110, 82, -32, -85, -128, -65 };
public static String decryptBasedDes(String cryptData) {
String decryptedData = null;
try {
// DES算法要求有一个可信任的随机数源
SecureRandom sr = new SecureRandom();
DESKeySpec deskey = new DESKeySpec(DES_KEY);
// 创建一个密匙工厂,然后用它把DESKeySpec转换成一个SecretKey对象
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
SecretKey key = keyFactory.generateSecret(deskey);
// 解密对象
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.DECRYPT_MODE, key, sr);
// 把字符串解码为字节数组,并解密
decryptedData = new String(cipher.doFinal(new sun.misc.BASE64Decoder().decodeBuffer(cryptData)));
} catch (Exception e) {
throw new RuntimeException("解密错误,错误信息:", e);
}
return decryptedData;
}
}
定义function
CREATE OR REPLACE FUNCTION Encrypt11 (cryptData IN STRING)
RETURN String
IS
LANGUAGE JAVA
NAME 'Encrypt1.decryptBasedDes(java.lang.String) return String';
调用:
declare abc String(50);
begin
abc:=Encrypt11('aT3dGMPB2FEMba0K881Jmg==');
dbms_output.put_line(abc);
end;