使用 java netbeans 对图像进行 rsa 加密和解密

cryptage et decryptage rsa sur une image utilisant java netbeans

我正在开发一个可以使用 RSA 算法加密和解密图像(特定选择)的应用程序,一切正常,但有些像素表现异常,我不明白为什么!我对 encrypt/decrypt 使用相同的参数并保存图像,但是,当我创建新图像并尝试读取加密区域中的像素时,我没有得到我的程序之前显示给我的像素。

File img = new File (Path); 
bf1  = ImageIO.read(img);  
marchdanslImage(bf1,captureRect); // only selected rectangle (captureRect) from image will be treated

///////我之前调用的函数

private void marchdanslImage(BufferedImage image , Rectangle REC) throws IOException {

    bf2 = new BufferedImage(REC.width, REC.height, BufferedImage.TYPE_INT_RGB); //this image gonna contain the pixels after encryption  

    for (int i = y; i < h; i++) {
    for (int j = x; j < w; j++) {
      int pixel = image.getRGB(j, i);//orginal values
      printPixelARGB(pixel,j,i); //here i call the code to crypt or decrypt
      bf2.setRGB(j-x,i-y, rgb); //new values
      } }
}

函数printPixelARGB的代码:

public void printPixelARGB(int pixel,int i , int j) {

 r[i][j] = (pixel >> 16) & 0xff; // original values
rr[i][j] = RSA_crypt_decrypt(r[i][j], appel);//values after treatment 
 g[i][j] = (pixel >> 8) & 0xff;
gg[i][j] = RSA_crypt_decrypt(g[i][j], appel);
 b[i][j] = (pixel) & 0xff;
bb[i][j] = RSA_crypt_decrypt(b[i][j], appel);
     rgb = rr[i][j];// new values on rgb to be set in bf2
     rgb = (rgb << 8) + gg[i][j];
     rgb = (rgb << 8) + bb[i][j];
}

最后保存我的工作:

public void save_image()
 {
     Graphics2D g;

     g = (Graphics2D) bf1.getGraphics();
     g.drawImage(bf2, captureRect.x,  captureRect.y,  captureRect.width, captureRect.height, null);
     g.dispose();
   //i draw the crypted pixels on my original image and create new image
    File outputFile = new File("C:/USERS/HP/DesKtop/output.jpg");
    try {
        ImageIO.write(bf1, "jpg", outputFile);
        } catch (IOException ex) {
        Logger.getLogger(MenuGenerale2.class.getName()).log(Level.SEVERE, null, ex);
    }

}

到目前为止一切正常,但是当打开我创建的图像并尝试解密时,我得到的值不一样,经过处理!

是因为省钱的部分吗?当我在白色图像上尝试时它无法正常工作,但在另一张图像上它根本不起作用!已经3周了没能解决这个问题...我真的很需要帮助。

这是我的应用程序的 link: www.fichier-rar.fr/2016/04/23/cryptagersa11/

问题是您使用 JPEG 压缩格式保存图像。 JPEG 压缩 不会 准确地保存数据:它是 lossy compression.

如果您使用 BMP 或 PNG 文件,则不会发生此问题。

你可能想调查一下隐写术,尽管我怀疑这与你想要实现的目标相反