【发布时间】:2015-08-04 10:58:15
【问题描述】:
我知道以前有人问过类似的问题,但我找不到特定于我的问题的答案。我正在尝试使用 RSA 加密来获取带有加密像素的图像并使用相同的私钥对其进行解密。尽管当我尝试使用 getRGB() 解密从图像像素中获得的 int 时,我一直遇到问题。这是我的代码:
int pixel = img.getRGB(1, 1);
System.out.println(pixel);
byte[] bytes = ByteBuffer.allocate(4).putInt(pixel).array();
System.out.println(fromByteArray(bytes));
bytes = rsa.RSADecryptB(privk, bytes);
int nom = ByteBuffer.wrap(bytes).getInt(); // big-endian by default
System.out.println(nom);
这是在我的主类中,它从另一个类调用方法 RSADecryptB,这里是那个方法:
public static []byte RSADecryptB(PrivateKey privatekey, byte[] cipherText) {
byte[] ll = null;
try {
/* Create cipher for decryption. */
Cipher decrypt_cipher = Cipher.getInstance("RSA");
decrypt_cipher.init(Cipher.DECRYPT_MODE, privatekey);
ll = decrypt_cipher.doFinal(cipherText);
} catch (IllegalBlockSizeException e) {
System.out.println("1");
e.printStackTrace();
}
catch (InvalidKeyException et) {
System.out.println("2");
et.printStackTrace();
}
catch (NoSuchAlgorithmException ev) {
System.out.println("3");
ev.printStackTrace();
}
catch (BadPaddingException ea) {
System.out.println("4");
ea.printStackTrace();
}
catch (NoSuchPaddingException eo) {
System.out.println("5");
eo.printStackTrace();
}
return ll;
}
当我运行程序时,输出是这样的:
-1606258341
5
4
javax.crypto.BadPaddingException: Decryption error
at sun.security.rsa.RSAPadding.unpadV15(RSAPadding.java:380)
at sun.security.rsa.RSAPadding.unpad(RSAPadding.java:291)
at com.sun.crypto.provider.RSACipher.doFinal(RSACipher.java:363)
at com.sun.crypto.provider.RSACipher.engineDoFinal(RSACipher.java:389)
at javax.crypto.Cipher.doFinal(Cipher.java:2165)
at Encryption5.RSADecryptB(Encryption5.java:126)
at ImageEncryptor.main(ImageEncryptor.java:405)
Exception in thread "main" java.lang.NullPointerException
at java.nio.ByteBuffer.wrap(ByteBuffer.java:396)
at ImageEncryptor.main(ImageEncryptor.java:406)
我不明白为什么会这样。 -1606258341 是像素的值,输出 5 和 4 是因为 NoSuchPaddingException 和 BadPaddingException。如果有人可以帮助解释为什么会发生这种情况以及我做错了什么,那就太好了。谢谢!
编辑:我知道我可能不应该直接使用 RSA,但我自己也在逐个像素地进行加密。我这样做更多是为了练习使用缓冲图像/字节数组/rsa,而不是创建最有效/最安全的程序。此外,当我在我的加密方法中解密整数时,它工作正常。当我从不同的方法解密它时,我得到了错误。此外,加密后的图像被保存到一个文件中,然后提取解密。这也是我的加密方法(它不使用任何填充):
int w = img.getWidth();
int h = img.getHeight();
byte[] bytes = null;
ByteBuffer wrappedo = null;
int nom = 0;
ByteBuffer wrapped = null;
int num = 0;
int pixel = 0;
System.out.println("width, height: " + w + ", " + h);
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
pixel = img.getRGB(j, i);
bytes = ByteBuffer.allocate(4).putInt(pixel).array();
bytes = rsa.RSAEncrypt(bytes);
wrappedo = ByteBuffer.wrap(bytes); // big-endian by default
nom = wrappedo.getInt();
img.setRGB(j, i, nom);
}
}
然后是 RSAEncrypt 方法:
public static byte[] RSAEncrypt(byte[] data) {
byte[] cipherData = null;
try {
PublicKey pubKey = readKeysFromFile();
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, pubKey);
cipherData = cipher.doFinal(data);
}
catch (Exception e) {}
return cipherData;
}
【问题讨论】:
-
您不能只解密 4 个字节的加密输入。那永远行不通。您需要立即解密整个内容。加密只是将单个字节转换为单个字节。顺便说一句,使用 RSA 加密图像也没有多大意义。 RSA 仅限于非常小的消息。图片很大。
标签: java encryption int badpaddingexception