V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
holystrike
V2EX  ›  Java

Java 的这段 cipher AES 解密算法,求个 PHP 版本

  •  
  •   holystrike · 2022-06-23 22:24:02 +08:00 · 1447 次点击
    这是一个创建于 895 天前的主题,其中的信息可能已经有所发展或是发生改变。

    这是某应用给的 Java 的解密算法

    
    import org.apache.commons.codec.binary.Base64;
    import javax.crypto.Cipher;
    import javax.crypto.spec.SecretKeySpec;
    import java.nio.charset.StandardCharsets;
    
    public class AESUtils {
        public static String aesDecrypt(String str, String key) throws Exception {
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
            cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), "AES"));
            byte[] bytes = Base64.decodeBase64(str);
            bytes = cipher.doFinal(bytes);
            return new String(bytes, StandardCharsets.UTF_8);
        }
    }
    

    我尝试了多种,都无法解出来

    参考加密数据(解密秘钥 1234567890123456 )

    E4M54v2CbwnbdG+quqWwgFGI5dgx3shx2gGZRiihvkQQLgbH12Y9/dJXO1/7H7QLL3H9fstismlYMLQrZxShEyknFJcLG96HbG4Cx/7gq4YMXgZJDI9Qvm1sH6H4arIHaPTSbHTk
    
    faYo7fo6Sc3lwBMOpJHi33Os5u7DobPmqkzkuyoRxbTD4mZaSYleDcYuouQTdma+rubH5PPzg0+R09XsEHWkgF6cc+Ylh2w0N6590eJDNdQvoI4m7eSiWQCJo5nN5zXj/2QeQcYwIfdpmQ==
    

    应得到的解密数据(解密秘钥 1234567890123456 )

    {
     "id": "dbe8970a-53a7-165c-7339-02c55bbddea5",
     "appKey": "FQa4kEGD",
     "appId": "34526534673",
     "msgType": "notice",
     "time": "1603698652093",
     "bizContent": {
      "name:": "测试",
      "value": "测试"
     }
    }
    

    无奈不大懂 java ,需要用 PHP 来进行解密

    求双修人士改为 PHP 可以用版本,谢!

    4 条回复    2022-06-24 10:12:58 +08:00
    y830CAa5nink4rUQ
        1
    y830CAa5nink4rUQ  
       2022-06-23 22:38:06 +08:00   ❤️ 1
    50 块钱解君愁
    leadfast
        2
    leadfast  
       2022-06-23 22:38:54 +08:00   ❤️ 1
    随便搜的一个就可以的啊


    ```
    function decrypt($str, $key)
    {
    $decrypted = openssl_decrypt($str, 'AES-128-ECB', $key, OPENSSL_ZERO_PADDING);
    $dec_s = strlen($decrypted);
    $padding = ord($decrypted[$dec_s - 1]);
    $decrypted = substr($decrypted, 0, -$padding);
    return $decrypted;
    }

    $str = "E4M54v2CbwnbdG+quqWwgFGI5dgx3shx2gGZRiihvkQQLgbH12Y9/dJXO1/7H7QLL3H9fstismlYMLQrZxShEyknFJcLG96HbG4Cx/7gq4YMXgZJDI9Qvm1sH6H4arIHaPTSbHTkfaYo7fo6Sc3lwBMOpJHi33Os5u7DobPmqkzkuyoRxbTD4mZaSYleDcYuouQTdma+rubH5PPzg0+R09XsEHWkgF6cc+Ylh2w0N6590eJDNdQvoI4m7eSiWQCJo5nN5zXj/2QeQcYwIfdpmQ==";

    $res = decrypt($str, "1234567890123456");

    ```
    y830CAa5nink4rUQ
        3
    y830CAa5nink4rUQ  
       2022-06-23 22:44:35 +08:00   ❤️ 1
    openssl_decrypt(base64_decode('E4M54v2CbwnbdG+quqWwgFGI5dgx3shx2gGZRiihvkQQLgbH12Y9/dJXO1/7H7QLL3H9fstismlYMLQrZxShEyknFJcLG96HbG4Cx/7gq4YMXgZJDI9Qvm1sH6H4arIHaPTSbHTkfaYo7fo6Sc3lwBMOpJHi33Os5u7DobPmqkzkuyoRxbTD4mZaSYleDcYuouQTdma+rubH5PPzg0+R09XsEHWkgF6cc+Ylh2w0N6590eJDNdQvoI4m7eSiWQCJo5nN5zXj/2QeQcYwIfdpmQ=='), 'AES-128-ECB', '1234567890123456', OPENSSL_RAW_DATA);

    ```
    {
    "id": "dbe8970a-53a7-165c-7339-02c55bbddea5",
    "appKey": "FQa4kEGD",
    "appId": "34526534673",
    "msgType": "notice",
    "time": "1603698652093",
    "bizContent": {
    "name:": "测试",
    "value": "测试"
    }
    }
    ```
    holystrike
        4
    holystrike  
    OP
       2022-06-24 10:12:58 +08:00
    感谢楼上各佬,管用!
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   5922 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 02:20 · PVG 10:20 · LAX 18:20 · JFK 21:20
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.