packagetest;importcom.alibaba.fastjson.JSONObject;importsun.misc.BASE64Decoder;importsun.misc.BASE64Encoder;importjavax.crypto.Cipher;importjavax.crypto.SecretKey;importjavax.crypto.SecretKeyFactory;importjavax.crypto.spec.DESKeySpec;importorg.apache.commons.lang.StringUtils;importjava.io.IOException;importjava.net.URLEncoder;importjava.security.SecureRandom;public classFpayEncryTool {private final static String ENCODE = "GBK";private final static String DES = "DES";private final static int keyLengthMax=24;//对数据进行加密//type:pay,refund
public static String encryFpayData(String key,String data) throwsException {//将数据转为json验证数据有效性
JSONObject jsonStr=JSONObject.parseObject(data);if(jsonStr.get("type").equals("pay")){if(jsonStr.get("order_amount")==null||!StringUtils.isNumeric(jsonStr.get("order_amount").toString())){return null;
}if(jsonStr.get("product_name")==null||(jsonStr.get("product_name")).toString().length()>100){return null;
}if(jsonStr.get("order_info")==null||(jsonStr.get("order_info")).toString().length()>100){return null;
}if(jsonStr.get("auth_code")==null||(jsonStr.get("auth_code")).toString().length()>128){return null;
}if(jsonStr.get("order_no")==null||(jsonStr.get("order_no")).toString().length()>30){return null;
}
}else if(jsonStr.get("type").equals("refund")){if(jsonStr.get("order_no")==null||(jsonStr.get("order_no")).toString().length()>30){return null;
}if(jsonStr.get("ref_desc")==null||(jsonStr.get("ref_desc")).toString().length()>30){return null;
}
}else if(jsonStr.get("type").equals("result")){
}else{return null;
}returnencrypt(data, key);
}//对数据进行解密
public static String decrypFpayData(String key,String data) throwsException {returndecrypt(data,key);
}/*** Description 根据键值进行加密
*@paramdata 待加密数据
*@paramkey 密钥
*@return*@throwsException*/
public static String encrypt(String data, String key) throwsException {byte[] bt =encrypt(data.getBytes(ENCODE), key.getBytes(ENCODE));
String strs= newBASE64Encoder().encode(bt);returnstrs;
}/*** 根据键值进行解密
*@paramdata 待解密数据
*@paramkey 密钥
*@return*@throwsIOException
*@throwsException*/
public static String decrypt(String data, String key) throwsIOException,
Exception {if (data == null)return null;
BASE64Decoder decoder= newBASE64Decoder();byte[] buf =decoder.decodeBuffer(data);byte[] bt =decrypt(buf, key.getBytes(ENCODE));return newString(bt, ENCODE);
}/*** Description 根据键值进行加密
*
*@paramdata
*@paramkey
* 加密键byte数组
*@return*@throwsException*/
private static byte[] encrypt(byte[] data, byte[] key) throwsException {
key=extendKey(key);//生成一个可信任的随机数源
SecureRandom sr = newSecureRandom();//从原始密钥数据创建DESKeySpec对象
DESKeySpec dks = newDESKeySpec(key);//创建一个密钥工厂,然后用它把DESKeySpec转换成SecretKey对象
SecretKeyFactory keyFactory =SecretKeyFactory.getInstance(DES);
SecretKey securekey=keyFactory.generateSecret(dks);//Cipher对象实际完成加密操作
Cipher cipher =Cipher.getInstance(DES);//用密钥初始化Cipher对象
cipher.init(Cipher.ENCRYPT_MODE, securekey, sr);returncipher.doFinal(data);
}public static byte[] extendKey(byte[] key) {//System.out.println("length:"+key.length);
byte[] tmpKey = new byte[keyLengthMax];if (key.length < keyLengthMax) { //short key ? .. extend to 24 byte key
int i;int iMax=(int)(keyLengthMax/key.length);int iRem=(int)(keyLengthMax%key.length);
System.arraycopy(key,0, tmpKey, 0,iRem+1);//System.out.println(" begI:"+0+" endI:"+(iRem));
for(i=0;i
System.arraycopy(key, 0, tmpKey, i*key.length+iRem, key.length);
}
}else{
System.arraycopy(key,0, tmpKey, 0,keyLengthMax);
}returntmpKey;
}/*** Description 根据键值进行解密
*
*@paramdata
*@paramkey 加密键byte数组
*@return*@throwsException*/
private static byte[] decrypt(byte[] data, byte[] key) throwsException {
key=extendKey(key);//生成一个可信任的随机数源
SecureRandom sr = newSecureRandom();//从原始密钥数据创建DESKeySpec对象
DESKeySpec dks = newDESKeySpec(key);//创建一个密钥工厂,然后用它把DESKeySpec转换成SecretKey对象
SecretKeyFactory keyFactory =SecretKeyFactory.getInstance(DES);
SecretKey securekey=keyFactory.generateSecret(dks);//Cipher对象实际完成解密操作
Cipher cipher =Cipher.getInstance(DES);//用密钥初始化Cipher对象
cipher.init(Cipher.DECRYPT_MODE, securekey, sr);returncipher.doFinal(data);
}public static voidmain(String[] args){/*String data = "12AUism810jsqASI08";
String key ="qwerrewq";
System.out.println("加密前===>"+data);
try {
//System.err.println(encrypt(data, key));
//System.err.println(decrypt(encrypt(data, key), key));
String jiamihou = encrypt(data,key);
System.out.println("加密后===>"+jiamihou);
System.out.println("解密后===>"+decrypt(jiamihou,key));
} catch (Exception e) {
e.printStackTrace();
}*/String temp;//temp="{'order_amount':'1','product_name':'测试','order_info':'测试加解密','auth_code':'285192674239050687','order_no':'201905140953','type':'pay'}";
temp="{'order_no':'TY_YH_ali_1557814483393_61','ref_desc':'测试退款退款','type':'refund'}";
String key="544646";
String str1=null;
String str2=null;try{
str1=FpayEncryTool.encryFpayData(key,temp);
str2=FpayEncryTool.decrypFpayData(key,str1);
}catch(Exception e) {
e.printStackTrace();
}
JSONObject jsonStr=JSONObject.parseObject(str2);
System.out.println("str1:"+str1);
System.out.println("str1转译:"+URLEncoder.encode(str1));
System.out.println("str2:"+str2);
System.out.println("type:"+jsonStr.get("type"));
}//将结果对特殊字符转译
}