Jtti广告广告
返回顶部
首页 > 资讯 > 后端开发 > PHP编程 >Java IO--实现文件的加密解密
  • 23
分享到

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

2021-03-16 14:03:44 23人浏览 佚名
摘要

我们知道文件存储的方式在计算机当中是以字节的方式进行存储的,可以通过对文件字节的操作来实现文件的加密。下面的例子是通过读取文件的字节,然后使字节中的每一位取反(1变0,0变1),再进行倒置,来实现加解密过程。import java

我们知道文件存储的方式在计算机当中是以字节的方式进行存储的,可以通过对文件字节的操作来实现文件的加密。下面的例子是通过读取文件的字节,然后使字节中的每一位取反(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;

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();
            }
        }
    }

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

三个文件对比。

到此这篇关于Java中Java IO--实现文件的加密解密的文章就介绍到这了,更多相关Java IO--实现文件的加密解密内容请搜索编程界以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程界!

--结束END--

本文标题: Java IO--实现文件的加密解密

本文链接: https://www.lsjlt.com/news/103.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

猜你喜欢
热门问答
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作