public class PicTest {
//图片的加密
@Test
public void test1(){
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream("爱情与友情.jpg");
fos = new FileOutputStream("爱情与友情secret.jpg");
byte[] buffer = new byte[20];
int len;
while((len = fis.read(buffer)) != -1){
//字节数组进行修改
//错误的:修改的是形参b的内容,buffer数组的内容没变
// for(byte b : buffer){
// b = (byte)(b ^ 5);
// }
//正确的
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 (IOException e) {
e.printStackTrace();
}
}
if(fis != null){
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
//图片的解密
@Test
public void test2(){
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream("爱情与友情secret.jpg");
fos = new FileOutputStream("爱情与友情4.jpg");
byte[] buffer = new byte[20];
int len;
while((len = fis.read(buffer)) != -1){
//字节数组进行修改
//错误的:修改的是形参b的内容,buffer数组的内容没变
// for(byte b : buffer){
// b = (byte)(b ^ 5);
// }
//正确的
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 (IOException e) {
e.printStackTrace();
}
}
if(fis != null){
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/110957.html