This is the 7th day of my participation in Gwen Challenge

Image encryption in IO stream transmission

1. Encryption process

Package Image encryption _2020;import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class PicTest {
    public static void main(String[] args) {
        FileInputStream fis=null;
        FileOutputStream fos=null;
        try {
            fis = new FileInputStream(new File("Long live the monster. PNG"));
            fos = new FileOutputStream("Long live the monster (encrypted).png");

            byte[] buffer = new byte[20];
            int len;
            while((len = fis.read(buffer)) ! = -1) {
                for (int i = 0; i < len; i++) {
                    buffer[i] = (byte) (buffer[i] ^ 5);
                }
                fos.write(buffer, 0, len); }}catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(fis ! =null) {
                try {
                    fis.close();
                } catch(IOException e) { e.printStackTrace(); }}if(fos ! =null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }
    }
}

Copy the code
            fis = new FileInputStream(new File("Long live the monster. PNG"));
            fos = new FileOutputStream("Long live the monster (encrypted).png");
Copy the code

Fis receives the image file fos that needs to be encrypted and stores the encrypted file

 while((len = fis.read(buffer)) ! = -1) {
                for (int i = 0; i < len; i++) {
                    buffer[i] = (byte) (buffer[i] ^ 5);
                }
                fos.write(buffer, 0, len);
            }
Copy the code

buffer[i] = (byte) (buffer[i] ^ 5); Key encryption step, each bit in the buffer with 5 or other numbers xor operation, without the operation of the decryption program, the resulting picture will not be opened properly.

2. Decrypt operations

Package Image encryption _2020;import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class jiemi {
    public static void main(String[] args) {
        FileInputStream fis=null;
        FileOutputStream fos=null;
        try {
            fis = new FileInputStream(new File("Long live the monster (encrypted).png"));
            fos = new FileOutputStream("Long live the Monster. PNG");

            byte[] buffer = new byte[20];
            int len;
            while((len = fis.read(buffer)) ! = -1) {
                for (int i = 0; i < len; i++) {
                    buffer[i] = (byte) (buffer[i] ^ 5);
                }
                fos.write(buffer, 0, len); }}catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(fis ! =null) {
                try {
                    fis.close();
                } catch(IOException e) { e.printStackTrace(); }}if(fos ! =null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }

    }
}
Copy the code
            fis = new FileInputStream(new File("Long live the monster (encrypted).png"));
            fos = new FileOutputStream("Long live the Monster. PNG");
Copy the code

Fis receives the encrypted file FOS stores the decrypted picture file

  for (int i = 0; i < len; i++) {
                    buffer[i] = (byte) (buffer[i] ^ 5);
                }
Copy the code

Where, the decryption operation again xOR 5, according to the characteristics of xor operation can get the normal picture content.