使用装饰设计模式加密数据:
packagecom.zs.JiaJiE09;importjava.io.FileOutputStream;importjava.io.IOException;importjava.io.OutputStream;/** * 使用装饰设计模式把数据取反进行加密解密 * Key方法为钥匙 * *@authorLZG * */publicclassJMOutputStreamextendsOutputStream{/** * 更多资料欢迎浏览凯哥学堂官网:http://kaige123.com *@author小沫 */privateintkey;privateOutputStream output;publicJMOutputStream(OutputStream output){this.output=output;}publicvoidKey(intkey){this.key=key;}publicvoidwrite(intb)throwsIOException{output.write(~b+key);}publicvoidwrite(byte[] b,intoff,intlen)throwsIOException{for(inti = off; i < len; i++) {output.write(~b[i]+key);}}publicvoidwrite(byte[] b)throwsIOException{for(inti =0; i < b.length; i++) {output.write(~b[i]+key);}}}
packagecom.zs.JiaJiE09;importjava.io.FileInputStream;importjava.io.FileOutputStream;importjava.io.IOException;publicclassTest{/** * 更多资料欢迎浏览凯哥学堂官网:http://kaige123.com *@author小沫 */publicstaticvoidmain(String[] args)throwsIOException{FileInputStream fin=newFileInputStream("e:/test/aa.txt");JMOutputStream fout =newJMOutputStream(newFileOutputStream("d:/test/aa111.txt"));fout.Key(1402);byte[] b =newbyte[1024];while(fin.available()!=0){intlen = fin.read(b);fout.write(b,0, len);}fin.close();fout.close();}}