代码实现:
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();
}
}
}
}
}
这篇博客展示了如何使用Java实现图片文件的加密和解密,通过字节数组操作进行简单的异或加密,提供test1和test2方法分别处理加密和解密过程。
5744

被折叠的 条评论
为什么被折叠?



