Java 谁能告诉我在使用RSA的图像加密和解密过程中犯了什么错误

Java 谁能告诉我在使用RSA的图像加密和解密过程中犯了什么错误,java,encryption,cryptography,Java,Encryption,Cryptography,我已经将图像分割成一定数量的子图像,并对每个子图像执行加密和解密过程。之后,我将所有加密的字节存储到一个jpg文件中,并将解密的字节存储到另一个jpg文件中。 但是我从解密文件和原始图像中得到的字节不一样。 代码如下 import java.awt.Graphics2D; import java.awt.image.BufferedImage; import java.io.File; import java.io.FileInputStream; import java.io.IOExcept

我已经将图像分割成一定数量的子图像,并对每个子图像执行加密和解密过程。之后,我将所有加密的字节存储到一个jpg文件中,并将解密的字节存储到另一个jpg文件中。 但是我从解密文件和原始图像中得到的字节不一样。 代码如下

import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.math.BigInteger;

import javax.imageio.ImageIO;
public class ImageSplitTest {
    private static final BigInteger N = null;
    public ImageSplitTest() {
        // TODO Auto-generated constructor stub
    }

    public static void main(String[] args) throws IOException {
        // TODO Auto-generated method stub
        File file = new File("C:\\Users\\Shwetha\\workspace\\penguins.jpg"); // I have bear.jpg in my working directory  
        byte b[]=rsa1.readBytesFromFile(file);
        System.out.println("original image bytes"+ b);
        FileInputStream fis = new FileInputStream(file);  
        BufferedImage image = ImageIO.read(fis); //reading the image file  

        int rows = 2; //You should decide the values for rows and cols variables  
        int cols = 2;  
        int chunks = rows * cols;  

        int chunkWidth = image.getWidth() / cols; // determines the chunk width and height  
        int chunkHeight = image.getHeight() / rows;  
        int count = 0;  
        BufferedImage imgs[] = new BufferedImage[chunks]; //Image array to hold image chunks  
        for (int x = 0; x < rows; x++) {  
            for (int y = 0; y < cols; y++) {  
                //Initialize the image array with image chunks  
                imgs[count] = new BufferedImage(chunkWidth, chunkHeight, image.getType());  

                // draws the image chunk  
                Graphics2D gr = imgs[count++].createGraphics();  
                gr.drawImage(image, 0, 0, chunkWidth, chunkHeight, chunkWidth * y, chunkHeight * x, chunkWidth * y + chunkWidth, chunkHeight * x + chunkHeight, null);  
                gr.dispose();  
            }  
        }  
        System.out.println("Splitting done");  

        //writing mini images into image files  
        for (int i = 0; i < imgs.length; i++) {  
            ImageIO.write(imgs[i], "jpg", new File("img" + i + ".jpg"));  


        }  
        System.out.println("Mini images created");  
       rsa1 rsa=new rsa1();
        BufferedImage[] buffImages = new BufferedImage[chunks];  
        byte[] decrypted=null;
        byte[] bytesImage=null;
        for (int i = 0; i < imgs.length; i++) {  
         bytesImage= rsa.readBytesFromFile(new File(
                "img" + i + ".jpg"));
          //readBytesFromFile: method to read file as bytes


     byte[] encrypted = rsa.encrypt(bytesImage);    
         rsa.writeBytesToFile(new File(
                "C:\\Users\\Shwetha\\workspace\\encrypted.jpg"),encrypted );

      //writeBytesToFile: method to write as bytes

    // decrypt
         decrypted = rsa.decrypt(encrypted); 
        rsa. writeBytesToFile(new File(
                "C:\\Users\\Shwetha\\workspace\\decrypted1.jpg"),decrypted );
    }
     System.out.println("bytes from decryption"+decrypted);  
     System.out.println("bytes before encryption"+bytesImage);
}

}
导入java.awt.Graphics2D;
导入java.awt.image.buffereImage;
导入java.io.File;
导入java.io.FileInputStream;
导入java.io.IOException;
导入java.math.biginger;
导入javax.imageio.imageio;
公共类图像分割测试{
私有静态final biginger N=null;
公共ImageSplitTest(){
//TODO自动生成的构造函数存根
}
公共静态void main(字符串[]args)引发IOException{
//TODO自动生成的方法存根
File File=new File(“C:\\Users\\Shwetha\\workspace\\penguins.jpg”);//我的工作目录中有bear.jpg
字节b[]=rsa1.readBytesFromFile(文件);
System.out.println(“原始图像字节”+b);
FileInputStream fis=新的FileInputStream(文件);
buffereImage image=ImageIO.read(fis);//读取图像文件
int rows=2;//您应该决定rows和cols变量的值
int cols=2;
int chunks=行*列;
int chunkWidth=image.getWidth()/cols;//确定块的宽度和高度
int chunkHeight=image.getHeight()/行;
整数计数=0;
BuffereImage imgs[]=新BuffereImage[chunks];//用于保存图像块的图像数组
对于(intx=0;x
rsa1 calss中提供了readBytesFromFile、加密和解密方法

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigInteger;
import java.util.Random;

public class rsa1 {
    private BigInteger p;
    private BigInteger q;
    private BigInteger N;
    private BigInteger phi;
    private static BigInteger e;
    private BigInteger d;
    private int bitlength = 1024;

    private Random r;
     public rsa1() {
        r = new Random();
        p = BigInteger.probablePrime(bitlength, r);
        q = BigInteger.probablePrime(bitlength, r);
        N = p.multiply(q);

        phi =   p.subtract(BigInteger.ONE).multiply(q.subtract(BigInteger.ONE));
        e = BigInteger.probablePrime(bitlength/2, r);
        System.out.println("e : "+e);

        while (phi.gcd(e).compareTo(BigInteger.ONE) > 0 && e.compareTo(phi) < 0 ) {
            e.add(BigInteger.ONE);
        }
       d = e.modInverse(phi);
     }

    public byte[] encrypt(byte[] image) {   
       byte[] encryptedImage = new byte[image.length];
      for (int i =0 ; i< image.length; i++){
          encryptedImage[i]= (BigInteger.valueOf(image[i])).modPow(e, N).byteValue(); 

       }   
        return encryptedImage;
    }
    public byte[] decrypt(byte[] image) {   
       byte[] decryptedImage = new byte[image.length];
       for (int i =0 ; i< image.length; i++){
           decryptedImage[i]= (BigInteger.valueOf(image[i])).modPow(d, N).byteValue();

        } 

        return decryptedImage;

        }
    public static byte[] readBytesFromFile(File file) throws IOException {
        InputStream is = new FileInputStream(file);

           // Get the size of the file
           long length = file.length();

           // You cannot create an array using a long type.
           // It needs to be an int type.
           // Before converting to an int type, check
           // to ensure that file is not larger than Integer.MAX_VALUE.
           if (length > Integer.MAX_VALUE) {
             throw new IOException("Could not completely read file " + file.getName() + " as it is too long (" + length + " bytes, max supported " + Integer.MAX_VALUE + ")");
           }

           // Create the byte array to hold the data
           byte[] bytes = new byte[(int)length];

           // Read in the bytes
           int offset = 0;
           int numRead = 0;
           while (offset < bytes.length && (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0) {
               offset += numRead;
           }

           // Ensure all the bytes have been read in
           if (offset < bytes.length) {
               throw new IOException("Could not completely read file " + file.getName());
           }

           // Close the input stream and return bytes
           is.close();
           return bytes;
        // TODO Auto-generated method stub

     }


     }
import java.io.BufferedOutputStream;
导入java.io.File;
导入java.io.FileInputStream;
导入java.io.FileNotFoundException;
导入java.io.FileOutputStream;
导入java.io.IOException;
导入java.io.InputStream;
导入java.math.biginger;
导入java.util.Random;
公共类rsa1{
私有整数bigp;
私有大整数q;
私有大整数N;
私有大整数phi;
私有静态大整数;
私有大整数d;
私有整数位长度=1024;
私有随机r;
公共服务1(){
r=新随机数();
p=BigInteger.probablePrime(位长,r);
q=BigInteger.probablePrime(位长度,r);
N=p.乘以(q);
phi=p.subtract(biginger.ONE)。乘法(q.subtract(biginger.ONE));
e=BigInteger.probablePrime(位长/2,r);
System.out.println(“e:+e”);
while(phi.gcd(e).compareTo(biginger.ONE)>0&&e.compareTo(phi)<0){
e、 加法(biginger.ONE);
}
d=e.modInverse(φ);
}
公共字节[]加密(字节[]图像){
byte[]encryptedImage=新字节[image.length];
对于(int i=0;i整数最大值){
抛出新IOException(“无法完全读取文件”+file.getName()+,因为它太长(“+length+”字节,支持的最大值“+Integer.max_VALUE+”));
}
//创建字节数组以保存数据
字节[]字节=新字节[(int)长度];
//读入字节
整数偏移=0;
int n