我正在尝试加密/解密文件的最后 64 KB,但加密文件中缺少一个文件夹。在这里,我试图在一个 zip 文件上实现。目前,zip 文件中有 3 个文件夹,在生成的 zip 中,它只显示两个文件夹。缺少其中一个文件夹。我认为它缺少一些字节。这是我的代码:
static void encryptLast64KB(String inputPath, String outputPath)
throws IOException, NoSuchAlgorithmException,
NoSuchPaddingException, InvalidKeyException {
File myFile = new File(inputPath);
FileInputStream fis = new FileInputStream(myFile);
FileOutputStream fos = new FileOutputStream(outputPath);
BufferedOutputStream bus = new BufferedOutputStream(fos);
SecretKeySpec sks = new SecretKeySpec("MyDifficultPassw".getBytes(),
"AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, sks);
int b = 0;
byte[] d = new byte[65536];
int offset = 0;
byte[] encVal = null;
while ((b = fis.read(d)) != -1) {
offset = offset + b;
Log.d(TAG, "Offset: "+offset);
Log.d(TAG, "b: "+b);
if((offset)>=myFile.length())
{
Log.d(TAG, "last 64 Kbytes");
try {
encVal = cipher.doFinal(d);
Log.d(TAG, "encVal: "+encVal);
bus.write(encVal);
} catch (Exception e) {
e.printStackTrace();
}
}
else
{
Log.d(TAG, "rest of the bytes");
bus.write(d);
}
bus.flush();
bus.close();
fis.close();
}
}
请检查..
[已编辑] 添加了用于解密最后 64 KB 的解密代码。
static byte[] decryptLast64KBytes(String inputPath) throws IOException,
NoSuchAlgorithmException, NoSuchPaddingException,
InvalidKeyException {
FileInputStream fis = new FileInputStream(inputPath);
SecretKeySpec sks = new SecretKeySpec("MyDifficultPassw".getBytes(), "AES");
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
byte[] iv = new byte[] { '3', 'd', '0', 'c', 'd', '7', 'A', '9', '7', 'e', '2', '0', 'b', 'x', 'g', 'y' };
IvParameterSpec ivParameterSpec = new IvParameterSpec(iv);
try {
cipher.init(Cipher.DECRYPT_MODE, sks, ivParameterSpec);
} catch (InvalidAlgorithmParameterException e) {
e.printStackTrace();
}
CipherInputStream cis = new CipherInputStream(fis, cipher);
int b;
byte[] d = new byte[1024];
ByteArrayOutputStream bos = new ByteArrayOutputStream();
int count =0;
int offset = 0;
while((b = fis.read(d)) != -1) {
offset = offset + b;
Log.d(TAG, "Offset: "+offset);
Log.d(TAG, "b: "+b);
if((offset)>=fis.available())
{
Log.d(TAG, "last 64 Kbytes");
while((b = cis.read(d, offset, offset+b))!=-1)
{
bos.write(d);
offset = offset + b;
}
}
else
{
Log.d(TAG, "rest of the bytes");
bos.write(d);
}
}
byte[] completeBytes = bos.toByteArray();
cis.close();
return completeBytes;
}