使用装饰设计模式加密数据:
class="hljs" style="display: inline; overflow: visible; padding: 0px; background: 0px 0px transparent; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; margin: 0px; font-size: 12px; border-radius: 3px; border: 0px; line-height: inherit;">package com.zs.JiaJiE09;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
public class JMOutputStream extends OutputStream{
private int key;
private OutputStream output;
function" style="">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 {
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();
}
}