Java AES解密BadPaddingException [英] Java AES decryption BadPaddingException
问题描述
注意:Java NOOB。
note: Java NOOB.
好吧,我知道这里已经回答了几十次,但解决方案似乎没有工作/直接应用到我理解他们的地方。 (是的,我知道我不完全理解加密/解密,AES等等,但这不是重点,我试图理解这一点)
Alright, I know this has been answered a few dozen times on here, but the solutions don't seem to work/apply directly to where I understand them. (Yes, I know I don't completely understand encryption/decryption, AES, etc. but that is not the point, I am trying to understand this)
我有一个实用程序api,我想传递一个字符串并返回一个加密的字符串。然后我想传递加密的字符串,并返回一个解密的字符串。简单。它适用于我传入的许多字符串,但在某些情况下,我得到异常 javax.crypto.BadPaddingException:给定最终块没有正确填充。
I have a utility api where I want to pass a string and return an encrypted string. Then I want to pass the encrypted string, and return a decrypted string. Simple. It works fine for many strings I pass in, but on some, I'm getting the exception javax.crypto.BadPaddingException: Given final block not properly padded.
例如,以下加密/解密罚款。
util / encrypt /?token = 123456789012wha
= 4TR0CbCcQKqeRK73zr83aw ==
util / decrypt /?token = 4TR0CbCcQKqeRK73zr83aw ==
= 123456789012wha
For example, the following encrypts/decrypts fine.
util/encrypt/?token=123456789012wha
= 4TR0CbCcQKqeRK73zr83aw==
util/decrypt/?token=4TR0CbCcQKqeRK73zr83aw==
= 123456789012wha
以下加密,但不解密:
util / encrypt /?token = 123456789012what
= NYaWmwnySoGNHyNmY9Jh + f3O2rqoLI1IAcnsl5V4OCE =
util / decrypt /?token = NYaWmwnySoGNHyNmY9Jh + f3O2rqoLI1IAcnsl5V4OCE =
= exception
The following encrypts, but does not decrypt:
util/encrypt/?token=123456789012what
= NYaWmwnySoGNHyNmY9Jh+f3O2rqoLI1IAcnsl5V4OCE=
util/decrypt/?token=NYaWmwnySoGNHyNmY9Jh+f3O2rqoLI1IAcnsl5V4OCE=
= exception
这是我控制器中的代码:
Here is the code in my controller:
private static final String ALGO = "AES";
@RequestMapping(value = "/util/encrypt/", method = RequestMethod.GET)
@ResponseBody
public String encrypt(HttpServletResponse httpResponse,
@RequestParam(value = "token", required=true) String token) throws Exception
{
Key key = generateKey();
Cipher c = Cipher.getInstance(ALGO);
c.init(Cipher.ENCRYPT_MODE, key);
byte[] encVal = c.doFinal(token.getBytes());
String encryptedValue = Base64.encodeBase64String(encVal);
return encryptedValue.trim();
}
@RequestMapping(value = "/util/decrypt/", method = RequestMethod.GET)
@ResponseBody
public String decrypt(HttpServletResponse httpResponse,
@RequestParam(value = "token", required=true) String token) throws Exception
{
token = URLDecoder.decode(token, "UTF-8");
Key key = generateKey();
Cipher c = Cipher.getInstance(ALGO);
c.init(Cipher.DECRYPT_MODE, key);
byte[] decordedValue = Base64.decodeBase64(token);
byte[] decValue = c.doFinal(decordedValue);
String decryptedValue = new String(decValue);
return decryptedValue.trim();
}
private Key generateKey() throws Exception
{
Key key = new SecretKeySpec(getAesKey().getBytes(), ALGO);
return key;
}
我认为它必须是通话电话 Cipher .getInstance()
我尝试使用 Cipher.getInstance(AES / CBC / PKCS5Padding)
,但在解密时总是失败。我很想知道这里发生了什么以及如何解决它。
I figure it must be something with the call Cipher.getInstance()
and I've tried using Cipher.getInstance("AES/CBC/PKCS5Padding")
but that always fails when decrypting. I would love to really understand what is happening here and how to fix it.
推荐答案
使用函数 encodeBase64URLSafeString 一>。 javadoc说
Use the function encodeBase64URLSafeString. the javadoc says
使用base64算法
的URL安全变体对二进制数据进行编码,但不会对输出进行分块。 url-safe变体发出 - 和_
而不是+和/字符。注意:没有添加填充。
Encodes binary data using a URL-safe variation of the base64 algorithm but does not chunk the output. The url-safe variation emits - and _ instead of + and / characters. Note: no padding is added.
这应该可以解决问题。
这篇关于Java AES解密BadPaddingException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!