java转php加密和解密
将UserJsonUtil类中的
encryptJsonUser()加密,写成php的加密;
decryptJsonUser()解密,写成php的解密。
package com.test.moltest;
import java.util.Collection;
import java.util.Iterator;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.capinfo.crypt.Md5;
/**
* Created by IntelliJ IDEA.
* User: bob_0703
* Date: 2008-10-18
* Time: 17:30:53
* To change this template use File | Settings | File Templates.
*/
public class UserJsonUtil {
private static final Log log = LogFactory.getLog("UserJsonUtil.class");
public static boolean decryptJsonUser(String s) {
boolean rs = false;
try {
s = DESTools.decrypt("testtest", s);//解密1
JSONObject jobj = JSONObject.fromObject(s);//转换成为json
JSONArray names=jobj.names();
String md5Str = (String) jobj.get("md5");//获取加密后的MD5
names.remove(names.size()-1);
StringBuffer buf=new StringBuffer();
Iterator<String> it=names.iterator();
while(it.hasNext()){
String name=it.next();
buf.append(jobj.get(name));
}
jobj.remove("md5");
Md5 md5 = new Md5("");
md5.hmac_Md5(buf.toString(), "test");
byte b[] = md5.getDigest();
String digestString = md5.stringify(b);//获取加密后的MD5
if (md5Str.equals(digestString)) {//比较
rs = true;
}
} catch (Exception ie) {
ie.printStackTrace();
log.error(ie);
}
return rs;
}
public static String encryptJsonUser(JSONObject jobj) {
try {
Collection values=jobj.values();
StringBuffer buf=new StringBuffer();
Iterator it=values.iterator();
while(it.hasNext()){
String value=it.next().toString();
buf.append(value);
}
Md5 md5 = new Md5("");
md5.hmac_Md5(buf.toString(), "test");
byte b[] = md5.getDigest();
String digestString = md5.stringify(b);//加密MD5
jobj.put("md5", digestString);
digestString = DESTools.encrypt("testtest", jobj.toString());//加密
return digestString;
} catch (Exception ie) {
ie.printStackTrace();
log.error(ie);
return "";
}
}
}
package com.test.moltest;
import java.io.UnsupportedEncodingException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Random;
import javax.crypto.Cipher;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.IvParameterSpec;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
/**
* Created by IntelliJ IDEA.
* User: wtj
* Date: 2008-9-11
* Time: 11:30:08
* To change this template use File | Settings | File Templates.
* 国政通加密工具
*/
public class DESTools {
private static Random ran = new Random();
//private static Logger logger;
private static final String ALGORITHM = "DES/CBC/PKCS5Padding";
private static byte SECRETKEY[];
private static BASE64Decoder base64de = new BASE64Decoder();
private static BASE64Encoder base64en = new BASE64Encoder();
private static Cipher encryptCipher;
private static Cipher decryptCipher;
public static int getActualMaximum(String date) throws ParseException {
SimpleDateFormat formatter = new SimpleDateFormat("yyyyMM");
Calendar cal = Calendar.getInstance();
cal.setTime(formatter.parse("200604"));
return cal.getActualMaximum(Calendar.DATE);
}
public static String encrypt(String key, String src) {
SECRETKEY = key.getBytes();
String ret = "";
try {
DESKeySpec dks = new DESKeySpec(SECRETKEY);
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
javax.crypto.SecretKey securekey = keyFactory.generateSecret(dks);
IvParameterSpec spec = new IvParameterSpec(SECRETKEY);
Cipher encrypt_Cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
encrypt_Cipher.init(1, securekey, spec);
byte dest[] = encrypt_Cipher.doFinal(src.getBytes());
ret = base64en.encode(dest);
} catch (Exception e) {
e.printStackTrace();
}
return ret;
}
public static String decrypt(String key, String src) throws UnsupportedEncodingException {
SECRETKEY = key.getBytes();
String ret = "";
try {
DESKeySpec dks = new DESKeySpec(SECRETKEY);
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
javax.crypto.SecretKey securekey = keyFactory.generateSecret(dks);
IvParameterSpec spec = new IvParameterSpec(SECRETKEY);
Cipher decrypt_Cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
decrypt_Cipher.init(2, securekey, spec);
byte buf[] = base64de.decodeBuffer(src);
byte dest[] = decrypt_Cipher.doFinal(buf);
ret = new String(dest);
} catch (Exception e) {
e.printStackTrace();
}
return ret;
}
public static String decrypt1(String key, String src) throws UnsupportedEncodingException {
SECRETKEY = key.getBytes("GB2312");
String ret = "";
try {
DESKeySpec dks = new DESKeySpec(SECRETKEY);
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
javax.crypto.SecretKey securekey = keyFactory.generateSecret(dks);
IvParameterSpec spec = new IvParameterSpec(SECRETKEY);
Cipher decrypt_Cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
decrypt_Cipher.init(2, securekey, spec);
byte buf[] = base64de.decodeBuffer(src);
byte dest[] = decrypt_Cipher.doFinal(buf);
ret = new String(dest);
} catch (Exception e) {
e.printStackTrace();
}
return ret;
}
//遨游加密程序。
public static String Maxencrypt(String key1, String key2, String src) {
// SECRETKEY = key1.getBytes();
String ret = "";
try {
DESKeySpec dks = new DESKeySpec(key1.getBytes());
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
javax.crypto.SecretKey securekey = keyFactory.generateSecret(dks);
IvParameterSpec spec = new IvParameterSpec(key2.getBytes());
Cipher encrypt_Cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
encrypt_Cipher.init(1, securekey, spec);
byte dest[] = encrypt_Cipher.doFinal(src.getBytes());
ret = base64en.encode(dest);
} catch (Exception e) {
e.printStackTrace();
}
return ret;
}
public static String Maxdecrypt(String key1, String key2, String src) {
//SECRETKEY = key1.getBytes();
String ret = "";
try {
DESKeySpec dks = new DESKeySpec(key1.getBytes());
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
javax.crypto.SecretKey securekey = keyFactory.generateSecret(dks);
IvParameterSpec spec = new IvParameterSpec(key2.getBytes());
Cipher decrypt_Cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
decrypt_Cipher.init(2, securekey, spec);
byte buf[] = base64de.decodeBuffer(src);
byte dest[] = decrypt_Cipher.doFinal(buf);
ret = new String(dest);
} catch (Exception e) {
e.printStackTrace();
}
return ret;
}
}