PHP怎么实现JAVA这种的字符串解析,求指点

问题:对接第三方SDK因为没有PHP版本,怎么把这个java代码用php的方式去实现解析的方法,有什么解决办法吗,或者相关文档

1. JAVA代码

package com.example.utils;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.security.Key;

/**
 * @program: 聊天室解密
 * @description: Aes工具类
 * @author: test
 * @create: 2022年9月19日18:32:51
 **/
public class AesUtil {

    private static final String ALGORITHM = "AES";
    private static final String AES_CBC_PADDING = "AES/CBC/PKCS5Padding";//AES/CBC/PKCS7Padding

    /**
     * Aes加密(ECB工作模式)
     *
     * @param key   密钥,key长度必须大于等于 3*8 = 24,并且是8的倍数
     * @param keyIv 初始化向量,keyIv长度必须等于16
     * @param data  密文
     * @return 明文
     * @throws Exception
     */
    public static byte[] decodeByCBC(byte[] key, byte[] keyIv, byte[] data)
            throws Exception {
        //获取SecretKey对象,也可以使用getSecretKey()方法
        Key secretKey = new SecretKeySpec(key, ALGORITHM);
        //获取指定转换的密码对象Cipher(参数:算法/工作模式/填充模式)
        Cipher cipher = Cipher.getInstance(AES_CBC_PADDING);
        //创建向量参数规范也就是初始化向量
        IvParameterSpec ips = new IvParameterSpec(keyIv);
        //用密钥和一组算法参数规范初始化此Cipher对象(加密模式)
        cipher.init(Cipher.DECRYPT_MODE, secretKey, ips);
        //执行加密操作
        return cipher.doFinal(data);
    }

    /**
     * 将16进制转换为二进制
     * @param hexStr
     * @return 转换后的结果
     */
    public static byte[] parseHexStr2Byte(String hexStr) {
        if (hexStr.length() < 1)
            return null;
        byte[] result = new byte[hexStr.length()/2];
        for (int i = 0;i< hexStr.length()/2; i++) {
            int high = Integer.parseInt(hexStr.substring(i*2, i*2+1), 16);
            int low = Integer.parseInt(hexStr.substring(i*2+1, i*2+2), 16);
            result[i] = (byte) (high * 16 + low);
        }
        return result;
    }
}

解析的函数

/**
 * @program: 解密接口返回data
 * @description: 示例方法
 * @param data
 * @param sKey
 * @param data  密文
 **/
private static String Decrypt(byte[] data, byte[] sKey) {
    try {
        byte[] decodeStr = AesUtil.decodeByCBC(sKey,sKey, data);
        String finalText = cn.hutool.core.codec.Base64.decodeStr(new String(decodeStr, StandardCharsets.UTF_8));
        return finalText;
    } catch (Exception ex) {
        System.out.println(ex.toString());
        return null;
    }
}
《L04 微信小程序从零到发布》
从小程序个人账户申请开始,带你一步步进行开发一个微信小程序,直到提交微信控制台上线发布。
《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
讨论数量: 6

php用openssl的AES/CBC/PKCS5Padding解析和加密就行

1年前 评论

直接用java不就好了,何必拘泥于语言

1年前 评论
esacpe 1年前
/**
 * rsa 公钥加密
 *
 * @return string|null
 */
function pre_rsa_public_encrypt($data, $json = true)
{
    $public_key = openssl_pkey_get_public(file_get_contents(storage_path('app/cert/rsa/rsa_public_key.pem')));

    $data = $json ? json_encode($data) : $data;

    openssl_public_encrypt($data, $encrypted_data, $public_key);

    return base64_encode($encrypted_data);
}

/**
 * rsa 私钥解密
 *
 * @return mixed|null
 */
function pre_rsa_private_decrypt($data, $json = true)
{
    $private_key = openssl_pkey_get_private(file_get_contents(storage_path('app/cert/rsa/rsa_private_key.pem')));

    openssl_private_decrypt(base64_decode($data), $decrypted_data, $private_key);

    return $json ? json_decode($decrypted_data, true) : $decrypted_data;
}

/**
 * rsa 私钥加密
 *
 * @return string|null
 */
function pre_rsa_private_encrypt($data, $json = true)
{
    $private_key = openssl_pkey_get_private(file_get_contents(storage_path('app/cert/rsa/rsa_private_key.pem')));

    $data = $json ? json_encode($data) : $data;

    openssl_private_encrypt($data, $encrypted_data, $private_key);

    return base64_encode($encrypted_data);
}

/**
 * rsa 公钥解密
 *
 * @return mixed|null
 */
function pre_rsa_public_decrypt($data, $json = true)
{
    $public_key = openssl_pkey_get_public(file_get_contents(storage_path('app/cert/rsa/rsa_public_key.pem')));

    openssl_public_decrypt(base64_decode($data), $decrypted_data, $public_key);

    return $json ? json_decode($decrypted_data, true) : $decrypted_data;
}
1年前 评论

我以前的做法是,打了个jar包运行起来,通过http协议,调用java程序加解密。 关键是给我加解密的人,不是写加解密算法的人。

1年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!