简体   繁体   English

在Java中使用RC4加密图像

[英]encrypting image with RC4 in java

I'm trying to encrypt a bmp image with RC4 but the encrypted image is not much different from The original one att all 我正在尝试使用RC4加密bmp图像,但加密后的图像与原始图像没有太大区别

public static void main(String[] args) throws FileNotFoundException, IOException{ 公共静态void main(String [] args)抛出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 我不知道是什么问题,我在寻找一些代码,但没有发现很多差异

Ok here's my answer: in the loop where you define pixels you need to apply the XOR to each band, R, G, B: 好的,这就是我的答案:在定义像素的循环中,需要将XOR应用于每个波段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. 在解压缩器中,您必须应用等效的相应处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM