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.
The function of the code that listed is same as oracle’s dbms_obfuscation_toolkit DES arithmetic
packageutil;
importjavax.crypto.Cipher;
importjavax.crypto.SecretKey;
importjavax.crypto.SecretKeyFactory;
importjavax.crypto.spec.DESKeySpec;
importjavax.crypto.spec.IvParameterSpec;
importorg.apache.commons.lang.ArrayUtils;
publicclassEncryption {
privateCipheren;
privateCipherde;
publicbyte[] encrypt(String s) {
try{
byte[] data = s.getBytes("SJIS");
if(data.length% 8 != 0) {
intlength = 8 - data.length% 8;
byte[] spaces =newbyte[length];
for(inti = 0; i < spaces.length; i++) {
spaces[i] = 0x20;
}
data = ArrayUtils.addAll(data, spaces);
}
returnen.doFinal(data);
}catch(Exception e) {
thrownewRuntimeException(e);
}
}
publicString decrypt(byte[] b) {
try{
byte[] data =de.doFinal(b);
returnnewString(data,"SJIS").trim();
}catch(Exception e) {
thrownewRuntimeException(e);
}
}
privateEncryption() {
try{
DESKeySpec deskey =newDESKeySpec("12345678".getBytes());
SecretKeyFactory skf = SecretKeyFactory.getInstance("DES");
SecretKey key = skf.generateSecret(deskey);
IvParameterSpec iv =newIvParameterSpec(newbyte[] { 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) {
thrownewRuntimeException(e);
}
}
privatestaticEncryptioninstance=newEncryption();
publicstaticEncryption getInstance() {
returninstance;
}
}
Ok. I will create a column that will mapped to Hibernate. The type of the column should beRAW!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.
CREATETABLEZENCRYPT
(
IDVARCHAR2(32CHAR)NOTNULL,
ENCRYPTRAW(32)
)
Then we will define a Hibernate UseType, this make encrypt and decrypt more transparent.
packageusertype;
importjava.io.Serializable;
importjava.sql.PreparedStatement;
importjava.sql.ResultSet;
importjava.sql.SQLException;
importjava.sql.Types;
importorg.hibernate.HibernateException;
importorg.hibernate.usertype.UserType;
importutil.Encryption;
publicclassEncryptTypeimplementsUserType {
publicObject assemble(Serializable cached, Object owner)throwsHibernateException {
returnnull;
}
publicObject deepCopy(Object value)throwsHibernateException {
if(value ==null) {
returnnull;
}else{
returnnewString((String) value);
}
}
publicSerializable disassemble(Object value)throwsHibernateException {
returnnull;
}
publicbooleanequals(Object x, Object y)throwsHibernateException {
return(x == y) || (x !=null&& y !=null&& (x.equals(y)));
}
publicinthashCode(Object x)throwsHibernateException {
returnx.hashCode();
}
publicbooleanisMutable() {
returnfalse;
}
publicObject nullSafeGet(ResultSet rs, String[] names, Object owner)throwsHibernateException, SQLException {
//Get bin data from database then decrypt to String
byte[] data = rs.getBytes(names[0]);
returnEncryption.getInstance().decrypt(data);
}
publicvoidnullSafeSet(PreparedStatement st, Object value,intindex)throwsHibernateException, SQLException {
if(value ==null) {
return;
}
//Encrypt String to bin data
bytedata[] = Encryption.getInstance().encrypt(value.toString());
st.setBytes(index, data);
}
publicObject replace(Object original, Object target, Object owner)throwsHibernateException {
returnnull;
}
publicClass returnedClass() {
returnjava.lang.String.class;
}
publicint[] sqlTypes() {
returnnewint[] { Types.BINARY};
}
}
Mapping file
-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">