使用装饰设计模式加密数据:
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();
}
}
|