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

2022-06-23 22:24:02 +08:00
 holystrike

这是某应用给的 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 可以用版本,谢!

1443 次点击
所在节点    Java
4 条回复
y830CAa5nink4rUQ
2022-06-23 22:38:06 +08:00
50 块钱解君愁
leadfast
2022-06-23 22:38:54 +08:00
随便搜的一个就可以的啊


```
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
2022-06-23 22:44:35 +08:00
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
2022-06-24 10:12:58 +08:00
感谢楼上各佬,管用!

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/861766

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX