一:DesUtil.java
package com.telgenius.wechat.alichargcenter;
import java.io.ByteArrayInputStream;
import java.io.FileInputStream;
import java.io.ObjectInputStream;
import java.io.UnsupportedEncodingException;
import java.security.GeneralSecurityException;
import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.SecureRandom;
import java.security.Signature;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.Date;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.lang.StringUtils;
import com.telgenius.wechat.common.tools.Base64Utils;
import com.telgenius.wechat.common.tools.DateUtil;
public class DesUtil {
private String Algorithm = "DES";
private SecretKey deskey;
private Cipher c;
private byte[] cipherByte;
public DesUtil() {
try {
this.c = Cipher.getInstance(this.Algorithm);
} catch (Exception localException) {
}
}
private byte[] createEncryptor(byte[] arr) {
try {
this.c.init(1, this.deskey);
this.cipherByte = this.c.doFinal(arr);
} catch (Exception ex) {
ex.printStackTrace();
}
return this.cipherByte;
}
private byte[] createDecryptor(byte[] buff) {
try {
this.c.init(2, this.deskey);
this.cipherByte = this.c.doFinal(buff);
} catch (Exception ex) {
ex.printStackTrace();
}
return this.cipherByte;
}
/**
* 加密
* @param inStringData
* @param keyStroe
* @return
* @throws Exception
*/
public String enCode(String inStringData, byte[] keyStroe) throws Exception {
ByteArrayInputStream inputKey = new ByteArrayInputStream(keyStroe);
ObjectInputStream inputKeyObj = new ObjectInputStream(inputKey);
this.deskey = ((SecretKey) inputKeyObj.readObject());
inputKeyObj.close();
byte[] retarr = createEncryptor(inStringData.getBytes("UTF-8"));
byte[] tmp = Base64Utils.encode(retarr).getBytes();
for (int i = 0; i < tmp.length; ++i) {
if (tmp[i] == 43)
tmp[i] = 46;
}
return new String(tmp, "UTF-8");
}
/**
* 解密
* @param inStringData
* @param keyStroe
* @return
* @throws Exception
*/
public String deCode(String inStringData, byte[] keyStroe) throws Exception {
ByteArrayInputStream inputKey = new ByteArrayInputStream(keyStroe);
ObjectInputStream inputKeyObj = new ObjectInputStream(inputKey);
this.deskey = ((SecretKey) inputKeyObj.readObject());
inputKeyObj.close();
byte[] dot = inStringData.getBytes("UTF-8");
for (int i = 0; i < dot.length; ++i) {
if (dot[i] == 46)
dot[i] = 43;
}
byte[] tmp = Base64Utils.decode(dot);
byte[] ret = createDecryptor(tmp);
return new String(ret, "UTF-8");
}
/**
* 读取文件路径
* @param filename
* @return
* @throws Exception
*/
public static synchronized byte[] readKeyStoreFile(String filename)
throws Exception {
FileInputStream fin = new FileInputStream(filename);
byte[] rettemp = new byte[3000];
int x = fin.read(rettemp);
byte[] ret = new byte[x];
for (int i = 0; i < x; ++i)
ret[i] = rettemp[i];
fin.close();
return ret;
}
}
二:所需的Base64Utils.java
package com.telgenius.wechat.common.tools;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
import com.sun.org.apache.xml.internal.security.utils.Base64;
public class Base64Utils {
/**
* <p>
* BASE64字符串解码为二进制数据
* </p>
*
* @param base64
* @return
* @throws Exception
*/
public static byte[] decode(byte[] base64) throws Exception {
return Base64.decode(base64);
}
/** */
/**
* <p>
* 二进制数据编码为BASE64字符串
* </p>
*
* @param bytes
* @return
* @throws Exception
*/
public static String encode(byte[] bytes) throws Exception {
return new String(Base64.encode(bytes));
}
}
三:使用方式
public void test2(HttpServletRequest request) {
try {
//加密
String date = "需要加密的数据";
byte[] datasource = date.getBytes();
byte[] keyarr;
String filePath = request.getSession().getServletContext().getRealPath("/");//key文件地址
keyarr = DesUtil.readKeyStoreFile(filePath+"netmanager.key");
DesUtil des = new DesUtil();
String encryption = (String)des.enCode(date,keyarr);//加密
System.out.println("原数据:"+date);
System.out.println("加密后:"+encryption);
//解密
byte[] keyarr2;
String filePath2 = request.getSession().getServletContext().getRealPath("/");//key文件地址
keyarr2 = DesUtil.readKeyStoreFile(filePath2+"netmanager.key");
DesUtil des2 = new DesUtil();
String str = des2.deCode(encryption,keyarr2);
System.out.println("解密后:"+str);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
四:结果
原数据:需要加密的数据
加密后:nFoudXGaKCXPwfizjEdi1cvsX5x5VPGR
解密后:需要加密的数据