我想在数据库数据插入等操作中进行一些加密操作,对一些需要加密的字段加密后进行替换,请问要怎么进行sql语句解析并替换。如:“insert into table b 123”变成“insert into table HFHHJ 877878”,我不是问加密算法,是怎么解析sql语句替换要加密的字段,然后再执行sql语句。另外我想用Java实现。
oracle 有自带的函数encrypt加密 decrypt解密。insert into table b values 123 ===insert into table b values encrypt(123,'key')....解密 select decrypt(b,'key') from table...
不知道是不是你想要的。。
往上有很多加密解密的技术,你可以搜索下,当你需要加密的时候,可以用加密的方法将这个字段进行加密,然后在插入到数据库,当你需要解密的时候,可以从数据库中取出加密后的字段的值,然后用解密的方法还原数据:下面是我的加密解密的工具类,你可以参考下:
public class SecurityUtil {
private static Logger logger = LoggerFactory.getLogger(SecurityUtil.class);
// 默认24字节的密钥
private final static byte[] defaultKeyBytes = { 0x11, 0x22, 0x4F, 0x58,
(byte) 0x88, 0x10, 0x40, 0x38, 0x28, 0x25, 0x79, 0x51, (byte) 0xCB,
(byte) 0xDD, 0x55, 0x66, 0x77, 0x29, 0x74, (byte) 0x98, 0x30, 0x40,
0x36, (byte) 0xE2 };
// 定义 加密算法,可用 DES,DESede,Blowfish
private static final String Algorithm = "DESede";
/**
* 被加密的数据缓冲区(源)
*/
public static byte[] encryptMode(byte[] src) {
return encryptMode(defaultKeyBytes, src);
}
/**
* @param keybyte
* 加密密钥,长度为24字节
* @param src
* 被加密的数据缓冲区(源)
* @return
*/
public static byte[] encryptMode(byte[] keybyte, byte[] src) {
try {
// 生成密钥
SecretKey deskey = new SecretKeySpec(keybyte, Algorithm);
// 加密
Cipher c1 = Cipher.getInstance(Algorithm);
c1.init(Cipher.ENCRYPT_MODE, deskey);
return c1.doFinal(src);
} catch (java.security.NoSuchAlgorithmException e1) {
e1.printStackTrace();
} catch (javax.crypto.NoSuchPaddingException e2) {
e2.printStackTrace();
} catch (java.lang.Exception e3) {
e3.printStackTrace();
}
return null;
}
/**
* 加密后的缓冲区
*/
public static byte[] decryptMode(byte[] src) throws Exception {
return decryptMode(defaultKeyBytes, src);
}
/**
* @param keybyte
* 加密密钥,长度为24字节
* @param src
* 加密后的缓冲区
* @return
*/
public static byte[] decryptMode(byte[] keybyte, byte[] src) throws Exception {
// 生成密钥
SecretKey deskey = new SecretKeySpec(keybyte, Algorithm);
// 解密
Cipher c1 = Cipher.getInstance(Algorithm);
c1.init(Cipher.DECRYPT_MODE, deskey);
return c1.doFinal(src);
}
/**
* 字节流转换成十六进制字符串
*
* @param b
* @return
*/
public static String byte2hex(byte[] b) {
String hs = "";
String stmp = "";
for (Integer n = 0; n < b.length; n++) {
stmp = (java.lang.Integer.toHexString(b[n] & 0XFF));
if (stmp.length() == 1)
hs = hs + "0" + stmp;
else
hs = hs + stmp;
if (n < b.length - 1)
hs = hs + ":";
}
return hs.toUpperCase();
}
/**
* 十六进制字符串转换成字节流
*
* @param hex
* @return
*/
public static byte[] hex2byte(String hex) {
String[] hexCode = hex.split(":");
byte[] byteCode = new byte[hexCode.length];
for (int i = 0; i < byteCode.length; i++) {
byteCode[i] = (byte) Integer.parseInt(hexCode[i], 16);
}
return byteCode;
}
/**
* 加密
*/
public static String encrypt(String str) {
byte[] encoded = encryptMode(str.getBytes());
return byte2hex(encoded);
}
/**
* 解密
*/
public static String decrypt(String str){
byte[] encoded = hex2byte(str);
try {
encoded = decryptMode(encoded);
} catch (Exception e) {
logger.error("密码解密过程中出现异常: SecurityUtil Method decrypt "+e.getMessage());
}
return new String(encoded).trim();
}
}