Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/296.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的AES加密_Java_Php_Algorithm_Encryption_Aes - Fatal编程技术网

使用Java和PHP的AES加密

使用Java和PHP的AES加密,java,php,algorithm,encryption,aes,Java,Php,Algorithm,Encryption,Aes,我最近在Java中使用了AES算法来加密文本。 现在我需要用PHP重建算法,但我不知道如何重建,因为互联网上的PHP算法返回不同的结果。也许你能帮我 这是要加密的Java代码: private static final String KEY = "57238004e784498bbc2f8bf984565090"; public static String encrypt(final String plaintext) throws GeneralSecurityException {

我最近在Java中使用了AES算法来加密文本。 现在我需要用PHP重建算法,但我不知道如何重建,因为互联网上的PHP算法返回不同的结果。也许你能帮我

这是要加密的Java代码:

private static final String KEY = "57238004e784498bbc2f8bf984565090";

public static String encrypt(final String plaintext) throws GeneralSecurityException {
    SecretKeySpec sks = new SecretKeySpec(hexStringToByteArray(KEY), "AES");
    Cipher cipher = Cipher.getInstance("AES");
    cipher.init(Cipher.ENCRYPT_MODE, sks, cipher.getParameters());
    byte[] encrypted = cipher.doFinal(plaintext.getBytes());
    return byteArrayToHexString(encrypted);
}

public static byte[] hexStringToByteArray(String s) {
    byte[] b = new byte[s.length() / 2];
    for (int i = 0; i < b.length; i++) {
        int index = i * 2;
        int v = Integer.parseInt(s.substring(index, index + 2), 16);
        b[i] = (byte) v;
    }
    return b;
}

public static String byteArrayToHexString(byte[] b) {
    StringBuilder sb = new StringBuilder(b.length * 2);
    for (int i = 0; i < b.length; i++) {
        int v = b[i] & 0xff;
        if (v < 16) {
            sb.append('0');
        }
        sb.append(Integer.toHexString(v));
    }
    return sb.toString().toUpperCase();
}
private static final String KEY=“5723804E784498BBC2F8BF984565090”;
公共静态字符串加密(最终字符串明文)引发GeneralSecurityException{
SecretKeySpec sks=新的SecretKeySpec(hexStringToByteArray(KEY),“AES”);
Cipher Cipher=Cipher.getInstance(“AES”);
cipher.init(cipher.ENCRYPT_模式,sks,cipher.getParameters());
byte[]encrypted=cipher.doFinal(明文.getBytes());
返回byteArrayTohextString(加密);
}
公共静态字节[]hexStringToByteArray(字符串s){
字节[]b=新字节[s.length()/2];
for(int i=0;i
你们能帮我建立一个PHP脚本,返回相同的结果吗

例如: 明文“STACKOVERFLOW”被加密为“FA652ECCD39A11A93D2458AA2A0793C”

提前谢谢

这应该可以做到:

function encrypt($plaintext, $key) {
    $plaintext = pkcs5_pad($plaintext, 16);
    return bin2hex(mcrypt_encrypt(MCRYPT_RIJNDAEL_128, hex2bin($key), $plaintext, MCRYPT_MODE_ECB));
}

function decrypt($encrypted, $key) {
    $decrypted = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, hex2bin($key), hex2bin($encrypted), MCRYPT_MODE_ECB);
    $padSize = ord(substr($decrypted, -1));
    return substr($decrypted, 0, $padSize*-1);
}

function pkcs5_pad ($text, $blocksize)
{
    $pad = $blocksize - (strlen($text) % $blocksize);
    return $text . str_repeat(chr($pad), $pad);
}
您发现的其他PHP算法返回不同结果的原因可能是填充。Java中AES的默认值是PKCS5,但PHP不支持这一点(因此使用了PKCS5_pad函数)

正如SLacks所说,你真的不应该使用ECB。如果需要,可以更改Java代码或重新加密现有数据。只要你继续使用ECB,你的数据就有风险


信用:从中获取的填充功能。

ECB不安全。您需要使用CBC并通过IV。根据您的安全边界,您可能还需要经过身份验证的加密,例如GCM。问题是,我不允许更改Java代码。这可能会引起兴趣:您也可以给我decrypt php函数吗?非常感谢!它很有效。根据你的要求添加。很棒的工作,西昂!这两个神秘函数给了我相同的结果。