使用RC6算法在Java中加密图像

使用RC6算法在Java中加密图像,java,image,encryption,Java,Image,Encryption,我应该使用RC6算法加密图像。 加密后我得到的是相同的图像(即旧图像)。 我使用Java BuffereImage类读取图像,使用ImageIO.getRGB获取每个像素并加密像素。 加密函数和密钥调度算法 加密函数 private static byte [] encryptImage (byte[] input,byte[] key){ S = generatingKey(key); byte[] out = new byte[input.length]; System.out.prin

我应该使用RC6算法加密图像。 加密后我得到的是相同的图像(即旧图像)。 我使用Java BuffereImage类读取图像,使用ImageIO.getRGB获取每个像素并加密像素。 加密函数和密钥调度算法

加密函数

private static byte [] encryptImage (byte[] input,byte[] key){

S = generatingKey(key);
byte[] out = new byte[input.length];

System.out.println("Out byte: "+out.length);
int[] temp = new int[input.length/4];
System.out.println("Input byte: "+input.length/4);
System.out.println("temp byte: "+temp.length);
for(int i=0;i<temp.length;i++) {
    temp[i] =0;
}

temp = convertByteToInt(input,temp.length);

int X=0,Y=0,Z=0,W=0;

X=temp[0]; 
//System.out.println("x:"+X);
Y=temp[1];
//System.out.println("y"+Y);
Z=temp[2];
//System.out.println("z"+Z);
W=temp[3];
//System.out.println("W"+W);

Y = Y + S[0];
W = W + S[1];

int t,u,flag;
for(int i=1;i<=r;i++) {
    t = rotateLeft(Y*(2*Y+1),5);
    u = rotateLeft(W*(2*W+1),5);
    X = rotateLeft(X^t,u)+ S[2*i];
    Z = rotateLeft(Z^u,t)+ S[2*i+1];

    flag = X;
    X = Y;
    Y = Z;
    Z = W;
    W = flag;

}
X= X + S[2*r+2];
Z = Z + S[2*r+3];

temp[0] = X;
temp[1] = Y;
temp[2] = Z;
temp[3] = W;

out = convertIntToByte(temp,input.length);

return out;
私有静态字节[]加密图像(字节[]输入,字节[]密钥){
S=生成键(键);
byte[]out=新字节[input.length];
System.out.println(“输出字节:“+out.length”);
int[]temp=new int[input.length/4];
System.out.println(“输入字节:“+Input.length/4”);
系统输出打印项次(“温度字节:“+温度长度”);
对于(int i=0;i
public static int[] generatingKey(byte[] userKey) {
        int sizeOfS = 2*r+4;

        int []S = new int[sizeOfS];


        int keyLength = userKey.length/(w/8);
        //System.out.println("KeyLength: "+keyLength);

        int []L = bytestoWords(userKey,keyLength);

        S[0] = PW;
        for(int i=1;i<sizeOfS;i++) {
            S[i] = S[i-1] + QW;
        }

        int X=0,Y=0,Z=0,W=0;

        int v = 3*Math.max(keyLength,sizeOfS);

        for(int j=0;j<v;j++) {
            X= S[Z] = rotateLeft((S[Z]+X+Y),3);
            Y= L[W] = rotateRight(L[W]+X+Y,X+Y);
            Z=(Z+1)%sizeOfS;
            W=(W+1)%keyLength;
        }

        return S;
    }
BufferedReader keyReader = new BufferedReader(
                new InputStreamReader(System.in));
        System.out.println("Enter user input: ");
        byte  [] userKey = new byte[16]; 
        for(int i=0;i<userKey.length;i++) {
            //read userInput and cast to byte
            userKey[i] =(byte)Integer.parseInt(keyReader.readLine());
            //System.out.println(userKey[i]);
        }

        //          
        BufferedImage img;
        File f = new File("./src/images/minion.jpg");
        try {
            img = ImageIO.read(f);
            int height = img.getHeight();
            int width = img.getWidth(); 


            byte pixelarray[] = new byte[height*width*3];
            int index = 0;
            for(int i=0;i<height;i++) {
                for(int j=0;j<width;j++) {
                    Color c = new Color(img.getRGB(j,i));
                    byte r =(byte)c.getRed();
                    byte g =(byte)c.getGreen();
                    byte b =(byte)c.getBlue();
                    pixelarray[index++] = r;
                    pixelarray[index++] = g;
                    pixelarray[index++] = b;
                }

            }

            byte [] pixel = new byte[height*width*3];

            pixel = encryptImage(pixelarray,userKey);

            BufferedImage newImage =new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);

            int index1 = 0;
            for(int i=0;i<height;i++) {
                for(int j=0;j<width;j++) {

                    int r = pixel[index1++]&0xFF;
                    int g = pixel[index1++]&0xFF;
                    int b = pixel[index1++]&0xFF;

                    //                      System.out.println("R: "+r);
                    //                      System.out.println("G: "+g);
                    //                      System.out.println("B: "+b);

                    Color newColor = new Color(r,g,b);

                    newImage.setRGB(j,i, newColor.getRGB());
                }
            }
            //              File outImage = new
            ImageIO.write(newImage, "jpg", new File("./src/images/newout.jpg"));
        }catch(IOException e) {
            System.out.println("Exception occured :" + e.getMessage());
        }
        System.out.println("Images were written succesfully."); 
    }