我试图用java中的RSA加密BMP图像,它应该创建加密和解密的图像。
好的,所以在阅读评论后得知单独使用RSA是不安全的;我编辑了我的问题。和tryd Java Cryptography但cipher.doFinal()不接受超过245字节的数据
File bmpFile = new File("C:\\Users\\acer\\Desktop\\py\\6.bmp");
BufferedImage image = ImageIO.read(bmpFile);
ByteArrayOutputStream baos=new ByteArrayOutputStream();
ImageIO.write(image, "bmp", baos );
byte[] b = baos.toByteArray();
byte[] b1=new byte[b.length];
KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("RSA");
keyPairGen.initialize(2048);
KeyPair pair = keyPairGen.generateKeyPair();
PublicKey publicKey = pair.getPublic();
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
cipher.update(b);
b1 = cipher.doFinal();
bmpFile=new File("C:\\Users\\acer\\Desktop\\py\\66.bmp");
FileOutputStream fos = new FileOutputStream(bmpFile);
fos.write(b1);
fos.flush();
fos.close();
它给了:
Exception in thread "main" javax.crypto.IllegalBlockSizeException: Data must not be longer than 245 bytes
at com.sun.crypto.provider.RSACipher.doFinal(RSACipher.java:344)
at com.sun.crypto.provider.RSACipher.engineDoFinal(RSACipher.java:389)
at javax.crypto.Cipher.doFinal(Cipher.java:2048)
我的大多数图像都是198x135,我在堆栈中找到了onverflow
The RSA algorithm can only encrypt data that has a maximum byte length of the RSA key length in bits divided with eight minus eleven padding bytes, i.e. number of maximum bytes = key length in bits / 8 - 11.
并说你必须用对称密钥加密数据并用rsa加密对称密钥。但是我想用RSA加密数据。
我去问,我必须将加密图像发送到其他电脑,但问题是p,q是随机的
非对称加密意味着对特定目标(公钥)进行加密。
所以步骤是:
因此,如果您想使用RSA加密任何数据,则参数对于目标接收器是随机的,但是为发送者提供。
我是否必须使用像RC4这样的对称算法加密主题并使用图像发送主题
正如您可能已经发现的那样,RSA操作非常缓慢。因此,使用RSA的常见方法是混合加密 - 使用随机对称加密密钥加密数据,并使用RSA仅加密随机密钥。
image.setRGB(i,j,pixels [i] [j] .intValue());
这不行。任何数据的加密都将具有密钥长度的长度。实际上,在您的情况下,每个图像像素需要1024位。将bigint修剪为intValue,您将丢失信息。
这就是使用(已经提到的)混合加密的原因
这是我学校项目的一部分
如果您将RSA用于现实生活中的项目: