Java IO--实现文件的加密解密

我们知道文件存储的方式在计算机当中是以字节的方式进行存储的,可以通过对文件字节的操作来实现文件的加密。

下面的例子是通过读取文件的字节,然后使字节中的每一位取反(1变0,0变1),再进行倒置,来实现加解密过程。

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Arrays;
/**
 * @author 朱俊伟
 * @date 2020/11/15
 */
public class FileEncrytionTest
{
    public static void main(String[] args)
    {
        //源文件
        File file1 = new File("D:\系统文件夹\桌面\test.txt");
        //加密文件
        File file2 = new File("D:\系统文件夹\桌面\myenc.txt");
        //解密文件
        File file3 = new File("D:\系统文件夹\桌面\mydec.txt");
        //加密方法
        EnFile(file1,file2);
        //解密方法
        DecFile(file2,file3);
    }

    //加密方法
    public static void EnFile(File srcFile,File tarFile)
    {
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null ;
        //源文件
        File file1 = srcFile;
        //加密文件
        File file2 = tarFile;
        try
        {
            InputStream is = new FileInputStream(file1);
            OutputStream os = new FileOutputStream(file2);
            bis = new BufferedInputStream(is);
            bos = new BufferedOutputStream(os);

            byte[] data = new byte[100];
            int len = 0 ;
            while((len = bis.read(data))!= -1)
            {
                byte[] temp = Arrays.copyOfRange(data,0,len);
                System.out.println("加密读取:"+Arrays.toString(temp));
                bos.write(reverseArray(temp));
                System.out.println("加密写入:"+Arrays.toString(temp));
            }

        } catch ( Exception e)
        {
            e.printStackTrace();
        } finally
        {
            try
            {
                if(bis != null)
                {
                    /* 关闭管子 */
                    bis.close();
                    bis = null ;
                }
            }catch(IOException e)
            {
                e.printStackTrace();
            }

            try
            {
                if(bos != null)
                {
                    /* 关闭管子 */
                    bos.close();
                    bos = null ;
                }
            }catch(IOException e)
            {
                e.printStackTrace();
            }
        }
    }
    //解密方法
    public static void DecFile(File srcFile,File tarFile)
    {
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null ;
        //源加密文件
        File file1 = srcFile;
        //解密文件
        File file2 = tarFile;
        try
        {
            InputStream is = new FileInputStream(file1);
            OutputStream os = new FileOutputStream(file2);
            bis = new BufferedInputStream(is);
            bos = new BufferedOutputStream(os);

            byte[] data = new byte[100];
            int len = 0 ;
            while((len = bis.read(data))!= -1)
            {
                byte[] temp = Arrays.copyOfRange(data,0,len);
                System.out.println("解密读取:"+Arrays.toString(temp));
                bos.write(reverseArray(temp));
                System.out.println("解密写入:"+Arrays.toString(temp));
            }

        } catch ( Exception e)
        {
            e.printStackTrace();
        } finally
        {
            try
            {
                if(bis != null)
                {
                    /* 关闭管子 */
                    bis.close();
                    bis = null ;
                }
            }catch(IOException e)
            {
                e.printStackTrace();
            }

            try
            {
                if(bos != null)
                {
                    /* 关闭管子 */
                    bos.close();
                    bos = null ;
                }
            }catch(IOException e)
            {
                e.printStackTrace();
            }
        }
    }

    /**
     * 字节数组的各位取反,并倒置
     * @param bytes
     * @return
     */
    public static byte[] reverseArray(byte[] bytes){
        for (int i=0; i<bytes.length/2; i++){
            byte b = (byte) ~bytes[i];
            bytes[i] = (byte) ~bytes[bytes.length-i-1];
            bytes[bytes.length-i-1] = b;
        }
        return bytes;
    }
}

控制台打印

三个文件对比。

--------------- 我每一次回头,都感觉自己不够努力,所以我不再回头。 ---------------
原文地址:https://www.cnblogs.com/zjw-blog/p/13977309.html