• PHP和JAVA AES加解密问题


    1.问题

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

    2.前置参数

            key 32位

            iv 16位

    3.问题解决

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

     4.PHP代码

    1. /**
    2. *
    3. * Created by PhpStorm
    4. * User: Noah
    5. * Date: 2023/10/10
    6. * Time: 16:35
    7. */
    8. namespace app\Service;
    9. use support\Log;
    10. class Aes
    11. {
    12. /**
    13. * var string $method 加解密方法,可通过openssl_get_cipher_methods()获得
    14. */
    15. protected $method;
    16. /**
    17. * var string $secret_key 加解密的密钥
    18. */
    19. protected $secret_key;
    20. /**
    21. * var string $iv 加解密的向量,有些方法需要设置比如CBC
    22. */
    23. protected $iv;
    24. /**
    25. * var string $options (不知道怎么解释,目前设置为0没什么问题)
    26. */
    27. protected $options;
    28. /**
    29. * 构造函数
    30. *
    31. * @param string $key 密钥
    32. * @param string $method 加密方式
    33. * @param string $iv iv向量
    34. * @param mixed $options 还不是很清楚
    35. *
    36. */
    37. public function __construct($key, $iv = '', $method = 'AES-256-CBC', $options = 1)
    38. {
    39. // key是必须要设置的
    40. $this->secret_key = isset($key) ? $key : 'morefun';
    41. $this->method = $method;
    42. $this->iv = $iv;
    43. $this->options = $options;
    44. }
    45. /**
    46. * 加密方法,对数据进行加密,返回加密后的数据
    47. *
    48. * @param string $data 要加密的数据
    49. *
    50. * @return string
    51. *
    52. */
    53. public function encrypt($data)
    54. {
    55. $result = openssl_encrypt($data, $this->method, $this->secret_key, $this->options, $this->iv);
    56. $result = base64_encode($result);
    57. return $result;
    58. }
    59. /**
    60. * 解密方法,对数据进行解密,返回解密后的数据
    61. *
    62. * @param string $data 要解密的数据
    63. *
    64. * @return string
    65. *
    66. */
    67. public function decrypt($data)
    68. {
    69. return openssl_decrypt(base64_decode($data), $this->method, $this->secret_key, $this->options, $this->iv);
    70. }
    71. }

    5.java代码

    1. package com.xxx.cn.utils;
    2. import org.apache.commons.codec.binary.Base64;
    3. import javax.crypto.Cipher;
    4. import javax.crypto.spec.IvParameterSpec;
    5. import javax.crypto.spec.SecretKeySpec;
    6. import java.nio.charset.StandardCharsets;
    7. public class Test {
    8. public static String encrypt(String secretKey, String iv, String strToEncrypt) {
    9. if (strToEncrypt == null || strToEncrypt.isEmpty()) {
    10. return strToEncrypt;
    11. }
    12. try {
    13. Cipher cipher = getCipher(secretKey, iv, Cipher.ENCRYPT_MODE);
    14. return
    15. Base64.encodeBase64String(cipher.doFinal(strToEncrypt.getBytes(StandardCharsets.UTF_8
    16. )));
    17. } catch (Exception exp) {
    18. exp.printStackTrace();
    19. return "";
    20. }
    21. }
    22. private static Cipher getCipher(String secretKey, String iv, int cipherMode)
    23. throws Exception {
    24. IvParameterSpec ivSpec = new IvParameterSpec(iv.getBytes(StandardCharsets.UTF_8));
    25. SecretKeySpec secretKeySpec = new
    26. SecretKeySpec(secretKey.getBytes(StandardCharsets.UTF_8), "AES");
    27. Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
    28. cipher.init(cipherMode, secretKeySpec, ivSpec);
    29. return cipher;
    30. }
    31. public static void main(String[] args) {
    32. String s = "xxxx";
    33. System.out.println(encrypt("95D03jGYjs1wTpg0PfKWgyvL2WfLXQ1y", "GETwRqKWgqtRO8Ew", s));
    34. }
    35. }

  • 相关阅读:
    html网页如何获取后台数据库的数据(html + ajax + php + mysql)
    eNSP - 基本命令解析
    Swagger2的配置
    MASC: Multi-scale Affinity with Sparse Convolution for 3D Instance Segmentation
    智慧仓储解决方案-最新全套文件
    人工神经网络的应用实例,人工神经网络算法实例
    Android,GreenDao数据库框架
    Linux动态库*.so函数名修改
    Azure DevOps 中 Dapr项目自动部署流程实践
    Codeforces Round #814 (Div. 2)(A~D)
  • 原文地址:https://blog.csdn.net/qq_29755359/article/details/133795778