使用Java和PHP进行AES加密 [英] AES encryption using Java and PHP

查看:146
本文介绍了使用Java和PHP进行AES加密的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近在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.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]; (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); (int i = 0; i int v = b [i]& 0xFF的;
if(v< 16){
sb.append('0');
}
sb.append(Integer.toHexString(v));
}
return sb.toString()。toUpperCase();
}

你们可以帮助我构建一个PHP脚本,这样返回相同结果?



示例:
明文STACKOVERFLOW被加密为FA652ECCDC39A11A93D2458AA2A0793C。



感谢提前!

解决方案

应该这样做:

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

函数解密($ encrypted,$ key){
$ decryptpted = mcrypt_decrypt(MCRYPT_RIJNDAEL_128,hex2bin($ key),hex2bin($ encrypted),MCRYPT_MODE_ECB);
$ padSize = ord(substr($ decryptpted,-1));
return substr($ decryptpted,0,$ padSize * -1);
}

函数pkcs5_pad($ text,$ blocksize)
{
$ pad = $ blocksize - (strlen($ text)%$ blocksize);
返回$文本。 str_repeat(chr($ pad),$ pad);
}

您发现其他PHP算法返回不同结果的原因可能是因为填充。 Java中的AES的默认值是PKCS5,但是PHP没有本机支持(因此是pkcs5_pad函数)。



正如SLacks所说,你真的不应该使用ECB虽然。如果需要,可以将Java代码更改或重新加密现有数据。只要您继续使用ECB,您就会将您的数据置于危险之中。



信用:从这里


I have recently used the AES algorithm in Java in order to cipher a text. Now I need to rebuild that algorithm in PHP, but I have no idea how, because PHP algorithms on the internet return different results. Maybe you can help me.

This is the Java-code to encrypt:

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();
}

Can you guys possibly help me with building a PHP script, that returns the same results?

Example: The plaintext "STACKOVERFLOW" is encrypted to "FA652ECCDC39A11A93D2458AA2A0793C".

Thanks in advance!

解决方案

This should do it:

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);
}

The reason that other PHP algorithms you found were returning different results is likely because of the padding. The default for AES in Java is PKCS5, but PHP doesn't have native support for this (hence the pkcs5_pad function).

As SLacks has said, you really should not be using ECB though. Either get the Java code changed or re-encrypt your existing data if need be. As long as you continue using ECB, you're putting your data at risk.

Credit: Padding function taken from here.

这篇关于使用Java和PHP进行AES加密的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆