简体   繁体   English

如何使AES 256位,ECB模式,PKCS5填充加密在Java和PHP之间起作用?

[英]How do I make AES 256-bit, ECB mode, PKCS5 Padding encryption works between Java and PHP?

I have a Java program that can encrypt using AES/ECB/PKCS5Padding cipher with 256bit key, and I want to write a PHP script that produce the same result. 我有一个Java程序,可以使用带有256位密钥的AES/ECB/PKCS5Padding密码进行加密,并且我想编写一个产生相同结果的PHP脚本。

However, my script failed to do so. 但是,我的脚本未能这样做。 I kind of get a feel that how they process the key before encryption is the problem, but I cannot tell. 我有点感觉到他们在加密之前如何处理密钥是问题,但是我无法确定。

Could someone point out what's wrong in the PHP script ? 有人可以指出PHP脚本出了什么问题吗?

Thanks. 谢谢。

Here are the example codes, you can simply run it after copy & paste: 以下是示例代码,您可以在复制和粘贴后直接运行它:

Java 爪哇

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import javax.xml.bind.DatatypeConverter;
import java.security.MessageDigest;

public class JavaCipher {

    private SecretKeySpec secretKey;

    private JavaCipher(String secret) throws Exception {
        MessageDigest sha = MessageDigest.getInstance("SHA-256");
        byte[] digest = sha.digest(secret.getBytes("UTF-8"));
        secretKey = new SecretKeySpec(digest, "AES");
    }

    private String encrypt(String sSrc) throws Exception {
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, secretKey);
        byte[] encrypted = cipher.doFinal(sSrc.getBytes());
        return DatatypeConverter.printHexBinary(encrypted).toLowerCase();
    }

    public static void main(String[] args) throws Exception {  
        JavaCipher cipher = new JavaCipher("some random key");

        // print d013acccb5d191a00898ac87057383ff
        System.out.println(cipher.encrypt("abcdefg"));
    }
}

PHP 的PHP

<?php

class PHPCipher {

    private $key;

    public function __construct($key)
    {
        $this->key = substr(hash('sha256', $key), 0, 32);
    }

    public function encrypt($data)
    {
        $size = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_ECB);
        $td = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_ECB, '');
        $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
        mcrypt_generic_init($td, $this->key, $iv);
        $result = mcrypt_generic($td, $this->pad($data, $size));
        mcrypt_generic_deinit($td);
        mcrypt_module_close($td);
        return bin2hex($result);
    }

    private function pad($text, $blocksize)
    {
        $pad = $blocksize - (strlen($text) % $blocksize);
        return $text . str_repeat(chr($pad), $pad);
    }
}

$cipher = new PHPCipher('some random key');
// print 9a4df66d67a3e8d4a1dda7cda6e94d07
echo $cipher->encrypt('abcdefg');

Since AES with key size 256 bit is what I need, all I need to do is provide a 32 bytes key. 由于我需要密钥大小为256位的AES,因此我所要做的就是提供32个字节的密钥。

Instead of cutting the string into 32 characters (which changes the key) : 而不是将字符串切成32个字符(这会更改密钥):

$this->key = substr(hash('sha256', $key), 0, 32);

I should do pack it into binary string 我应该将其打包成二进制字符串

$this->key = pack('H*', hash('sha256', $key));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM