简体   繁体   English

java AES加密然后使用相同的密钥和IV在PHP中解密

[英]java AES encryption then decrypt in PHP using same Key and IV

I follow this guide AES encryption on Java side - decryption on PHP side and selecting a single key but the accepted answer is not really working.在 Java 端遵循本指南AES 加密 - 在 PHP 端解密并选择单个密钥,但接受的答案并没有真正起作用。 I even hardcoded the KEY and IV values in java so that i am sure that i have the same values in PHP.我什至在 Java 中对 KEY 和 IV 值进行了硬编码,以便我确定我在 PHP 中具有相同的值。

private static final byte[] keyValue =
        new byte[] { 'T', 'h', 'e', 'B', 'e', 's', 't',
                'S', 'e', 'c', 'r','e', 't', 'K', 'e', 'y' };

private static final byte[] ivValue =
        new byte[] { '0', '1', '2', '3', '4', '5', '6',
                '7', '8', '9', '0','1', '2', '3', '4', '5' };

JAVA (key) generation: JAVA(密钥)生成:

String ALGO = "AES/CBC/ZeroBytePadding";
Key key = new SecretKeySpec(keyValue, ALGO);

JAVA (iv) generation: JAVA(四)代:

IvParameterSpec ivSpec = new IvParameterSpec(ivValue); 

Then i use it to Encrypt / Decrypt in JAVA like this:然后我用它来像这样在 JAVA 中加密/解密:

  //ENCRYPTION
  String encrypted = encrypt("Hello World!",key,ivSpec);

  //DECRYPTION
  String decrypted = decrypt(encrypted,key,ivSpec);

public static String encrypt(String Data, Key key, IvParameterSpec ivSpec) throws Exception {

    Cipher c = Cipher.getInstance(ALGO);
    c.init(Cipher.ENCRYPT_MODE, key, ivSpec);       
    byte[] encVal = c.doFinal(Data.getBytes());     
    String encryptedValue = Base64.encodeToString(encVal, Base64.NO_WRAP);
    return encryptedValue;
}


public static String decrypt(String encryptedData, Key key, IvParameterSpec ivSpec) throws Exception {

    Cipher c = Cipher.getInstance(ALGO);
    c.init(Cipher.DECRYPT_MODE, key, ivSpec);   
    byte[] decordedValue = Base64.decode(encryptedData,Base64.NO_WRAP);
    byte[] decValue = c.doFinal(decordedValue);
    String decryptedValue = new String(decValue);
    return decryptedValue;
}

NOW, in PHP i get the encrypted value thru POST and i hardcoded the KEY and IV value:现在,在 PHP 中,我通过 POST 获得加密值,并对 KEY 和 IV 值进行硬编码:

$encrypted = $_POST["encrypted"];
$key = "TheBestSecretKey";
$iv = "0123456789012345";

$decrypted = decryptMessage($encrypted,$key,$iv);

function decryptMessage($encrypted,$key,$iv)
{
   $ivNum = (int)$iv;
   $ivIn = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC), $ivNum);
   $decrypted = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key, $encrypted, MCRYPT_MODE_CBC, $ivIn);
}

Encrypted Message: TXCv3f+r+h71y/NzCk08Hw==加密消息:TXCv3f+r+h71y/NzCk08Hw==

Expected Result of (decrypted) variable: "Hellow World!" (解密)变量的预期结果:“Hellow World!”

Current Result of (decrypted) variable: @ 6 I Ԗmݕ WMu (解密)变量的当前结果: @ 6 I Ԗm 补 WMu

Is there any other workaround to achieve my expected result.有没有其他解决方法可以达到我的预期结果。 Thank you in advance.先感谢您。

You need to provide the key, iv, input data and encrypted data in hex so they can be examined, encryption works with data, not strings.您需要以十六进制形式提供密钥、iv、输入数据和加密数据,以便对其进行检查,加密适用于数据,而不是字符串。

key: 546865426573745365637265744b6579密钥:546865426573745365637265744b6579
iv: 30313233343536373839303132333435四:30313233343536373839303132333435
text: 48656c6c6f7720576f726c6421文本:48656c6c6f7720576f726c6421

and encryption in CBC mode with zero padding you should get encrypted:并在 CBC 模式下使用零填充进行加密,您应该加密:

AE64AA4836D7251E03070C1647A4B531 AE64AA4836D7251E03070C1647A4B531

but you don't, you get但你没有,你得到

4D70AFDDFFABFA1EF5CBF3730A4D3C1F 4D70AFDDFFABFA1EF5CBF3730A4D3C1F

So it seems the encryption is bad.所以看起来加密很糟糕。

See: AES CALCULATOR请参阅: AES 计算器

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

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