private boolean encryptFile(String oldFile, String newFile) throws Exception {
// 动态生成锁
KEYVALUE = getRandomString().getBytes(); FileInputStream in = new FileInputStream(oldFile); File file = new File(newFile);
if (!file.exists())
file.createNewFile();
FileOutputStream out = new FileOutputStream(file); // 把锁放到文件头
out.write(KEYVALUE); // 其他部分开始加密
int c, pos, keylen, myFlag;
myFlag = 0;
pos = 0;
keylen = KEYVALUE.length;
byte buffer[] = new byte[BUFFERLEN];
while ((c = in.read(buffer)) != -1) {
if (myFlag++ == 0) {
for (int i = 0; i < c; i++) {
buffer[i] ^= KEYVALUE[pos];
out.write(buffer[i]);
pos++;
if (pos == keylen)
pos = 0;
}
} else {
out.write(buffer, 0, c);
}
}
out.flush();
in.close();
out.close();

return true;
}
private byte[] decryptFile(String oldFile) throws Exception {
FileInputStream in = new FileInputStream(oldFile); List<byte[]> tmpBuffer = new ArrayList<byte[]>(); byte[] tmpB = new byte[prefixLength];
// 先取出来锁
in.read(tmpB, 0, prefixLength);
KEYVALUE = tmpB; int c, pos, keylen, myFlag;
myFlag = 0;
pos = 0;
keylen = KEYVALUE.length;
byte buffer[] = new byte[BUFFERLEN];
while ((c = in.read(buffer)) != -1) {
if (myFlag++ == 0) {
byte[] tmpbuffer_2 = new byte[buffer.length];
for (int i = 0; i < c; i++) {
buffer[i] ^= KEYVALUE[pos];
tmpbuffer_2[i] = buffer[i];
pos++;
if (pos == keylen)
pos = 0;
} tmpBuffer.add(tmpbuffer_2);
} else {
tmpBuffer.add(buffer);
}
}
in.close(); byte[] all = new byte[1024];
int maxsize = 0;
for (byte[] b : tmpBuffer) {
maxsize += b.length;
if (all.length > maxsize) {
System.arraycopy(b, 0, all, maxsize - b.length, b.length);
} else {
byte[] tempAll = new byte[all.length + 512];
System.arraycopy(all, 0, tempAll, 0, all.length);
all = tempAll;
System.arraycopy(b, 0, all, maxsize - b.length, b.length);
}
} return all;
}