gpt4 book ai didi

java - php中未调用解密函数

转载 作者:太空宇宙 更新时间:2023-11-04 08:16:27 25 4
gpt4 key购买 nike

我在java中使用加密并在php中执行解密。
我正在使用以下代码在java中进行加密。

String iv = "fedcba9876543210";
IvParameterSpec ivspec;
KeyGenerator keygen;
Key key;

ivspec = new IvParameterSpec(iv.getBytes());

keygen = KeyGenerator.getInstance("AES");
keygen.init(128);
key = keygen.generateKey();

keyspec = new SecretKeySpec(key.getEncoded(), "AES");

Cipher cipher;
byte[] encrypted;

cipher = Cipher.getInstance("AES/CBC/NoPadding");
cipher.init(Cipher.ENCRYPT_MODE, keyspec, ivspec);
encrypted = cipher.doFinal(padString(text).getBytes());

private String padString(String source) {
char paddingChar = ' ';
int size = 16;
int padLength = size - source.length() % size;

for (int i = 0; i < padLength; i++) {
source += paddingChar;
}

return source;
}


为了在 php 中解密,我使用以下代码:

function decrypt($code, $key) {
$key = $this->hex2bin($key);
$code = $this->hex2bin($code);

$td = mcrypt_module_open("rijndael-128", "", "cbc", "fedcba9876543210");

mcrypt_generic_init($td, $key, CIPHER_IV);
$decrypted = mdecrypt_generic($td, $code);

mcrypt_generic_deinit($td);
mcrypt_module_close($td);

return utf8_encode(trim($decrypted));
}

function hex2bin($hexdata) {
$bindata = "";

for ($i = 0; $i < strlen($hexdata); $i += 2) {
$bindata .= chr(hexdec(substr($hexdata, $i, 2)));
}

return $bindata;
}


加密工作正常,但在 php 的此函数解密期间,流程停止:

$td = mcrypt_module_open("rijndael-128", "", "cbc", "fedcba9876543210");

那么我错过了什么?

最佳答案

您遇到的错误是什么?

您是否安装/编译了 PHP 的 mcrypt? Info here

关于java - php中未调用解密函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10327126/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com