代码实现:
public class PictureTest {
//图片的加密
@Test
public void test1(){
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream("1.jpg");
fos = new FileOutputStream("11.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 (Exception e) {
e.printStackTrace();
}
}
if(fis != null){
try {
fis.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
//图片的解密
@Test
public void test2(){
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream("11.jpg");
fos = new FileOutputStream("111.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 (Exception e) {
e.printStackTrace();
}
}
if(fis != null){
try {
fis.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}