Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.9k views
in Technique[技术] by (71.8m points)

php 如何实现 java 的解密方式

会PHP。还不会Java,奈何文档只有Java 代码demo

public static String decode(String message, String privateKey) throws KsMerchantApiException {
        try {
            if (KsStringUtils.isBlank(message) || KsStringUtils.isBlank(privateKey)) {
                throw new KsMerchantApiException("decode param is blank");
            }
            Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
            byte[] key = Base64.decodeBase64(privateKey);
            SecretKeySpec keySpec = new SecretKeySpec(key, KEY_ALGORITHM);
            cipher.init(Cipher.DECRYPT_MODE, keySpec, new IvParameterSpec(new byte[16]));
            return new String(cipher.doFinal(Base64.decodeBase64(message)), CHARSET);
        } catch (Exception e) {
            throw new KsMerchantApiException("decode message fail", e);
        }
    }

已经根据Java demo 写出了一个PHP版。只是拿实际参数怎么运行都是解析false

        
        function decode($message, $privateKey)
        {
            // base64 反编码数据
            $message = base64_decode($message);
            // base64 反编码密钥
            $key = base64_decode($privateKey);
            // Iv向量我根据demo代码猜应该是反编码密钥后的前16位
            $iv = substr($key, 0, 16);
            // 只知道Java的加密是 采用AES加密。我PHP对应 aes-256-cbc? 不知道对不对
            $result = openssl_decrypt($message, 'aes-256-cbc', $key, OPENSSL_RAW_DATA, $iv);
            return $result;
        }

可提供参数密钥,寻求相关大佬指教

补充文档地址: 文档地址


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

已被蒙中加密方式AES-128-CBC 无向量,问题已解决


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...