使用PHP获得与java相同的AES-128-CBC加密

时间:2016-06-14 05:35:51

标签: php encryption aes

我知道关于PHP Java AES CBC加密的stackoverflow有很多讨论,但没有一个能解决我的问题

这是java函数:

public static String encrypt(String input, String key){
    byte[] crypted = null;

    SecretKeySpec skey = new SecretKeySpec(getHash("MD5", key), "AES");
    IvParameterSpec iv = new IvParameterSpec(new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 });
    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    cipher.init(Cipher.ENCRYPT_MODE, skey, iv);
    crypted = cipher.doFinal(input.getBytes());
    return new String(Base64.encodeBase64(crypted));
}

 private static byte[] getHash(String algorithm, String text) {
    try {
        byte[] bytes = text.getBytes("UTF-8");
        final MessageDigest digest = MessageDigest.getInstance(algorithm);
        digest.update(bytes);
        return digest.digest();
    } catch (final Exception ex) {
        throw new RuntimeException(ex.getMessage());
    }
}

以下是我在PHP中所做的事情

public static function encrypt($input, $key) {

    $key = hash('md5', $key, true);
    $iv = '0000000000000000';
    return openssl_encrypt($input, 'aes-128-cbc', $key, 0, $iv);
}

让我们看看结果

key:"790757e76c8942f995675b247aa57c2a"
input:"1234"

result in java:UfczMtIAm8ewSuIGRdPTDQ==
result in PHP:wd/OTHoIXwgHGDHcj8OTgg==    

在php中,我也使用其他方法,例如mcrypt_encryptmcrypt_generic和pkcs5填充(请参阅代码打击),所有这些方法都可以获得与openssl_encrypt相同的加密,但仍与Java函数结果

不同
public static function encrypt($input, $key) {

    $key = hash('md5', $key, true);


    $iv = '0000000000000000';

    $size = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);
    $input = Security::pkcs5_pad($input, $size);


    $encrypted = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $input, MCRYPT_MODE_CBC, $iv);

    // output wd/OTHoIXwgHGDHcj8OTgg==
    echo base64_encode($encrypted);

    $td = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');

    mcrypt_generic_init($td, $key, $iv);
    $data = mcrypt_generic($td, $input);
    mcrypt_generic_deinit($td);
    mcrypt_module_close($td);
    $data = base64_encode($data);

    // output wd/OTHoIXwgHGDHcj8OTgg==
    echo $data;
}

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

我注意到另一个奇怪的事情,如果我将密码方法从CBC更改为ECB,java和php加密都会相同,但遗憾的是,对于访问第三方API,我必须使用CBC

1 个答案:

答案 0 :(得分:2)

终于明白了

"/myAssembley;component/Resources/image1.png"

应该用

替换它
$iv = '0000000000000000';

我从其他地方复制它,并认为$iv = str_repeat(chr(0), 16); 等于'0000000000000000',这完全错了..