IO流传输中的图片加密

这是我参与更文挑战的第7天,活动详情查看: 更文挑战

IO流传输中的图片加密

1.加密过程

package 图片加密_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("怪物万岁.png"));
            fos = new FileOutputStream("怪物万岁(加密).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();
                }
            }

        }
    }
}

复制代码
            fis = new FileInputStream(new File("怪物万岁.png"));
            fos = new FileOutputStream("怪物万岁(加密).png");
复制代码

fis接收需要加密的图片文件
fos存放加密后文件

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

buffer[i] = (byte) (buffer[i] ^ 5);
关键加密步骤,将buffer中的每一位与5或其他数字异或操作,不经解密程序的操作,得到的图片将不能正常打开。

image.png

2.解密操作

package 图片加密_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("怪物万岁(加密).png"));
            fos = new FileOutputStream("怪物万岁(解密后).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();
                }
            }

        }

    }
}
复制代码
            fis = new FileInputStream(new File("怪物万岁(加密).png"));
            fos = new FileOutputStream("怪物万岁(解密后).png");
复制代码

fis接收加密后的文件
fos则存放解密后的图片文件

  for (int i = 0; i < len; i++) {
                    buffer[i] = (byte) (buffer[i] ^ 5);
                }
复制代码

其中,解密操作再次异或5,根据异或操作的特性可以得到正常的图片内容。

image.png

© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享