首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >文件加密解密(字节流)

文件加密解密(字节流)

作者头像
Cell
发布2022-02-25 16:52:45
发布2022-02-25 16:52:45
1.3K0
举报
文章被收录于专栏:Cell的前端专栏Cell的前端专栏

基本功能: 给定一个密钥,读取文件内容,加密后,输出到另外一个文件。 这里使用文件输入流读取文件内容,然后每个字节和密码进行异或简单加密。加密完成,使用文件输出流写入另一个文件中。解密和加密方法一样。利用的是对同一个数异或两遍其值不变的性质。因此一个程序可以完成加密和解密功能。只需修改文件名即可。

文件加密解密

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45

package cn.lruihao.base; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; public class EncryptFile { public static void main(String[] args) throws IOException { byte pwd=123;//加密/解密密码 FileInputStream f=new FileInputStream("src/cn/lruihao/base/EncryptFile.java");//待加密文件 FileOutputStream fout=new FileOutputStream("encrypted.txt");//已加密文件 System.out.println("开始加密。"); int n=f.available()/5; byte[] b=new byte[n];//以一个字节数组的长度读取和复制 int count=0; while((count=f.read(b,0,n))!=-1) { //写入之前先加密/解密 for(int i=0;i<count;i++) { b[i]=(byte)(b[i]^pwd);// } fout.write(b,0,count); } System.out.println("完成加密"); f.close(); fout.close(); // f=new FileInputStream("encrypted.txt"); // fout=new FileOutputStream("unencrypted.txt"); // System.out.println("开始解密。"); // n=f.available()/5; // b=new byte[n];//以一个字节数组的长度读取和复制 // count=0; // while((count=f.read(b,0,n))!=-1) { // //写入之前先加密/解密 // for(int i=0;i<count;i++) { // b[i]=(byte)(b[i]^pwd); // } // fout.write(b,0,count); // } // System.out.println("完成解密"); // f.close(); // fout.close(); } }

文件复制

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37

package cn.lruihao.base; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; public class FileStreamCopy { public static void main(String[] args) throws IOException { int size; FileInputStream f=new FileInputStream("src/cn/lruihao/base/FileStreamCopy.java"); FileOutputStream fout=new FileOutputStream("copy-of-file.txt"); System.out.println("总长度:"+(size=f.available())); int n=size/10; System.out.print("使用单字节方法读取后:"); for(int i=0;i<n;i++) { fout.write(f.read()); } System.out.println("剩余长度:"+f.available()); System.out.println("读取一个字节数组后:"); byte b[]=new byte[n]; f.read(b); fout.write(b); System.out.println("剩余长度:"+f.available()); System.out.println("读取余下数据:"); int count=0; while((count=f.read(b,0,n))!=-1) { //System.out.println(count); fout.write(b,0,count); } System.out.println("剩余长度:"+f.available()); f.close(); fout.flush(); fout.close(); } }

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32

package cn.lruihao.base; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; public class BufferedStreamCopy { public static void main(String[] args) throws IOException { FileInputStream f=new FileInputStream("src/cn/lruihao/base/BufferedStreamCopy.java"); FileOutputStream fout=new FileOutputStream("copy-of-file.txt"); BufferedInputStream bis=new BufferedInputStream(f); BufferedOutputStream bos=new BufferedOutputStream(fout); System.out.println("开始复制。"); int n=f.available()/5; byte[] b=new byte[n]; int count=0; while((count=bis.read(b,0,n))!=-1) { bos.write(b,0,count); } System.out.println("复制完成"); bis.close(); bos.flush(); bos.close(); f.close(); fout.flush(); fout.close(); } }

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019-05-02,如有侵权请联系 cloudcommunity@tencent.com 删除
目录
  • 文件加密解密
  • 文件复制
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档