当前位置:   article > 正文

aes-gcm-256 php加密 java解密成功 java php有区别!!_php gcm 加密解密

php gcm 加密解密

我在公司折腾了很久,php加密,java端解密不成功,翻墙后找到了解决问题的关键。

下面是外网答案:

使用 AES GCM 模式的 PHP 和 Java 之间的跨平台加密正在运行。有一些细节可能会阻止你成功。

首先:在 PHP 端,openssl_encrypt 返回一个 base64 编码的密文,当将密文与版本、iv 和标签连接时,该密文再次被 base64 编码。为了避免这种情况,我将 OPENSSL 选项设置为“OPENSSL_RAW_DATA”。

第二:在Java端,标签被附加到密文中,因此“密文|标签”可以直接被使用。

请注意:我的示例只是展示了 PHP 端的加密和 Java 端的解密将如何工作,但可能与您的源代码无关(Java 端特别) - 我懒得采用我的例子: -)

这是 PHP 端的输出:

  1. AES GCM in PHP/Java
  2. ciphertext: djAx/kMbxfJI5Zx7lTWeDbw601cD2wkjBvuKeVBbKOZHll98GstPNfi1xHvyRlBwJDQ6YWvpymsk76kwbBbD0cBsOzzK/tH8UpA=

将密文复制到Java程序并让它运行:

  1. AES GCM in PHP/Java
  2. decryptedtext: The quick brown fox jumps over the lazy dog

您可以在下面找到这两个程序的源代码。安全警告:代码使用固定和硬编码的密钥 - 不要这样做。这些程序没有任何异常处理,仅用于教育目的。

该代码在 PHP > 7.2 和 Java 11+ 上运行。

PHP代码:

  1. <?php
  2. function encrypt($key, $textToEncrypt){
  3. $cipher = 'aes-256-gcm';
  4. $iv_len = 12;
  5. $tag_length = 16;
  6. $version_length = 3;
  7. $version = "v01";
  8. $iv = openssl_random_pseudo_bytes($iv_len);
  9. $tag = ""; // will be filled by openssl_encrypt
  10. $ciphertext = openssl_encrypt($textToEncrypt, $cipher, $key, OPENSSL_RAW_DATA, $iv, $tag, "", $tag_length);
  11. $encrypted = base64_encode($version.$iv.$ciphertext.$tag);
  12. return $encrypted;
  13. }
  14. echo 'AES GCM in PHP/Java' . PHP_EOL;
  15. // ### security warning: never use hardcoded keys in source ###
  16. $key = '12345678901234567890123456789012';
  17. $plaintext = 'The quick brown fox jumps over the lazy dog';
  18. $ciphertext = encrypt($key, $plaintext);
  19. echo 'ciphertext: ' . $ciphertext . PHP_EOL;
  20. ?>

Java代码:

  1. import javax.crypto.BadPaddingException;
  2. import javax.crypto.Cipher;
  3. import javax.crypto.IllegalBlockSizeException;
  4. import javax.crypto.NoSuchPaddingException;
  5. import javax.crypto.spec.GCMParameterSpec;
  6. import javax.crypto.spec.SecretKeySpec;
  7. import java.nio.charset.StandardCharsets;
  8. import java.security.InvalidAlgorithmParameterException;
  9. import java.security.InvalidKeyException;
  10. import java.security.NoSuchAlgorithmException;
  11. import java.util.Arrays;
  12. import java.util.Base64;
  13. public class SO_final {
  14. public static void main(String[] args) throws NoSuchPaddingException, InvalidAlgorithmParameterException, NoSuchAlgorithmException, IllegalBlockSizeException, BadPaddingException, InvalidKeyException {
  15. System.out.println("AES GCM in PHP/Java");
  16. // https://stackoverflow.com/questions/65001817/aes-gcm-in-php-java
  17. String ciphertext = "djAx/kMbxfJI5Zx7lTWeDbw601cD2wkjBvuKeVBbKOZHll98GstPNfi1xHvyRlBwJDQ6YWvpymsk76kwbBbD0cBsOzzK/tH8UpA=";
  18. // ### security warning: never use hardcoded keys in source ###
  19. byte[] key = "12345678901234567890123456789012".getBytes(StandardCharsets.UTF_8);
  20. String decryptedtext = decryptGcmBase64(key, ciphertext);
  21. System.out.println("decryptedtext: " + decryptedtext);
  22. }
  23. public static String decryptGcmBase64(byte[] key, String ciphertextBase64) throws
  24. NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException,
  25. InvalidAlgorithmParameterException, BadPaddingException, IllegalBlockSizeException {
  26. byte[] ciphertextComplete = Base64.getDecoder().decode(ciphertextBase64);
  27. // split data
  28. // base64 encoding $encrypted = base64_encode($version.$iv.$ciphertext.$tag);
  29. byte[] version = Arrays.copyOfRange(ciphertextComplete, 0, 3); // 3 bytes
  30. byte[] iv = Arrays.copyOfRange(ciphertextComplete, 3, 15); // 12 bytes
  31. byte[] ciphertextWithTag = Arrays.copyOfRange(ciphertextComplete, 15, ciphertextComplete.length);
  32. SecretKeySpec secretKeySpec = new SecretKeySpec(key, "AES");
  33. GCMParameterSpec gcmParameterSpec = new GCMParameterSpec(16 * 8, iv);
  34. Cipher cipher = Cipher.getInstance("AES/GCM/PKCS5Padding");//NOPadding
  35. cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, gcmParameterSpec);
  36. return new String(cipher.doFinal(ciphertextWithTag), StandardCharsets.UTF_8);
  37. }
  38. }
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/176569
推荐阅读
相关标签
  

闽ICP备14008679号