我正在处理一些大文件,何时进行完整的文件加密/解密。这需要太多时间。现在我只想加密文件的前 1024 个字节,其余字节将保持不变。
这是我的代码:
static void encrypt(String inputPath, String outputPath) throws IOException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException
{
// Here you read the cleartext.
FileInputStream fis = new FileInputStream(inputPath);
// This stream write the encrypted text. This stream will be wrapped by another stream.
FileOutputStream fos = new FileOutputStream(outputPath);
// Length is 16 byte
SecretKeySpec sks = new SecretKeySpec("MyDifficultPassw".getBytes(), "AES");
// Create cipher
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, sks);
// Wrap the output stream
CipherOutputStream cos = new CipherOutputStream(fos, cipher);
// Write bytes
int b;
int count = 0;
byte[] d = new byte[1024];
while((b = fis.read(d)) != -1) {
if(count <= 1024){
count += b;
cos.write(d, 0, b);
}else{
cos.write(d, 0, b);
}
// cos.write(d, 0, b);
}
// Flush and close streams.
cos.flush();
cos.close();
fis.close();
}
static byte[] decrypt(String inputPath) throws IOException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException {
FileInputStream fis = new FileInputStream(inputPath);
// FileOutputStream fos = new FileOutputStream(outputPath);
SecretKeySpec sks = new SecretKeySpec("MyDifficultPassw".getBytes(), "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, sks);
CipherInputStream cis = new CipherInputStream(fis, cipher);
int b;
byte[] d = new byte[1024];
ByteArrayOutputStream bos = new ByteArrayOutputStream();
int count =0;
while((b = cis.read(d)) != -1) {
if(count <= 1024){
count += b;
bos.write(d, 0, b);
}else{
bos.write(d, 0, b);
}
}
byte[] completeBytes = bos.toByteArray();
cis.close();
return completeBytes;
}
请建议一些东西..