PHP openssl 对接 JAVA AES加密

PHP对接爱康单点登录接口 

JAVA

import java.net.URLDecoder;
import java.net.URLEncoder;
import java.security.SecureRandom;
import java.util.Base64;

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

public class AESEncoderUtil {
    private static final String AES = "AES";
    private static final String SP = "SHA1PRNG";
    private static final String CHARACTER = "utf-8";

    /**
     * 对原字符串串进⾏行行加密
     * 
     * @param content
     *            原字符串串
     * @return
     */
    public static String encode(String content, String encodeRules) {
        try {
            KeyGenerator kgt = KeyGenerator.getInstance(AES);
            SecureRandom secureRandom = SecureRandom.getInstance(SP);
            secureRandom.setSeed(encodeRules.getBytes());
            kgt.init(128, secureRandom);
            SecretKey secretKey = kgt.generateKey();
            byte[] enCodeFormat = secretKey.getEncoded();
            SecretKeySpec key = new SecretKeySpec(enCodeFormat, AES);
            Cipher cipher = Cipher.getInstance(AES);
            byte[] byteContent = content.getBytes(CHARACTER);
            cipher.init(Cipher.ENCRYPT_MODE, key);
            byte[] result = cipher.doFinal(byteContent);
            // 对sign进⾏行行编码,防⽌止加号变为空格
            return URLEncoder.encode(parseByte2HexStr(result), "UTF-8");
        } catch (Exception e) {
            throw new RuntimeException("encode error");
        }
    }

    /**
     * 对加密的字符串串解密
     * 
     * @param content
     *            加密的字符串串
     * @return
     */
    public static String decode(String content, String encodeRules) {
        try {
            // 对sign进⾏行行解码
            byte[] decryptFrom = parseHexStr2Byte(URLDecoder.decode(content, "UTF-8"));
            KeyGenerator kgt = KeyGenerator.getInstance(AES);
            SecureRandom secureRandom = SecureRandom.getInstance(SP);
            secureRandom.setSeed(encodeRules.getBytes());
            kgt.init(128, secureRandom);
            SecretKey secretKey = kgt.generateKey();
            byte[] enCodeFormat = secretKey.getEncoded();
            SecretKeySpec key = new SecretKeySpec(enCodeFormat, AES);
            Cipher cipher = Cipher.getInstance(AES);// 创建密码器器
            cipher.init(Cipher.DECRYPT_MODE, key);// 初始化
            byte[] result = cipher.doFinal(decryptFrom);
            return new String(result); // 加密
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 将⼆二进制转换成16进制
     * 
     * @param buf
     * @return
     */
    private static String parseByte2HexStr(byte buf[]) {
        return new String(Base64.getEncoder().encodeToString(buf));
    }

    /**
     * 将16进制转换为⼆二进制
     * 
     * @param hexStr
     * @return
     */
    private static byte[] parseHexStr2Byte(String hexStr) {
        return Base64.getDecoder().decode(hexStr);
    }

    public static void main(String[] args) {
        String CDNO = "{\"companyCode\": \"test\",\"auth\":1555407227,\"username\": \"123456789\"}";
        String encodeRules = "8N3U2C3I4B5C2J7P83A";
        String encode = AESEncoderUtil.encode(CDNO, encodeRules);
        System.out.println("test encode:" + encode);
        System.out.println("demo encode:bHwXyaQzi6B80L01X%2FdDVhzT9wg58rR3vP6NpDmqnN14KCKgmsX%2BmcqTPz5%2F79w7Ko2PKmqk6hupFSqDFlbcLbTMRnhataKrZKUOkvxGnpg%3D");
        String decode = AESEncoderUtil.decode("bHwXyaQzi6B80L01X%2FdDVhzT9wg58rR3vP6NpDmqnN14KCKgmsX%2BmcqTPz5%2F79w7sC%2BgOav9kPwfJ9SzfbPMBZS6mO6RMYO0IAuZeM1npE8%3D", encodeRules);
        System.out.println("decode:" + decode);
        System.out.printf("完成\n\r");
    }

}

PHP

class Aes
{

    public static function encode($str, $key)
    {
        $key = substr(openssl_digest(openssl_digest($key, 'sha1', true), 'sha1', true), 0, 16);
        $data = openssl_encrypt($str, 'AES-128-ECB', $key, OPENSSL_RAW_DATA);
        return urlencode(base64_encode($data));
    }

    public static function decode($str, $key)
    {
        $key = substr(openssl_digest(openssl_digest($key, 'sha1', true), 'sha1', true), 0, 16);

        $data = base64_decode(urldecode($str));
        $data = openssl_decrypt($data, 'AES-128-ECB', $key, OPENSSL_RAW_DATA, '');
        return $data;
    }

}

$str = '{"companyCode": "test","auth": 1555407227,"username":"123456789"}';
$key = '8N3U2C3I4B5C2J7P83A';
$encode = 'bHwXyaQzi6B80L01X%2FdDVhzT9wg58rR3vP6NpDmqnN14KCKgmsX%2BmcqTPz5%2F79w7Ko2PKmqk6hupFSqDFlbcLbTMRnhataKrZKUOkvxGnpg%3D';

var_dump(Aes::encode($str,$key));
echo '
'; var_dump(Aes::decode($encode,$key));

 

php的同学可以装个虚拟机上跑java

sudo yum -y install java
sudo yum install -y java-1.8.0-openjdk-devel.x86_64
javac AESEncoderUtil.java
java AESEncoderUtil

 

你可能感兴趣的:(PHP,JAVA,AES,openssl)