国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開APP
userphoto
未登錄

開通VIP,暢享免費電子書等14項超值服

開通VIP
JAVA上DES加密算法的實現(xiàn)用例

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>

本站僅提供存儲服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊舉報。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
Spring中的HibernateTemplate
Hibernate如何映射enum(JDK5.0中的枚舉類)方法一
自定義數(shù)據(jù)類型映射
Hibernate Annotations 實戰(zhàn)
將Hibernate與Struts結(jié)合
Hibernate 工具類
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長圖 關(guān)注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點擊這里聯(lián)系客服!

聯(lián)系客服