PHP和JAVA AES加解密问题

1.问题

php加密的java不能正常机密,java加密的php不能正常解密

2.前置参数

key 32位

iv 16位

3.问题解决

因为key是32位的,所以参数需要method=AES-256-CBC,options=1

4.PHP代码

php 复制代码
<?php
/**
 *
 * Created by PhpStorm
 * User: Noah
 * Date: 2023/10/10
 * Time: 16:35
 */

namespace app\Service;



use support\Log;

class Aes
{
    /**
     * var string $method 加解密方法,可通过openssl_get_cipher_methods()获得
     */
    protected $method;

    /**
     * var string $secret_key 加解密的密钥
     */
    protected $secret_key;

    /**
     * var string $iv 加解密的向量,有些方法需要设置比如CBC
     */
    protected $iv;

    /**
     * var string $options (不知道怎么解释,目前设置为0没什么问题)
     */
    protected $options;

    /**
     * 构造函数
     *
     * @param string $key 密钥
     * @param string $method 加密方式
     * @param string $iv iv向量
     * @param mixed $options 还不是很清楚
     *
     */
    public function __construct($key, $iv = '', $method = 'AES-256-CBC', $options = 1)
    {
        // key是必须要设置的
        $this->secret_key = isset($key) ? $key : 'morefun';

        $this->method = $method;

        $this->iv = $iv;

        $this->options = $options;
    }

    /**
     * 加密方法,对数据进行加密,返回加密后的数据
     *
     * @param string $data 要加密的数据
     *
     * @return string
     *
     */
    public function encrypt($data)
    {

        $result = openssl_encrypt($data, $this->method, $this->secret_key, $this->options, $this->iv);
        $result = base64_encode($result);
        return $result;
    }

    /**
     * 解密方法,对数据进行解密,返回解密后的数据
     *
     * @param string $data 要解密的数据
     *
     * @return string
     *
     */
    public function decrypt($data)
    {

        return openssl_decrypt(base64_decode($data), $this->method, $this->secret_key, $this->options, $this->iv);
    }
}

5.java代码

java 复制代码
package com.xxx.cn.utils;

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;

public class Test {

    public static String encrypt(String secretKey, String iv, String strToEncrypt) {
        if (strToEncrypt == null || strToEncrypt.isEmpty()) {
            return strToEncrypt;
        }
        try {
            Cipher cipher = getCipher(secretKey, iv, Cipher.ENCRYPT_MODE);
            return
                    Base64.encodeBase64String(cipher.doFinal(strToEncrypt.getBytes(StandardCharsets.UTF_8
                    )));
        } catch (Exception exp) {
            exp.printStackTrace();
            return "";
        }
    }

    private static Cipher getCipher(String secretKey, String iv, int cipherMode)
            throws Exception {
        IvParameterSpec ivSpec = new IvParameterSpec(iv.getBytes(StandardCharsets.UTF_8));
        SecretKeySpec secretKeySpec = new
                SecretKeySpec(secretKey.getBytes(StandardCharsets.UTF_8), "AES");
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
        cipher.init(cipherMode, secretKeySpec, ivSpec);
        return cipher;
    }

    public static void main(String[] args) {
        String s = "xxxx";

        System.out.println(encrypt("95D03jGYjs1wTpg0PfKWgyvL2WfLXQ1y", "GETwRqKWgqtRO8Ew", s));
    }

}
相关推荐
withoutfear5 分钟前
宝塔面板Linux系统安装BuildAdmin
php·buildadmin
LucianaiB5 分钟前
【华为云码道|Agent / 自定义 Skills 】把架构红线写成 AI 照做的 Skill:多模块隐蔽故障排错实测
java·架构·华为云·atomgit·华为云码道·ai写码你来写故事
十年Java程序媛7 分钟前
MySQL 时间字段踩坑!date、datetime、timestamp 怎么选
android·java·开发语言·数据库·mysql·面试·个人开发
吃我一窝jio12 分钟前
Java输出流全解析
java·开发语言·笔记
Rocky Ding*16 分钟前
【三年面试五年模拟】2026-11-28 Java后端与AI Agent开发50道高频面试题全解析
java·论文阅读·人工智能·深度学习·机器学习·aigc·ai-native
SummerJokes29 分钟前
Java List 集合进阶|商品去重 equals 重写 + 完整学生管理系统菜单交互
java·list·交互
路灯上の马陈29 分钟前
Java File1 学习笔记
java·笔记·学习
中科三方39 分钟前
新一代通信网建设实施方案:域名安全被纳入通信安全保障重点方向
数据库·安全·php
卓豪终端管理42 分钟前
应用白名单和黑名单怎么建?一份可落地的应用程序控制指南
安全·web安全·php
Rain的Java大神之路44 分钟前
CNN 要学到什么程度?Java 工程师转 AI 的能力边界、推理落地与避坑指南
java·后端·面试