问题:将图片进行加密操作
解题思路:将图片进行加密就是按照某种特定操作进行复制的过程,首先要对非文本文件进行读取,用到字节流FileInputStream,由于想提高读取效率,故使用处理流BufferedInputStream将其封装起来,写入时则采用配套的BufferedOutputStream。
package FileStudy;
import java.io.*;
//实现图片的加密和解密操作
public class PicTest {
public static void main(String[] args) {
String sname="C:/Users/0.0/Pictures/6666.jpg";
String ename="C:/Users/0.0/Pictures/6777.jpg";
String hname="C:/Users/0.0/Pictures/6888.jpg";
PicTest.jiami( sname, ename);
PicTest.jiami( ename, hname);
}
//一、加密
public static void jiami(String sname,String ename) {
BufferedInputStream bis=null;
BufferedOutputStream bos=null;
try {
bis = new BufferedInputStream(new FileInputStream(sname));
bos = new BufferedOutputStream(new FileOutputStream(ename));
byte[] bs=new byte[10];
int len;
while((len=bis.read(bs))!=-1) {
for(int i=0;i<len;i++) {
bs[i]=(byte)(bs[i]^5);
}
bos.write(bs,0,len);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try {
bis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
bos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
//二、解密
public static void jiemi(String ename,String hname) {
BufferedInputStream bis=null;
BufferedOutputStream bos=null;
try {
bis = new BufferedInputStream(new FileInputStream(ename));
bos = new BufferedOutputStream(new FileOutputStream(hname));
byte[] bs=new byte[10];
int len;
while((len=bis.read(bs))!=-1) {
for(int i=0;i<len;i++) {
bs[i]=(byte)(bs[i]^5^5);
}
bos.write(bs,0,len);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try {
bis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
bos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}