Java基础05(补充二)-异或的应用
2012-03-28 21:20
302 查看
1.异或的性质:
一个数,异或其他的数两次后,还是其本身!
2.应用:简单的文件加密程序
代码:
测试方法:
javac Encrypt.java
java Encrypt d:\1.txt encrypt
(待后续知识改进)
一个数,异或其他的数两次后,还是其本身!
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
(待后续知识改进)
相关文章推荐
- [零基础学JAVA]Java SE应用部分-31.Java IO操作(05)IO操作实例讲解之实现简单MIS 推荐
- 黑马程序员_java基础知识补充
- java基础应用01(应用题:走梅花桩)
- JAVA基础_Date,Calender应用
- Java语言基础之接口模板的应用
- java反射基础知识(四)反射应用实践
- Java基础应用之循环控制(使用嵌套循环在控制台上输出九九乘法表)
- java基础之:匿名内部类应用例子一
- Java基础(极客)——20、Java集合类详解(补充)
- Java基础20天--05--IO流
- 黑马程序员_Java基础_面向对象(一)_05
- 黑马程序员---java基础---05面向对象(下)
- Java.util包简介并教您如何开发应用之二-Java基础-Java-编程开发
- 为什么Java这个语言没有在基础应用领域发挥优势?
- Java基础---基础加强---增强for循环、自动拆装箱及享元、枚举的作用、实现带有构造方法、透彻分析反射的基础_Class类、成员变量的反射、数组参数的成员方法进行反射、数组的反射应用
- Java学习-异或应用
- java基础知识应用--双色球开奖号码
- Java await/signal基础应用
- 基础应用:java web三层架构程序实例
- 【JAVA零基础入门系列】Day12 Java类的简单应用