使用装饰设计模式加密数据

使用装饰设计模式加密数据:

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();
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值