实现图片的加密和解密简易版
import org.junit.Test;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class PicTest {
@Test
public void test() {
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream(new File("sun.jpg"));
fos = new FileOutputStream(new File("sun1.jpg"));
byte[] bytes = new byte[1024];
int len;
while ((len = fis.read(bytes)) != -1) {
for (int i = 0; i < len; i++) {
bytes[i] = (byte) (bytes[i] ^ 5);
}
fos.write(bytes, 0, len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Test
public void test1() {
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream(new File("sun1.jpg"));
fos = new FileOutputStream(new File("sun2.jpg"));
byte[] bytes = new byte[1024];
int len;
while ((len = fis.read(bytes)) != -1) {
for (int i = 0; i < len; i++) {
bytes[i] = (byte) (bytes[i] ^ 5);
}
fos.write(bytes, 0, len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}