例26:图片的加密解密操作
//图片的加密----
@Test
public void test(){
FileInputStream fis= null;
FileOutputStream fos= null;
try {
fis = new FileInputStream("lay3.jpg");
fos = new FileOutputStream("lay5.jpg");
byte[] buffer=new byte[20];
int len;
while ((len=fis.read(buffer))!=-1){
//字节数组修改----》实现加密操作
for (int i = 0; i <len ; i++) {
buffer[i]=(byte)(buffer[i]^5);
}
fos.write(buffer,0,len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if(fos!=null){
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(fis!=null){
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
//图片的解密
@Test
public void test1(){
FileInputStream fis= null;
FileOutputStream fos= null;
try {
fis = new FileInputStream("lay5.jpg");
fos = new FileOutputStream("lay3.jpg");
byte[] buffer=new byte[20];
int len;
while ((len=fis.read(buffer))!=-1){
//字节数组修改----》之前^5,再^5变回去
for (int i = 0; i <len ; i++) {
buffer[i]=(byte)(buffer[i]^5);
}
fos.write(buffer,0,len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if(fos!=null){
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(fis!=null){
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}