0
votes

I'm trying to encrypt a bmp image with RC4 but the encrypted image is not much different from The original one att all

public static void main(String[] args) throws FileNotFoundException, IOException{

try{
        File bmpFile = new File("C:\\Users\\acer\\Desktop\\py\\6.bmp");
        BufferedImage image = ImageIO.read(bmpFile);
        int width          = image.getWidth();
        int height         = image.getHeight();

        int[][] pixels = new int[width][height];

        for( int i = 0; i < width; i++ )
            for( int j = 0; j < height; j++ )
                 pixels[i][j] = image.getRGB( i, j );

        int []key ={100,70,600,878};
        int []t =new int[256];
        int []s =new int[256];

        for(int i=0;i<s.length;i++)
            s[i]=i;

        for(int i=0;i<t.length;i++)
            t[i]=key[i%key.length];


        int j=0;
        for(int i=0;i<s.length;i++){

            j=(j+s[i]+t[i])%s.length;
            int temp=s[i];
            s[i]=s[j];
            s[j]=temp;

        } 
        int tt;
        int k;
        j=0;
        int i=0;
        for(int c=0;c<width;c++){
           for(int cc=0;cc<height;cc++){
            i = (i + 1) % s.length;
            j = (j + s[i]) % s.length;
            int temp=s[i];
            s[i]=s[j];
            s[j]=temp;
            tt = (s[i] + s[j]) % 256;
            k = s[tt];
            pixels [c][cc]=pixels[c][cc] ^ k;
            }
        } 
        for(  i = 0; i < width; i++ )
            for( j = 0; j < height; j++ )
                image.setRGB(i, j, pixels[i][j]);

        bmpFile = new File("C:\\Users\\acer\\Desktop\\py\\66.bmp");
        ImageIO.write(image, "bmp", bmpFile);


    }
    catch (IOException e){
         System.out.println(e.getMessage());
    }
 } 

i dont know what the problem,i looked for some codes but didn't found a lot of differences

1

1 Answers

1
votes

Ok here's my answer: in the loop where you define pixels you need to apply the XOR to each band, R, G, B:

    for(int c=0;c<width;c++){
       for(int cc=0;cc<height;cc++){
        i = (i + 1) % s.length;
        j = (j + s[i]) % s.length;
        int temp=s[i];
        s[i]=s[j];
        s[j]=temp;
        tt = (s[i] + s[j]) % 256;
        k = s[tt];
        int R=(pixels [c][cc]&0xff0000)>>16, G=(pixels [c][cc]&0x00ff00)>>8, B=(pixels [c][cc]&0x0000ff);
        R=R ^ k;
        G=G ^ k;
        B=B ^ k;
        pixels [c][cc]=(R<<16)|(G<<8)|B;
        }
    } 

This will hopefully create some "different" image. In the decompressor you have to apply equivalent corresponding processing.