package com.unifease.utility;
import java.io.IOException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.Security;
import java.security.spec.InvalidKeySpecException;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESedeKeySpec;
import javax.crypto.spec.IvParameterSpec;
import Decoder.BASE64Decoder;
import Decoder.BASE64Encoder;
/**
* 3DES加密解密处理
*/
public class TripleDES {
// 定义 加密算法,可用 DES,DESede,Blowfish
private static final String Algorithm = "DESede";
public static final String PASSWORD_CRYPT_KEY="c+xRAPXG8tvD+nHnafKcnJBCgkpPzqIr";
public static final String PASSWORD_IV="7MN+0yYTukY=";
public static void main(String[] args) {
/*
System.out.println("早餐的解码: " + URLDecoder.decode("%E6%97%A9%E9%A4%90"));
System.out.println("早餐的编码: " + URLEncoder.encode("早餐"));
String encryptData = "早餐";
String jmdata = TripleDES.encryptData(encryptData);
System.out.println("加密的数据:[" + jmdata + "]");
String jiemidata = TripleDES.decryptData(jmdata);
System.out.println("解密的数据:[" + jiemidata + "]");
*/
}
/**
* @param b为将要转换成十六进制的字节数组
*/
public static String byte2hex(byte[] b) {
String hs = "";
String stmp = "";
for (int n = 0; n < b.length; n++) {
stmp = (java.lang.Integer.toHexString(b[n] & 0XFF));
if (stmp.length() == 1)
hs = hs + "0" + stmp;
else
hs = hs + stmp;
if (n < b.length - 1)
hs = hs + ":";
}
return hs.toUpperCase();
}
/**
* 数据加密
*
* @param PASSWORD_CRYPT_KEY
* 密钥值 一般由第三方认证机构提供
* @param PASSWORD_IV
* 密钥向量值 一般由第三方认证机构提供
* @param str
* 被加密对象
* @return 返回加密后的对象
*/
public static String encryptData(String encryptData) {
Security.addProvider(new com.sun.crypto.provider.SunJCE());
// 创建加密的强随机数生成器
DESedeKeySpec dks;
SecretKeyFactory keyFactory;
try {
// BASW64解码,这个很关键哦!
BASE64Encoder encoder = new BASE64Encoder();
BASE64Decoder decoder = new BASE64Decoder();
byte[] buf = decoder.decodeBuffer(PASSWORD_IV);
byte[] key = decoder.decodeBuffer(PASSWORD_CRYPT_KEY);
// 生成密钥
dks = new DESedeKeySpec(key);
// 指定算法
keyFactory = SecretKeyFactory.getInstance(Algorithm);
// 指定生成的密钥
SecretKey securekey = keyFactory.generateSecret(dks);
// 指定向量值
IvParameterSpec iv = new IvParameterSpec(buf);
// 指定加密mode和padding ,ECB模式不支持IV向量
Cipher cipher = Cipher.getInstance("DESede/CBC/PKCS5Padding");// DESede
// 只支持
// CBC和ECB两种模式
// 初始化加密程序
cipher.init(Cipher.ENCRYPT_MODE, securekey, iv);
// 返回加密后的结果,注意BASE64的编码
return encoder.encodeBuffer(cipher.doFinal(encryptData.getBytes()));
} catch (NoSuchAlgorithmException e) {
//log.error("没有指定的算法", e);
return null;
} catch (InvalidKeySpecException e) {
//log.error("指定的密钥不正确", e);
return null;
} catch (NoSuchPaddingException e) {
//log.error("Padding模式不匹配", e);
} catch (InvalidAlgorithmParameterException e) {
//log.error("算法参数异常", e);
return null;
} catch (IllegalBlockSizeException e) {
//log.error("密钥字节数不匹配", e);
return null;
} catch (BadPaddingException e) {
//log.error("Padding加载不正确", e);
return null;
} catch (InvalidKeyException e) {
//log.error("IV向量值错误!", e);
return null;
} catch (IOException e) {
//log.error("文件流错误", e);
return null;
}
return null;
}
/**
* 数据解密
*
* @param PASSWORD_CRYPT_KEY密钥值
* 一般由第三方认证机构提供
* @param PASSWORD_IV
* 密钥向量值 一般由第三方认证机构提供
* @param str
* @return
*/
public static String decryptData(String decryptData) {
Security.addProvider(new com.sun.crypto.provider.SunJCE());
// SecureRandom sr = new SecureRandom();
DESedeKeySpec dks;
SecretKeyFactory keyFactory;
try {
// BASW64解码和编码,这个很关键哦!
BASE64Encoder encoder = new BASE64Encoder();
BASE64Decoder decoder = new BASE64Decoder();
byte[] buf = decoder.decodeBuffer(PASSWORD_IV);
byte[] key = decoder.decodeBuffer(PASSWORD_CRYPT_KEY);
// 生成密钥
dks = new DESedeKeySpec(key);
// 指定算法
keyFactory = SecretKeyFactory.getInstance(Algorithm);
// 指定生成的密钥
SecretKey securekey = keyFactory.generateSecret(dks);
// 指定向量值
IvParameterSpec iv = new IvParameterSpec(buf);
// 指定加密mode和padding ,ECB模式不支持IV向量
Cipher cipher = Cipher.getInstance("DESede/CBC/PKCS5Padding");// DESede
// 只支持
// CBC和ECB两种模式
// 初始化加密程序
cipher.init(Cipher.DECRYPT_MODE, securekey, iv);
// 返回加密后的结果,注意参数的BASE64 解码,即decoder.decodeBuffer(str);
return new String(cipher.doFinal(decoder
.decodeBuffer(decryptData)));
} catch (NoSuchAlgorithmException e) {
return null;
} catch (InvalidKeySpecException e) {
return null;
} catch (NoSuchPaddingException e) {
return null;
} catch (InvalidAlgorithmParameterException e) {
return null;
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
return null;
} catch (BadPaddingException e) {
return null;
} catch (InvalidKeyException e) {
return null;
} catch (IOException e) {
// return null;
}
return null;
}
}
显示进行Unicode编码再将数据加密、
String type = "byt_GetSchoolName";
String jsonstr = "{\"type\":\"" + type + "\",\"username\":\"" + SZYEY_USERNAME + "\",\"sbbs\":\"" + DEVICEID + "\"}";
String postParam = URLEncoder.encode(TripleDES.encryptData(jsonstr));
String result = HttpRequest.sendPost(SZYEY_URL, postParam);