Java基础05(补充二)-异或的应用

1.异或的性质:

    一个数,异或其他的数两次后,还是其本身!

 

2.应用:简单的文件加密程序

   代码:

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.FileNotFoundException;

class Encrypt {
		private static final int PASSWORD = 0x12345678;
		private static final String SUFFIX = ".enc";
		
		public void doEncrypt(String path) {
			FileInputStream fis = null;
			FileOutputStream fos = null;
			
			try {					
				File file = new File(path);
				File newFile = new File(path+SUFFIX);
				fis = new FileInputStream(file); //FileNotFoundException
				fos = new FileOutputStream(newFile); //FileNotFoundException
				byte [] buf = new byte[1024];
				int len = 0;
				while((len=fis.read(buf))!=-1) { //IOException
					for(int i = 0;i<len;i++)
					{
						buf[i]=(byte)(buf[i]^PASSWORD); 
					}
					fos.write(buf,0,len); //IOException
				}
			}
			catch(FileNotFoundException e) {
				throw new RuntimeException("文件不存在,请重试!");
			}	
			catch(IOException e) {
				throw new RuntimeException("文件读取异常!");
			}
			finally {
				try {
					if(fis!=null)
						fis.close();
				}catch(Exception e ) {
					throw new RuntimeException("文件读取流异常!");
				}	
				
				try {
					if(fos!=null)
						fos.close();
				}catch(Exception e) {
					throw new RuntimeException("文件存储流异常!");
				}	
			}
		}
		
		public void doDecrypt(String path) {
			int index = path.lastIndexOf(SUFFIX);
			if(index!=(path.length()-SUFFIX.length())) {
				System.out.println("文件类型不正确!请重试!");
				return;	
			}
				
			FileInputStream fis = null;
			FileOutputStream fos = null;
			
			try {					
				File file = new File(path);
				File newFile = new File(path.substring(0,index));
				fis = new FileInputStream(file); //FileNotFoundException
				fos = new FileOutputStream(newFile); //FileNotFoundException
				byte [] buf = new byte[1024];
				int len = 0;
				while((len=fis.read(buf))!=-1) { //IOException
					for(int i = 0;i<len;i++)
					{
						buf[i]=(byte)(buf[i]^PASSWORD); 
					}
					fos.write(buf,0,len); //IOException
				}
			}
			catch(FileNotFoundException e) {
				throw new RuntimeException("文件不存在,请重试!");
			}	
			catch(IOException e) {
				throw new RuntimeException("文件读取异常!");
			}
			finally {
				try {
					if(fis!=null)
						fis.close();
				}catch(Exception e ) {
					throw new RuntimeException("文件读取流异常!");
				}	
				
				try {
					if(fos!=null)
						fos.close();
				}catch(Exception e) {
					throw new RuntimeException("文件存储流异常!");
				}	
			}
		}
		
		public static void main(String [] args) {
			Encrypt e = new Encrypt();
			if(args.length != 2) {
				System.out.println("参数输入错误!请重新输入!");
				return;
			}
			
			if(args[0].equalsIgnoreCase("encrypt"))
				e.doEncrypt(args[1]);
			else if(args[0].toUpperCase().equals("DECRYPT")) {
				e.doDecrypt(args[1]);
			}
				
		}
	}


测试方法:

javac Encrypt.java

java Encrypt d:\1.txt encrypt

 

(待后续知识改进)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值