思路:
加密:将文件头中固定长度的字节与文件尾中同样长度的内容对换
解密:再对换一次
java代码:
public class RanRWFile {
public void encode(String path){
int len = 128;
byte[] bufHead = new byte[len];
byte[] bufTail = new byte[len];
File imgFile = new File(path);
RandomAccessFile img = null;
try {
img = new RandomAccessFile(imgFile, "rws");
img.seek(0);
img.read(bufHead, 0, len);
img.seek(img.length() - len);
img.read(bufTail, 0, len);
img.seek(0);
img.write(bufTail, 0, len);
img.seek(img.length() - len);
img.write(bufHead, 0, len);
} catch (FileNotFoundException e) {
System.out.println(e.getMessage());
} catch (IOException e) {
System.out.println(e.getMessage());
} finally{
if(img != null){
try {
img.close();
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
}
}
public void decode(String path){
encode(path);
}
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
String path = "d:/new.png";
RanRWFile rf = new RanRWFile();
// rf.encode(path);
rf.decode(path);
}
}