j4pQhWlzoubeaF+/wEI6kA==
用 Java 解密后得到ok
两个字符,PHP 却做不到。Java 代码如下
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.Charset;
import java.security.SecureRandom;
import java.util.Base64;
public class Main {
public static void main(String[] args) throws Exception {
String password = "password";
byte[] secret = Base64.getDecoder().decode(
"j4pQhWlzoubeaF+/wEI6kA==");
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
KeyGenerator kg = KeyGenerator.getInstance("AES");
SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
random.setSeed(password.getBytes());
kg.init(128, random);
cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(kg.generateKey().getEncoded(), "AES"));
byte[] result = cipher.doFinal(secret);
System.out.print(new String(result, Charset.forName("UTF-8")));
}
}
1
richangfan OP 用下面的 PHP 代码只能得到 false
``` var_export(openssl_decrypt(base64_decode('j4pQhWlzoubeaF+/wEI6kA=='), 'aes-128-ecb', 'password', OPENSSL_RAW_DATA)); ``` |
2
dedemao 2023-07-14 20:49:39 +08:00 3
```
$sKey = substr(openssl_digest(openssl_digest('password', 'sha1', true), 'sha1', true), 0, 16); echo openssl_decrypt(base64_decode('j4pQhWlzoubeaF+/wEI6kA=='), 'AES-128-ECB', $sKey, OPENSSL_RAW_DATA); ``` |
3
richangfan OP @dedemao
可以啊,厉害厉害 |
4
jhdxr 2023-07-15 05:54:08 +08:00
@richangfan 你之前的 php 实现忽略了 SecureRandom.getInstance("SHA1PRNG"); 这一段
|