首页 > 编程语言 > 详细

java 文件加密

时间:2018-06-28 18:45:35      阅读:176      评论:0      收藏:0      [点我收藏+]
System.out.println("文件:");
		String s = new Scanner(System.in).nextLine();
		File file = new File(s);
		if(! file.isFile()) {
			System.out.println("不是文件");
			return;
		}		
		System.out.print("KEY:");
		int key = new Scanner(System.in).nextInt(); 
		try {
			encrypt(file, key);
			System.out.println("完成");
		} catch (Exception e) {
			System.out.println("失败");
		}
		

	}

	private static void encrypt(
			File file, int key) throws Exception{
		RandomAccessFile raf = 
		 new RandomAccessFile(file, "rw");
		
		//单字节读取标准格式
		//int b;
		//while((b = raf.read()) != -1) {
		//	b ^= key; //b = b^key;
		//	raf.seek(raf.getFilePointer()-1);        
		//	raf.write(b);
		//}
		
		// 8k 8192
		byte[] buff = new byte[8192];
		int n;//保存一批的数量
		while((n = raf.read(buff)) != -1) {
			//数组中前n个字节加密
			for(int i=0;i<n;i++) {
				buff[i] ^= key;
			}
			//下标移回n个位置
			raf.seek(raf.getFilePointer()-n);
			//输出数组中前 n 个字节
			raf.write(buff,0,n);
		}
		
		
		raf.close();
	}

  

java 文件加密

原文:https://www.cnblogs.com/xiaokaivip/p/9240174.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!