求助:JAVA加密的数据PHP解密

已经尝试过用 php 解密,但是对 java 代码一知半解,最终解密失败,希望帮忙解答一下。

encodingAesKey:yPynf9C4b41bjisixYRhPQYseYMo4EuT9LsYsXyRwuc

java 加密后的密文:GOjohJfhlQl51zoHKWak+/eR5dND3kyOuFL83Kb4nkYmIA654rqRLyDeb5GKBmwiE5bAxcAtUUjKPsNqveDfWx+C4Fyp57CKcwbjV2rlfNNFzl//VU/Jns5VAAuQLXmT

以下是 java 的 解密示例,希望能用 php 解密出来

import com.google.common.base.CharMatcher;
import com.google.common.io.BaseEncoding;
import org.apache.commons.codec.binary.Base64;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;

public class CryptUtil {
    protected byte[] aesKey;

    public CryptUtil(String encodingAesKey) {
        this.aesKey = BaseEncoding.base64().decode(CharMatcher.whitespace().removeFrom(encodingAesKey));
    }

    public String decrypt(String encryptedText) {
        byte[] original;
        try {
            // 设置解密模式为AES的CBC模式
            Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
            SecretKeySpec keySpec = new SecretKeySpec(this.aesKey, "AES");
            IvParameterSpec iv = new IvParameterSpec(Arrays.copyOfRange(this.aesKey, 0, 16));
            cipher.init(Cipher.DECRYPT_MODE, keySpec, iv);
            // 使用BASE64对密文进行解码
            byte[] encrypted = Base64.decodeBase64(encryptedText);
            // 解密
            original = cipher.doFinal(encrypted);
        } catch (Exception e) {
            return null;
        }
        String trueContent;
        try {
            // 去除补位字符
            byte[] bytes = decode(original);
            // 分离16位随机字符串,网络字节序
            byte[] networkOrder = Arrays.copyOfRange(bytes, 16, 20);
            int contentLength = bytesNetworkOrder2Number(networkOrder);
            trueContent = new String(Arrays.copyOfRange(bytes, 20, 20 + contentLength), StandardCharsets.UTF_8);
        } catch (Exception e) {
            return null;
        }
        return trueContent;
    }

    public static byte[] decode(byte[] decrypted) {
        int pad = decrypted[decrypted.length - 1];
        if (pad < 1 || pad > 32) {
            pad = 0;
        }
        return Arrays.copyOfRange(decrypted, 0, decrypted.length - pad);
    }

    /**
     * 4个字节的网络字节序bytes数组还原成一个数字.
     */
    private static int bytesNetworkOrder2Number(byte[] bytesInNetworkOrder) {
        int sourceNumber = 0;
        for (int i = 0; i < 4; i++) {
            sourceNumber <<= 8;
            sourceNumber |= bytesInNetworkOrder[i] & 0xff;
        }
        return sourceNumber;
    }
}

java端解密部分变量打印结果如下:
求助:JAVA加密的数据PHP解密

《L05 电商实战》
从零开发一个电商项目,功能包括电商后台、商品 & SKU 管理、购物车、订单管理、支付宝支付、微信支付、订单退款流程、优惠券等
《L04 微信小程序从零到发布》
从小程序个人账户申请开始,带你一步步进行开发一个微信小程序,直到提交微信控制台上线发布。
最佳答案
class CryptUtil {
    protected $aesKey;

    public function __construct($encodingAesKey) {
        $this->aesKey = base64_decode(str_replace(' ', '', $encodingAesKey));
    }

    public function decrypt($encryptedText) {
        try {
            $cipher = "aes-256-cbc";
            $iv = substr($this->aesKey, 0, 16);
            $encrypted = base64_decode($encryptedText);
            $original = openssl_decrypt($encrypted, $cipher, $this->aesKey, OPENSSL_RAW_DATA | OPENSSL_ZERO_PADDING, $iv);

            $trueContent = $this->decode($original);
            $networkOrder = substr($trueContent, 16, 4);
            $contentLength = $this->bytesNetworkOrder2Number($networkOrder);
            $trueContent = substr($trueContent, 20, $contentLength);
            return $trueContent;
        } catch (Exception $e) {
            return null;
        }
    }

    public static function decode($decrypted) {
        $pad = ord($decrypted[strlen($decrypted) - 1]);
        if ($pad < 1 || $pad > 32) {
            $pad = 0;
        }
        return substr($decrypted, 0, strlen($decrypted) - $pad);
    }

    private static function bytesNetworkOrder2Number($bytesInNetworkOrder) {
        $sourceNumber = 0;
        for ($i = 0; $i < 4; $i++) {
            $sourceNumber <<= 8;
            $sourceNumber |= ord($bytesInNetworkOrder[$i]);
        }
        return $sourceNumber;
    }
}

试试看

8个月前 评论
wjx (楼主) 8个月前
讨论数量: 6

试一下

<?php

$aesKey = 'yPynf9C4b41bjisixYRhPQYseYMo4EuT9LsYsXyRwuc';
$aesKey = base64_decode($aesKey);
$iv = substr($aesKey, 0, 16);
$encryptText = 'GOjohJfhlQl51zoHKWak+/eR5dND3kyOuFL83Kb4nkYmIA654rqRLyDeb5GKBmwiE5bAxcAtUUjKPsNqveDfWx+C4Fyp57CKcwbjV2rlfNNFzl//VU/Jns5VAAuQLXmT';
$encryptText = base64_decode($encryptText);

$plainText = openssl_decrypt($encryptText, 'AES-256-CBC', $aesKey, OPENSSL_RAW_DATA | OPENSSL_ZERO_PADDING, $iv);
var_dump($plainText);
8个月前 评论

//将加密密钥转换成AES专用秘钥 $key = substr(openssl_digest(openssl_digest($secretKey, 'sha1', true), 'sha1, true), 0, 0);

我之前也做过这两个的转换,因为没有转换这个加密密钥,一直有问题。看这种是不是能给你提供个思路呢?

8个月前 评论
class CryptUtil {
    protected $aesKey;

    public function __construct($encodingAesKey) {
        $this->aesKey = base64_decode(str_replace(' ', '', $encodingAesKey));
    }

    public function decrypt($encryptedText) {
        try {
            $cipher = "aes-256-cbc";
            $iv = substr($this->aesKey, 0, 16);
            $encrypted = base64_decode($encryptedText);
            $original = openssl_decrypt($encrypted, $cipher, $this->aesKey, OPENSSL_RAW_DATA | OPENSSL_ZERO_PADDING, $iv);

            $trueContent = $this->decode($original);
            $networkOrder = substr($trueContent, 16, 4);
            $contentLength = $this->bytesNetworkOrder2Number($networkOrder);
            $trueContent = substr($trueContent, 20, $contentLength);
            return $trueContent;
        } catch (Exception $e) {
            return null;
        }
    }

    public static function decode($decrypted) {
        $pad = ord($decrypted[strlen($decrypted) - 1]);
        if ($pad < 1 || $pad > 32) {
            $pad = 0;
        }
        return substr($decrypted, 0, strlen($decrypted) - $pad);
    }

    private static function bytesNetworkOrder2Number($bytesInNetworkOrder) {
        $sourceNumber = 0;
        for ($i = 0; $i < 4; $i++) {
            $sourceNumber <<= 8;
            $sourceNumber |= ord($bytesInNetworkOrder[$i]);
        }
        return $sourceNumber;
    }
}

试试看

8个月前 评论
wjx (楼主) 8个月前

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