First we should implement Oracle’s DES arithmetic using Java. Now Java provide the DES arithmetic in jce.jar. So we only work is provide the transformation.
Please refer JavaDoc and Oracle forum
The function of the code that listed is same as oracle’s dbms_obfuscation_toolkit DES arithmetic
package util;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.IvParameterSpec;
import org.apache.commons.lang.ArrayUtils;
public class Encryption {
private Cipher en;
private Cipher de;
public byte[] encrypt(String s) {
try {
byte[] data = s.getBytes("SJIS");
if (data.length % 8 != 0) {
int length = 8 - data.length % 8;
byte[] spaces = new byte[length];
for (int i = 0; i < spaces.length; i++) {
spaces[i] = 0x20;
}
data = ArrayUtils.addAll(data, spaces);
}
return en.doFinal(data);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public String decrypt(byte[] b) {
try {
byte[] data = de.doFinal(b);
return new String(data, "SJIS").trim();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
private Encryption() {
try {
DESKeySpec deskey = new DESKeySpec("12345678".getBytes());
SecretKeyFactory skf = SecretKeyFactory.getInstance("DES");
SecretKey key = skf.generateSecret(deskey);
IvParameterSpec iv = new IvParameterSpec(new byte[] { 0, 0, 0, 0, 0, 0, 0, 0 });
en = Cipher.getInstance("DES/CBC/NoPadding");
en.init(Cipher.ENCRYPT_MODE, key, iv);
de = Cipher.getInstance("DES/CBC/NoPadding");
de.init(Cipher.DECRYPT_MODE, key, iv);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
private static Encryption instance = new Encryption();
public static Encryption getInstance() {
return instance;
}
}
Ok. I will create a column that will mapped to Hibernate. The type of the column should be RAW! VARCHAR2 is not a good choice, actually VARCHAR2 does not work with encrypted column , because can not get the correct encrypted data from VARCHAR2 type.
CREATE TABLE ZENCRYPT
(
ID VARCHAR2(32 CHAR) NOT NULL,
ENCRYPT RAW(32)
)
Then we will define a Hibernate UseType, this make encrypt and decrypt more transparent.
package usertype;
import java.io.Serializable;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import org.hibernate.HibernateException;
import org.hibernate.usertype.UserType;
import util.Encryption;
public class EncryptType implements UserType {
public Object assemble(Serializable cached, Object owner) throws HibernateException {
return null;
}
public Object deepCopy(Object value) throws HibernateException {
if (value == null) {
return null;
} else {
return new String((String) value);
}
}
public Serializable disassemble(Object value) throws HibernateException {
return null;
}
public boolean equals(Object x, Object y) throws HibernateException {
return (x == y) || (x != null && y != null && (x.equals(y)));
}
public int hashCode(Object x) throws HibernateException {
return x.hashCode();
}
public boolean isMutable() {
return false;
}
public Object nullSafeGet(ResultSet rs, String[] names, Object owner) throws HibernateException, SQLException {
//Get bin data from database then decrypt to String
byte[] data = rs.getBytes(names[0]);
return Encryption.getInstance().decrypt(data);
}
public void nullSafeSet(PreparedStatement st, Object value, int index) throws HibernateException, SQLException {
if (value == null) {
return;
}
//Encrypt String to bin data
byte data[] = Encryption.getInstance().encrypt(value.toString());
st.setBytes(index, data);
}
public Object replace(Object original, Object target, Object owner) throws HibernateException {
return null;
}
public Class returnedClass() {
return java.lang.String.class;
}
public int[] sqlTypes() {
return new int[] { Types.BINARY };
}
}
Mapping file
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="entity.ZEncrypt" table="ZENCRYPT">
<id name="ids" type="string">
<column name="ID" />
<generator class="assigned" />
</id>
<property name="encrypt" type="usertype.EncryptType">
<column name="ENCRYPT" />
</property>
</class>
</hibernate-mapping>
聯(lián)系客服