简体   繁体   English

使用PHP OpenSSL将Java AES / ECB / PKCS7Padding /代码转换为PHP

[英]Converting Java AES/ECB/PKCS7Padding/ code to PHP using PHP OpenSSL

I want to encrypt a string using AES 256-bit encryption algorithm with ECB and PKCS7Padding. 我想使用带有ECB和PKCS7Padding的AES 256位加密算法来加密字符串。 I had gone through many sites but none of them was suitable. 我曾浏览过许多站点,但没有一个适合。

I have Java code which is using AES/ECB/PKCS7Padding method to encrypt a string. 我有使用AES / ECB / PKCS7Padding方法加密字符串的Java代码。

Input: 'mystring' Output: sWVnDZ0dwiTYoK4ixtURdA== 输入:'mystring'输出:sWVnDZ0dwiTYoK4ixtURdA ==

I want this algorithm in PHP using OpenSSL, I have tried many solutions but none of them works for me. 我想要使​​用OpenSSL在PHP中使用此算法,我尝试了许多解决方案,但没有一个适合我。

Please help me to solve the issue 请帮我解决问题

JAVA CODE JAVA代码

package com.test.utils;



import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;

import java.security.Key;
import java.security.Security;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

public class Crypto {

    private final String ALGORITHM = "AES/ECB/PKCS7Padding";
    private byte[] keyValue;

    public Crypto(String token) {
        keyValue = token.getBytes();
    }

    public Crypto() {
        keyValue= new byte[]{1, 2, 3, 4, 5, 6, 7,8,9, 10, 11, 12,13, 14, 15, 16};
    }

    public String encrypt(String valueToEnc) throws Exception {

        Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
        Key key = generateKey();

        Cipher c = Cipher.getInstance(ALGORITHM, "BC");
        c.init(Cipher.ENCRYPT_MODE, key);
        byte[] encValue = c.doFinal(valueToEnc.getBytes("UTF8"));
        return Base64.encode(encValue);

    }

    public String decrypt(String encryptedValue) throws Exception {
        Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
        Key key = generateKey();
        Cipher c = Cipher.getInstance(ALGORITHM, "BC");
        c.init(Cipher.DECRYPT_MODE, key);
        byte[] decordedValue = Base64.decode(encryptedValue);
        byte[] decValue = c.doFinal(decordedValue);
        return new String(decValue);
    }


    private Key generateKey() throws Exception {
        return new SecretKeySpec(keyValue, ALGORITHM);
    }
}

I have tried doing in php using OpenSSL 我尝试使用OpenSSL在php中做

<?php
$data = "mystring";
$method = "AES-256-ECB";
$ivSize = openssl_cipher_iv_length($method);

$iv = [1, 2, 3, 4, 5, 6, 7,8,9, 10, 11, 12,13, 14, 15, 16];

$packed = call_user_func_array("pack", array_merge(array("c*"), $iv));

$output_utf8 = mb_convert_encoding($packed, 'utf-8');
$encrypted = openssl_encrypt($data,$method,$output_utf8,OPENSSL_RAW_DATA);



echo strlen('sWVnDZ0dwiTYoK4ixtURdA==');
echo "<br>";
echo "Expected Answer : sWVnDZ0dwiTYoK4ixtURdA==<br><br>";
echo strlen(base64_encode($encrypted));
echo "<br>";
echo "Answer : ".base64_encode($encrypted);

?>

Expected Answer : sWVnDZ0dwiTYoK4ixtURdA== 预期答案:sWVnDZ0dwiTYoK4ixtURdA ==

Actual Answer : 7e5sKRCi21xzcwo6+ZOfaA== 实际答案:7e5sKRCi21xzcwo6 + ZOfaA ==

There are two issues here. 这里有两个问题。

First, in Java, the variant of AES (128, 192 or 256) that is used is determined by the length of the key. 首先,在Java中,所使用的AES变体(128、192或256)由密钥的长度确定。 Here you are using a 16 byte, or 128 bit, key so you are using AES-128. 在这里,您使用的是16字节或128位的密钥,因此您正在使用AES-128。

In PHP you specify the AES variant directly, and you are specifying AES-256. 在PHP中,您可以直接指定AES变体,也可以指定AES-256。 PHP will simply extend the key with null bytes until it is the required length. PHP将简单地将密钥扩展为空字节,直到达到所需的长度为止。 You should specify AES-128-ECB if you want to match the Java code. 如果要匹配Java代码,则应指定AES-128-ECB

Second, you need to be careful about how you are handling your key. 其次,您需要谨慎处理密钥。 In your PHP code you are actually using a key of all null bytes, since pack doesn't work on strings the way you expect. 在您的PHP代码中,您实际上使用的是全为空字节的密钥,因为pack不能按您期望的方式在字符串上工作。 You can simply use implode to convert the array into a string: 您可以简单地使用implode将数组转换为字符串:

$encrypted = openssl_encrypt($data, $method, implode($iv), OPENSSL_RAW_DATA);

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

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