哪位大神能帮忙把这个java加密解密函数 用php写一个,要求结果一致,谢谢了!
SecurityHelper
package com.units;
import java.security.*;
import javax.crypto.*;
import javax.crypto.spec.*;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class SecurityHelper {
protected final static Log logger = LogFactory.getLog(SecurityHelper.class);
private final static int ITERATIONS = 20;
public static String encrypt(String key, String plainText) throws Exception {
try {
byte[] salt = new byte[8];
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(key.getBytes());
byte[] digest = md.digest();
for (int i = 0; i < 8; i++) {
salt[i] = digest[i];
}
PBEKeySpec pbeKeySpec = new PBEKeySpec(key.toCharArray());
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
SecretKey skey = keyFactory.generateSecret(pbeKeySpec);
PBEParameterSpec paramSpec = new PBEParameterSpec(salt, ITERATIONS);
Cipher cipher = Cipher.getInstance("PBEWithMD5AndDES");
cipher.init(Cipher.ENCRYPT_MODE, skey, paramSpec);
byte[] cipherText = cipher.doFinal(plainText.getBytes());
String saltString = new String(Base64.encode(salt));
String ciphertextString = new String(Base64.encode(cipherText));
return saltString + ciphertextString;
} catch (Exception e) {
throw new Exception("Encrypt Text Error:" + e.getMessage(), e);
}
}
public static String decrypt(String key, String encryptTxt) throws Exception {
int saltLength = 12;
try {
String salt = encryptTxt.substring(0, saltLength);
String ciphertext = encryptTxt.substring(saltLength, encryptTxt.length());
byte[] saltarray = Base64.decode(salt.getBytes());
byte[] ciphertextArray = Base64.decode(ciphertext.getBytes());
PBEKeySpec keySpec = new PBEKeySpec(key.toCharArray());
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
SecretKey skey = keyFactory.generateSecret(keySpec);
PBEParameterSpec paramSpec = new PBEParameterSpec(saltarray, ITERATIONS);
Cipher cipher = Cipher.getInstance("PBEWithMD5AndDES");
cipher.init(Cipher.DECRYPT_MODE, skey, paramSpec);
byte[] plaintextArray = cipher.doFinal(ciphertextArray);
return new String(plaintextArray);
} catch (Exception e) {
throw new Exception(e);
}
}
public static void main(String[] args) {
String encryptTxt = "";
String plainTxt = "{\"code\":\"0000\",\"detail\":\"成功\"}";
try {
System.out.println(plainTxt);
encryptTxt = encrypt("mypassword01", plainTxt);
plainTxt = decrypt("mypassword01", encryptTxt);
System.out.println(encryptTxt);
System.out.println(plainTxt);
} catch (Exception e) {
e.printStackTrace();
System.exit(-1);
}
}
}
Base64
package com.units;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import javax.mail.internet.MimeUtility;
public class Base64 {
public static byte[] encode(byte[] b) throws Exception {
ByteArrayOutputStream baos = null;
OutputStream b64os = null;
try {
baos = new ByteArrayOutputStream();
b64os = MimeUtility.encode(baos, "base64");
b64os.write(b);
b64os.close();
return baos.toByteArray();
} catch (Exception e) {
throw new Exception(e);
} finally {
try {
if (baos != null) {
baos.close();
baos = null;
}
} catch (Exception e) {
}
try {
if (b64os != null) {
b64os.close();
b64os = null;
}
} catch (Exception e) {
}
}
}
public static byte[] decode(byte[] b) throws Exception {
ByteArrayInputStream bais = null;
InputStream b64is = null;
try {
bais = new ByteArrayInputStream(b);
b64is = MimeUtility.decode(bais, "base64");
byte[] tmp = new byte[b.length];
int n = b64is.read(tmp);
byte[] res = new byte[n];
System.arraycopy(tmp, 0, res, 0, n);
return res;
} catch (Exception e) {
throw new Exception(e);
} finally {
try {
if (bais != null) {
bais.close();
bais = null;
}
} catch (Exception e) {
}
try {
if (b64is != null) {
b64is.close();
b64is = null;
}
} catch (Exception e) {
}
}
}
}