- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有这种加密方法:
private byte[] encrypt(byte[] data) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException,
BadPaddingException {
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
cipher.init(Cipher.ENCRYPT_MODE, myPublicKey);
ByteArrayInputStream input = new ByteArrayInputStream(data);
ByteArrayOutputStream output = new ByteArrayOutputStream();
byte[] buffer = new byte[64];
int bytes;
ByteArrayOutputStream aux;
try {
while ((bytes = input.read(buffer)) != -1) {
aux = new ByteArrayOutputStream();
aux.write(buffer, 0, bytes);
byte[] fragment = aux.toByteArray();
byte[] encryptedFragment = cipher.doFinal(fragment);
output.write(encryptedFragment);
}
} catch (IOException e) {
e.printStackTrace();
}
byte[] result = output.toByteArray();
return result;
}
这个用于解密:
public static String decrypt(byte[] data) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException, IOException {
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
cipher.init(Cipher.DECRYPT_MODE, myPrivateKey);
int bitLenght = ((java.security.interfaces.RSAPrivateKey) privateKey).getModulus().bitLength();
int blockSize = bitLenght / 8;
byte[] buffer = new byte[blockSize];
int bytes;
byte[] decrypted;
ByteArrayOutputStream aux;
ByteArrayInputStream input = new ByteArrayInputStream(data);
ByteArrayOutputStream output = new ByteArrayOutputStream();
while ((bytes = input.read(buffer)) != -1) {
aux = new ByteArrayOutputStream();
aux.write(buffer, 0, bytes);
byte[] fragment = aux.toByteArray();
byte[] decryptedFragment = cipher.doFinal(fragment);
output.write(decryptedFragment);
}
decrypted = output.toByteArray();
return new String(decrypted);
}
但是我遇到了这个异常:
javax.crypto.BadPaddingException: Decryption error
正如我所见,我已将密码配置为具有相同的 PKCS1Padding
,因此我无法猜测为什么会出现该错误。
我已按如下方式创建了私钥:
openssl genrsa -out myPrivateKey.key 2048
还有公共(public)的:
openssl rsa -in myPrivateKey.pem -pubout -out myPublicKey.key
据我所知,通过该命令它们都是 PKCS1,事实上我的私钥以 -----BEGIN RSA PRIVATE KEY-----
开头。
我错过了什么?
注意:我也尝试过使用 blockSize = 64
,结果相同。
最佳答案
加密流 - 正确地,您应该在循环中使用 cipher.update(..)
,并在处理所有数据后仅调用一次 .doFinal(..)
。
解密时,如果您对部分消息调用doFinal
,您可能会遇到异常。不管怎样,从您的代码中看不出来这是否是您面临的问题。 (假设您已正确导入 key 对)
事实上,RSA 仅适用于短消息(117 字节)。否则您可以搜索“混合加密”
P。 S.:你处理流和数组的方式迫切需要优化,所以也看看它,但那是针对不同的问题
关于java - 尝试解密时出现 BadPaddingException - Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59411645/
我正在尝试在我的 settings.xml 中加密服务器密码,并且在尝试部署 Artifact 时遇到此异常。 Caused by: javax.crypto.BadPaddingException:
我的应用程序提示用户输入用于加密控制文件的密码。如果输入了错误的密码,应用程序将通过创建新的控制文件进行响应。因此,我需要捕获 BadPaddingException,以便我可以触发适当的响应。 这是
如果我使用错误的 key 或错误的盐进行解密,则会抛出 BadPaddingException。我希望返回一个不正确的字符串。doFinal() 导致解密方法出现异常 消息:这只是一个示例 Unfug
这就是我创建 AES 密码的方式,但我仍然在 doFinal() 解密 block 中收到 BadPaddingException 错误 // Get the key generator
我正在使用 javax.crypto.Cipher 加密文件,然后解密它,但我仍然收到 BadPaddingException。以下类接收来自输入文件的 inputStream 和来自输出文件的 ou
我创建了一个Encryption类来加密/解密我的Android项目中的二进制数据,通过这个link . package com.my.package; import java.security.Se
在 Java 中使用 RSA 加密/解密时,我遇到了一个非常特殊的问题。 示例代码: KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA"
我正在使用以下(片段)代码来加密用户的私有(private)数据以存储在数据库中,然后解密以便他们可以查看。 Cipher cipher = Cipher.getInstance("DESede");
这是我的加密设置: public static String encryptionAlgorithm = "AES"; public static short encryptionBitCount =
这是我的加密设置: public static String encryptionAlgorithm = "AES"; public static short encryptionBitCount =
我正在研究 AES 算法,但我遇到了无法解决的异常。 javax.crypto.BadPaddingException: Given final block not properly padded a
我有这种加密方法: private byte[] encrypt(byte[] data) throws NoSuchAlgorithmException, NoSuchPaddingExceptio
我正在使用对称 key (AES)加密文件,然后使用 rsa key 加密 key 。 加密工作正常,但在解密时出现错误: 这是堆栈跟踪: http://pastebin.com/37AB7EPH 我
我的 RSA 加密程序中出现 BadPaddingException。我不知道为什么会发生。 import java.io.ByteArrayInputStream; import java.io.B
我正在加密字符串并将它们写入文本文件。为了解密内容,我正在读取该文件并打印解密的数据。当我使用一个字符串值测试我的代码时,它的加密和解密效果非常好;但是,当我添加更多字符串进行加密时,加密工作正常,但
我正在尝试创建 AES 加密/解密方法,但如果不使用 AES/ECB/NoPadding,我似乎无法获得原始输入。现在我尝试使用 AES/CBC/PKCS7Padding。我已经确认从文件读取和写入字
我试图从这里收集有关加密/解密的所有可能信息。修补它,一些成功和失败。 但现在我已经应用了代码及其成功与否。某些文件(exe 或 msi 的)正在运行,但它们仍然给出有关 BadPaddingExce
我正在尝试运行一个简单的加密/解密程序。我收到填充异常。一定有什么隐藏的东西是我不知道的。我基本上加密了一个字符串,将其写入文件,读回,然后解密。原加密数组解密没有问题。我将原始加密数组与从文件中读回
我已获得一个 RSA 公钥和一个带有 OAEP SHA-256 填充的 RSA 私钥。我试图简单地加密一个随机字符串,然后解密它以断言结果等于原始结果。 这是我得到的公钥: -----BEGIN RS
我正在对我的文件进行加密/解密。代码如下。我找不到问题所在。我错过了什么吗?我需要实现 128 位 AES 加密。有什么不对的吗?以错误结束 "javax.crypto.BadPaddingExce
我是一名优秀的程序员,十分优秀!