使用相同的密钥解密AES时BadPaddingException

编程入门 行业动态 更新时间:2024-10-24 16:26:59
本文介绍了使用相同的密钥解密AES时BadPaddingException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

这是测试人员:

public class CryptographySimpleTests extends ActivityTestCase { public void testsCryptographyClass_encryptAndDecrypt() { final String orgVal = "hi world! :D"; final String key = "key"; try { final byte[] encryptKey = Cryptography.deriveAES256Key(key); final byte[] decryptKey = Cryptography.deriveAES256Key(key); //Deviation method Assert.assertTrue(Arrays.equals(encryptKey, decryptKey)); byte[] encrypted = Cryptography.encryptAES(encryptKey, orgVal.getBytes()); Assert.assertFalse(Arrays.equals(encrypted, orgVal.getBytes())); byte[] decrypted = Cryptography.decryptAES(decryptKey, encrypted); Assert.assertTrue(Arrays.equals(orgVal.getBytes(), decrypted)); } catch (Exception e) { Assert.fail(e.getMessage()); } } }

由于最后一次失败断言:

Wich fails because of the last assert:

Assert.fail(e.getMessage());

尝试执行时:

byte[] decrypted = Cryptography.decryptAES(decryptKey, encrypted);

给出这个堆栈跟踪:

javax.crypto.BadPaddingException: error:06065064:digital envelope routines:EVP_DecryptFinal_ex:bad decrypt at com.android.conscrypt.NativeCrypto.EVP_CipherFinal_ex(Native Method) at com.android.conscrypt.OpenSSLCipher.doFinalInternal(OpenSSLCipher.java:430) at com.android.conscrypt.OpenSSLCipher.engineDoFinal(OpenSSLCipher.java:466) at javax.crypto.Cipher.doFinal(Cipher.java:1340) at bdevel.encuentralo.utils.Cryptography.decryptAES(Cryptography.java:59) at bdevel.encuentralo.CryptographySimpleTests.testsCryptographyClass_encryptAndDecrypt(CryptographySimpleTests.java:32) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at android.test.InstrumentationTestCase.runMethod(InstrumentationTestCase.java:214) at android.test.InstrumentationTestCase.runTest(InstrumentationTestCase.java:199) at junit.framework.TestCase.runBare(TestCase.java:134) at junit.framework.TestResult$1.protect(TestResult.java:115) at junit.framework.TestResult.runProtected(TestResult.java:133) at junit.framework.TestResult.run(TestResult.java:118) at junit.framework.TestCase.run(TestCase.java:124) at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:191) at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:176) at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:555) at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1837)

这些是我的函数:

public class Cryptography { /** * @param key AES Key * @param inputValue Data to encrypt * @return Can return null if something goes wrong */ public static byte[] encryptAES(byte[] key, byte[] inputValue) throws NoSuchPaddingException, BadPaddingException, IllegalBlockSizeException { SecretKeySpec sKeyS = new SecretKeySpec(key, "AES"); Cipher cipher = null; try { cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); cipher.init(Cipher.ENCRYPT_MODE, sKeyS); } catch (NoSuchAlgorithmException | InvalidKeyException i) { cipher = null; } return cipher != null ? cipher.doFinal(inputValue) : null; } public static byte[] decryptAES(byte[] key, byte[] encryptedData) throws NoSuchPaddingException, BadPaddingException, IllegalBlockSizeException { SecretKeySpec sKeyS = new SecretKeySpec(key, "AES"); Cipher cipher = null; try { cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); cipher.init(Cipher.DECRYPT_MODE, sKeyS); } catch (NoSuchAlgorithmException | InvalidKeyException i) { cipher = null; } return cipher != null ? cipher.doFinal(encryptedData) : null; } private static byte[] deriveAES256KeySalt = null; public static byte[] deriveAES256Key(String password) throws InvalidKeySpecException, NoSuchAlgorithmException { /* Store these things on disk used to derive key later: */ int iterationCount = 1000; int saltLength = 32; // bytes; should be the same size as the output (256 / 8 = 32) int keyLength = 256; // 256-bits for AES-256, 128-bits for AES-128, etc /* When first creating the key, obtain a salt with this: */ if(deriveAES256KeySalt == null) { SecureRandom random = new SecureRandom(); deriveAES256KeySalt = new byte[saltLength]; random.nextBytes(deriveAES256KeySalt); } /* Use this to derive the key from the password: */ KeySpec keySpec = new PBEKeySpec(password.toCharArray(), deriveAES256KeySalt, iterationCount, keyLength); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1"); byte[] keyBytes = keyFactory.generateSecret(keySpec).getEncoded(); return keyBytes; } }

如果断言检查密钥是否相同工作,为什么我得到那个例外?

If the assert that checks if the keys are the same works, why do I get that exception ?

推荐答案

你正在吃一个 java.security.InvalidKeyException: encryptAES 和 decryptAES 方法中的非法密钥大小或默认参数异常。所以不要吃它们,要么声明为抛出,要么提升为 RuntimeException 。

You are eating an java.security.InvalidKeyException: Illegal key size or default parameters exception in your encryptAES and decryptAES methods. So don't eat them, either declare as throws or promote to RuntimeException.

原来你有两个问题,因此,你不能做256,但是128解决了这个问题,那么你也在没有 IvParameterSpec (导致 java.security.InvalidKeyException:缺少参数)。所以提供或更改为 ECB :

Turns out you have two problems, for this reason, you can't do 256, but 128 solves that, then you are also requesting CBC without an IvParameterSpec (Which is causing java.security.InvalidKeyException: Parameters missing). So supply that or change to ECB:

public static byte[] encryptAES(byte[] key, byte[] inputValue) throws NoSuchPaddingException, BadPaddingException, IllegalBlockSizeException, NoSuchAlgorithmException, InvalidKeyException { SecretKeySpec sKeyS = new SecretKeySpec(key, "AES"); Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); cipher.init(Cipher.ENCRYPT_MODE, sKeyS); return cipher.doFinal(inputValue); } public static byte[] decryptAES(byte[] key, byte[] encryptedData) throws NoSuchPaddingException, BadPaddingException, IllegalBlockSizeException, NoSuchAlgorithmException, InvalidKeyException { SecretKeySpec sKeyS = new SecretKeySpec(key, "AES"); Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); cipher.init(Cipher.DECRYPT_MODE, sKeyS); return cipher.doFinal(encryptedData); }

密钥长度:

public static byte[] deriveAES256Key(String password) throws InvalidKeySpecException, NoSuchAlgorithmException { ... int keyLength = 128; // 256-bits for AES-256, 128-bits for AES ...

所以我得到了这样的工作,但第一步是停止吃异常,你会得到更好的线索,并可能自己解决。

So I got it working like that, but step one is to stop eating the exception and you'll get better clues and probably work out on own.

更多推荐

使用相同的密钥解密AES时BadPaddingException

本文发布于:2023-11-13 01:45:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1583070.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:密钥   AES   BadPaddingException

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!