Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/396.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JAVA加密&;PHP返回不同的结果_Java_Php_Algorithm_Encryption_Cryptography - Fatal编程技术网

JAVA加密&;PHP返回不同的结果

JAVA加密&;PHP返回不同的结果,java,php,algorithm,encryption,cryptography,Java,Php,Algorithm,Encryption,Cryptography,我在尝试将一些代码从JAVA“翻译”到PHP时遇到问题 我已经尝试了很多函数,但是没有任何东西能让我在两方面都得到相同的结果 JAVA代码 public static String encrypt(String text, String key, String charset) throws Exception { byte[] keyBytes = Base64.decodeBase64(key); SecretKeySpec secretKeySpec = new Secre

我在尝试将一些代码从JAVA“翻译”到PHP时遇到问题

我已经尝试了很多函数,但是没有任何东西能让我在两方面都得到相同的结果

JAVA代码

public static String encrypt(String text, String key, String charset) throws Exception {
    byte[] keyBytes = Base64.decodeBase64(key);
    SecretKeySpec secretKeySpec = new SecretKeySpec(keyBytes, ALGORITHM);

    Cipher cipher = Cipher.getInstance(ALGORITHM);
    cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
    byte[] textBytes = text.getBytes(charset);
    byte[] bytes = cipher.doFinal(textBytes);

    return new String(Base64.encodeBase64(bytes), charset);
}
关键参数->“NWelNxflgZ+rjP0bo2gi2Q==”
文本参数->“我是一个测试”
字符集参数->“UTF-8”
算法常数->AES

结果->“13vh3qeuc+kN7NvcKwM6pw==”


PHP代码

function encryptAES($text, $key)
{
    $key = strtohex($key);
    $encrypt = openssl_encrypt($text, 'aes128', $key, OPENSSL_RAW_DATA);
    if (!$encrypt) {
        throw new Exception('AES encryption error');
    }
    return base64_encode($encrypt);
}

function strtohex($x)
{
    $s='';
    foreach (str_split($x) as $c) $s.=sprintf("%02X",ord($c));
    return($s);
}
关键参数->“NWelNxflgZ+rjP0bo2gi2Q==”
文本参数->“我是一个测试”

结果->“Vs5pwAC7PK0fQUQQ+PMhKw=”


有人能帮我解释一下为什么我的代码不起作用吗


非常感谢各位。

修复了,问题是strothex会给您输入控制台的密码

 public static function encryptAES($data, $AES_key)
    {
        $AES_key = base64_decode($AES_key);
        $encrypt = openssl_encrypt($data, 'aes128', $AES_key);
        if (!$encrypt) {
            throw new Exception('AES encryption error');
        }
        return base64_encode($encrypt);
    }


这里是结果代码,它只是用base64_解码转换密钥。谢谢大家

@RiggsFolly我在看另一个问题。。。。这似乎是完全不同的。我现在无法通过查看发送给我的问题来回答问题。您是否阅读了有关
OPENSSL\u RAW\u DATA
@riggsfully的部分?是的,它仍然不起作用!现在我得到了一个正确的strlen
strlen
,所以我觉得我更接近了。但还是不一样。我正在编辑问题,改变这一点!