记一次java和php使用aes加密/解密打通

对接了个第三方公司的服务,需要集成到公司的产品页,由于对方的技术栈是java,并且对接接口需要将数据加密传输,对方发来了java版的加解密方法,如下:

package kd.common.codec;

import java.security.MessageDigest;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

/**
 *  AES 128 加密,与Nodejs 保持一致
 * @author daiyang
 *
 */
public class AesUtil {
	
	private static final String secret = "pater";
	
	public static final String DEFAULT_CODING = "utf-8";

	/**
	 * 解密
	 * @param encrypted
	 * @return
	 */
	public static String decrypt(String encrypted){
		return decrypt(encrypted, secret);
	}
	/**
	 * 解密
	 * @param encrypted
	 * @param secret
	 * @return
	 * @throws Exception
	 */
	public static String decrypt(String encrypted, String secret){
		try{
			byte[] keyb = secret.getBytes(DEFAULT_CODING);
			MessageDigest md = MessageDigest.getInstance("MD5");
			byte[] thedigest = md.digest(keyb);
			SecretKeySpec skey = new SecretKeySpec(thedigest, "AES");
			Cipher dcipher = Cipher.getInstance("AES");
			dcipher.init(Cipher.DECRYPT_MODE, skey);

			byte[] clearbyte = dcipher.doFinal(toByte(encrypted));
			return new String(clearbyte);
		}catch(Exception e){
			e.printStackTrace();
			return "";
		}
		
	}

	/**
	 * 加密
	 * @param content
	 * @return
	 */
	public static String encrypt(String content)  {
		return encrypt(content, secret);
	}
	/**
	 * 加密
	 * @param content
	 * @param secret
	 * @return
	 * @throws Exception
	 */
	public static String encrypt(String content, String secret)  {
		try{
			byte[] input = content.getBytes(DEFAULT_CODING);

			MessageDigest md = MessageDigest.getInstance("MD5");
			byte[] thedigest = md.digest(secret.getBytes(DEFAULT_CODING));
			SecretKeySpec skc = new SecretKeySpec(thedigest, "AES");
			Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
			cipher.init(Cipher.ENCRYPT_MODE, skc);

			byte[] cipherText = new byte[cipher.getOutputSize(input.length)];
			int ctLength = cipher.update(input, 0, input.length, cipherText, 0);
			ctLength += cipher.doFinal(cipherText, ctLength);

			return parseByte2HexStr(cipherText);
		}catch(Exception e){
			e.printStackTrace();
			return "";
		}
	}

	/**
	 * 字符串转字节数组
	 * @param hexString
	 * @return
	 */
	private static byte[] toByte(String hexString) {
		int len = hexString.length() / 2;
		byte[] result = new byte[len];
		for (int i = 0; i < len; i++) {
			result[i] = Integer.valueOf(hexString.substring(2 * i, 2 * i + 2), 16).byteValue();
		}
		return result;
	}

	/**
	 * 字节转16进制数组
	 * @param buf
	 * @return
	 */
	private static String parseByte2HexStr(byte buf[]) {
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < buf.length; i++) {
			String hex = Integer.toHexString(buf[i] & 0xFF);
			if (hex.length() == 1) {
				hex = '0' + hex;
			}
			sb.append(hex);
		}
		return sb.toString();
	}

}

然后我发现,这完全就是百度的一个加密方法- -,其中,有nodejs版的互通方法:

/**
 * aes加密
 * @param data
 * @param secretKey
 */
encryptUtils.aesEncrypt = function(data, secretKey) {
	var cipher = crypto.createCipher('aes-128-ecb',secretKey);
	return cipher.update(data,'utf8','hex') + cipher.final('hex');
}
 
/**
 * aes解密
 * @param data
 * @param secretKey
 * @returns {*}
 */
encryptUtils.aesDecrypt = function(data, secretKey) {
	var cipher = crypto.createDecipher('aes-128-ecb',secretKey);
	return cipher.update(data,'hex','utf8') + cipher.final('utf8');

下面,我提供php版的加/解密方法,如下

/**
* 加密
* @param $str-需要加密的字符串
* @param $key-密钥
* @return string
/
function getEncryptContent($str,$key)
{
    return bin2hex(openssl_encrypt($str,'aes-128-ecb', openssl_digest($key, 'md5', true), OPENSSL_RAW_DATA));
}

/**
* 获取解秘内容
* @param $decode_str-需要解密的字符串
* @param $key-密钥
* @return string
*/
function getDecryptContent($decode_str,$key)
{
    return openssl_decrypt(hex2bin($decode_str), 'aes-128-ecb', openssl_digest($key, 'md5', true), OPENSSL_RAW_DATA);
}

发表评论

邮箱地址不会被公开。 必填项已用*标注