使用裝飾設計模式加密數據
阿新 • • 發佈:2017-07-17
java
使用裝飾設計模式加密數據:
package com.zs.JiaJiE09;import java.io.FileOutputStream;import java.io.IOException;import java.io.OutputStream;/** * 使用裝飾設計模式把數據取反進行加密解密 * Key方法為鑰匙 * * @author LZG * */public class JMOutputStream extends OutputStream{/** * 更多資料歡迎瀏覽凱哥學堂官網:http://kaige123.com * @author 小沫 */ private int key; private OutputStream output; public JMOutputStream(OutputStream output){ this.output=output; } public void Key(int key){ this.key=key; } public void write(int b) throws IOException { output.write(~b+key); } public void write(byte[] b, int off, int len) throws IOException { for (int i = off; i < len; i++) { output.write(~b[i]+key); } } public void write(byte[] b) throws IOException { for (int i = 0; i < b.length; i++) { output.write(~b[i]+key); } } }
測試類:
package com.zs.JiaJiE09;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;public class Test {/** * 更多資料歡迎瀏覽凱哥學堂官網:http://kaige123.com * @author 小沫 */ public static void main(String[] args) throws IOException{ FileInputStream fin= new FileInputStream("e:/test/aa.txt"); JMOutputStream fout = new JMOutputStream(new FileOutputStream("d:/test/aa111.txt")); fout.Key(1402); byte[] b = new byte[1024]; while(fin.available()!=0){ int len = fin.read(b); fout.write(b, 0, len); } fin.close(); fout.close(); } }
使用裝飾設計模式加密數據