java 对图片文件进行加密

图片使用字节流,并使用对应的处理流对象

BufferedInputStream和
BufferedOutputStream

对于文本文件,虽然也可以放入,但是效率可能会低,最好还是使用字符缓冲流

/**
*@ClassName: BuffeTest1
*@Description 实现图片加密和解析
*@Author 我心
*@Date 2021/7/18
*@Time 12:45
*/

 class BuffeTest1 {
    public static void decryptFile(File decryptedFile,File newFile) throws IOException {
        FileInputStream fileInputStream=new FileInputStream(decryptedFile);
        FileOutputStream fileOutputStream=new FileOutputStream(newFile);
        BufferedInputStream inputStream=new BufferedInputStream(fileInputStream);
        BufferedOutputStream outputStream=new BufferedOutputStream(fileOutputStream);
        int b;
        while ((b=inputStream.read())!=-1){
            //与5进行异或
            outputStream.write(b^5);
        }
        outputStream.flush();//刷新流
        //关闭流
        inputStream.close();
        outputStream.close();
    }
    public static void encryptedFile(File oldFile,File newFile) throws IOException {
        FileInputStream fileInputStream=new FileInputStream(oldFile);
        FileOutputStream fileOutputStream=new FileOutputStream(newFile);
        BufferedInputStream inputStream=new BufferedInputStream(fileInputStream);
        BufferedOutputStream outputStream=new BufferedOutputStream(fileOutputStream);
        int b;
        while ((b=inputStream.read())!=-1){
            //与5进行异或
            outputStream.write(b^5);
        }
        outputStream.flush();//刷新流
        //关闭流
        inputStream.close();
        outputStream.close();
    }
    public static void main(String[] args) throws IOException {
        decryptFile(new File("src/IO_use/iomystry.jpg"),new File("src/IO_use/iomystry1.jpg"));

    }
}

你可能感兴趣的:(笔记,java,intellij-idea)