将写入的字节用异或打乱顺序,实现图片加密
public class StreamTest03 {
//图片加密
@Test
public void bufferedStreamTest(){
BufferedInputStream bufferedInputStream = null;
BufferedOutputStream bufferedOutputStream = null;
try {
bufferedInputStream = new BufferedInputStream(new FileInputStream("C:\\Users\\pc\\IdeaProjects\\untitled2\\f6330d141cf7324e310ea5e481b56e11--2979603408.jpg"));
bufferedOutputStream = new BufferedOutputStream(new FileOutputStream("C:\\Users\\pc\\IdeaProjects\\untitled2\\f6330d141cf7324e310ea5e481b56e11--2979603408serice.jpg"));
byte[] bytes = new byte[1024];
int len;
while ((len = bufferedInputStream.read(bytes)) != -1){
//加密
for (int i = 0; i < len; i++) {
bytes[i] = (byte)(bytes[i] ^ 5);
}
bufferedOutputStream.write(bytes,0,len);
}
} catch (IOException ioException) {
ioException.printStackTrace();
} finally {
if (bufferedInputStream != null){
try {
bufferedInputStream.close();
} catch (IOException ioException) {
ioException.printStackTrace();
}
}
try {
bufferedOutputStream.close();
} catch (IOException ioException) {
ioException.printStackTrace();
}
}
}
//图片解密 m^n^n = m
@Test
public void bufferedStreamTest2(){
BufferedInputStream bufferedInputStream = null;
BufferedOutputStream bufferedOutputStream = null;
try {
bufferedInputStream = new BufferedInputStream(new FileInputStream("C:\\Users\\pc\\IdeaProjects\\untitled2\\f6330d141cf7324e310ea5e481b56e11--2979603408serice.jpg"));
bufferedOutputStream = new BufferedOutputStream(new FileOutputStream("C:\\Users\\pc\\IdeaProjects\\untitled2\\f6330d141cf7324e310ea5e481b56e11--2979603408serice02.jpg"));
byte[] bytes = new byte[1024];
int len;
while ((len = bufferedInputStream.read(bytes)) != -1){
//解密
for (int i = 0; i < len; i++) {
bytes[i] = (byte)(bytes[i] ^ 5);
}
bufferedOutputStream.write(bytes,0,len);
}
} catch (IOException ioException) {
ioException.printStackTrace();
} finally {
if (bufferedInputStream != null){
try {
bufferedInputStream.close();
} catch (IOException ioException) {
ioException.printStackTrace();
}
}
try {
bufferedOutputStream.close();
} catch (IOException ioException) {
ioException.printStackTrace();
}
}
}
}