• 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;
        }
    }
    

    控制台打印

    三个文件对比。

    --------------- 我每一次回头,都感觉自己不够努力,所以我不再回头。 ---------------
  • 相关阅读:
    Js特效之放大镜(淘宝商品展示)
    关于使用JavaScript实现图片点击切换(附带改变导航图片 方案二)
    关于使用JavaScript实现图片点击切换(附带改变导航图片 方案一)
    JavaScript常用的方法和函数(setInterval和setTimeout)
    When to use dequeueReusableCellWithIdentifier vs dequeueReusableCellWithIdentifier: forIndexPath
    iOS 7.1 安装 企业应用 提示 无法下载应用程序
    Git忽略规则及.gitignore规则不生效的解决办法
    Core Animation 学习
    AutoLayout那些坑
    欲善其事必先利其器---Xcode插件
  • 原文地址:https://www.cnblogs.com/zjw-blog/p/13977309.html
Copyright © 2020-2023  润新知