这是我参与更文挑战的第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或其他数字异或操作,不经解密程序的操作,得到的图片将不能正常打开。

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,根据异或操作的特性可以得到正常的图片内容。

© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END
    






















![[桜井宁宁]COS和泉纱雾超可爱写真福利集-一一网](https://www.proyy.com/skycj/data/images/2020-12-13/4d3cf227a85d7e79f5d6b4efb6bde3e8.jpg)

![[桜井宁宁] 爆乳奶牛少女cos写真-一一网](https://www.proyy.com/skycj/data/images/2020-12-13/d40483e126fcf567894e89c65eaca655.jpg)
