zoukankan      html  css  js  c++  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();
    	}
    }
  • 相关阅读:
    Birt报表存储过程多选参数的设置
    jQuery UI AutoComplete的使用
    关于事件的简单优化
    Java编程思想(Chapter2、4、6)
    CSS层模型
    [转]Java并发编程:Lock
    Java多线程synchronized同步
    关于Thread.currentThread()和this的差异
    关于JavaScript闭包的小问题
    ReactiveCocoa(二)
  • 原文地址:https://www.cnblogs.com/kaigexuetang/p/7197031.html
Copyright © 2011-2022 走看看