java的des加密算法_JAVA上DES加密算法的实现用例

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">

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值