源码如下,问题出在哪里?package locked;import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;import javax.swing.ImageIcon;import org.apache.commons.io.FileUtils;public class HzLocked {
byte[] KEYVALUE = null;
final int BUFFERLEN = 512;
// 设置锁的长度
final int prefixLength = 100; public static void main(String[] args) throws Exception {
new HzLocked().encryptFile("D:/data/datapath/datapath/716734920/Pictures/000/0000000b.jpg","D:/locked.jpg");
//System.out.println(new String(new HzLocked().decryptFile("D:/locked.xml")));

FileOutputStream fileOutputStream = new FileOutputStream("D:/ok.jpg");
fileOutputStream.write(new HzLocked().decryptFile("D:/locked.jpg"));
fileOutputStream.flush();
fileOutputStream.close();

}

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

//其他字节直接输出
else {
out.write(buffer, 0, bufferlength);
}
}
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[] tmpKeyValue = new byte[prefixLength];
// 先取出来锁
in.read(tmpKeyValue, 0, prefixLength);
KEYVALUE = tmpKeyValue; int bufferlength, keyPosition, keylength, myFlag;
myFlag = 0;
keyPosition = 0;
keylength = KEYVALUE.length;
byte buffer[] = new byte[BUFFERLEN];
while ((bufferlength = in.read(buffer)) != -1) {
//只解密前BUFFERLEN个字节
if (myFlag++ == 0) {
byte[] tmpbufferTemp = new byte[buffer.length];
for (int i = 0; i < bufferlength; i++) {
buffer[i] ^= KEYVALUE[keyPosition];
tmpbufferTemp[i] = buffer[i];
keyPosition++;
if (keyPosition == keylength)
keyPosition = 0;
} tmpBuffer.add(tmpbufferTemp);
} else {
tmpBuffer.add(buffer);
}
}
in.close(); byte[] all = new byte[10240];
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;
} // 产生随机的数据锁
private String getRandomString() {
StringBuffer sbString = new StringBuffer();
for (int i = 0; i < prefixLength; i++) {
if (i == 0) {
sbString.append('h');
} else if (i == 1) {
sbString.append('z');
} else {
sbString.append(rs.charAt(new Random().nextInt(rs.length())));
}
}
return sbString.toString();
} private String rs = "123456789abcdefghijklmnopqwstuvwxyz*-+)?><=-`(&^%$#@!.,';:][}{";
}