生活随笔
收集整理的這篇文章主要介紹了
IO流练习题 实现图片的加密解密操作
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
代碼中對圖片加密用到 ^ 異或運算
原理簡單說一下:一個數兩次異或之后還是他本身
代碼實現
package BYSSSExer2
;import org
.junit
.Test
;import java
.io
.File
;
import java
.io
.FileInputStream
;
import java
.io
.FileOutputStream
;
import java
.io
.IOException
;
public class PicTest {@Testpublic void test1() throws IOException
{File srcfile
= new File("cxy.jpg");File destfile
= new File("cxysafe.jpg");FileInputStream fis
= new FileInputStream(srcfile
);FileOutputStream fos
= new FileOutputStream(destfile
);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
);}fis
.close();fos
.close();}@Testpublic void test2() throws IOException
{FileInputStream fis
= new FileInputStream("cxysafe.jpg");FileOutputStream fos
= new FileOutputStream("cxy5.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
);}fis
.close();fos
.close();}
}
加密后
解密后
總結
以上是生活随笔為你收集整理的IO流练习题 实现图片的加密解密操作的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。