Fix decryption

Signed-off-by: alperozturk <alper_ozturk@proton.me>
This commit is contained in:
alperozturk 2024-03-22 10:05:31 +01:00 committed by Alper Öztürk
parent c0f23c435a
commit 67ddd0ba38
2 changed files with 22 additions and 23 deletions

View file

@ -267,18 +267,14 @@ public class DownloadFileOperation extends RemoteOperation {
try {
Cipher cipher = EncryptionUtils.getCipher(Cipher.DECRYPT_MODE, key, iv);
tmpFile = EncryptionUtils.decryptFile(tmpFile, authenticationTagString, cipher, new ArbitraryDataProviderImpl(operationContext), user);
EncryptionUtils.decryptFile(cipher, tmpFile, newFile, authenticationTagString, new ArbitraryDataProviderImpl(operationContext), user);
} catch (Exception e) {
return new RemoteOperationResult(e);
}
}
if (downloadType == DownloadType.DOWNLOAD) {
moved = tmpFile.renameTo(newFile);
newFile.setLastModified(file.getModificationTimestamp());
if (!moved) {
result = new RemoteOperationResult(RemoteOperationResult.ResultCode.LOCAL_STORAGE_NOT_MOVED);
}
} else if (downloadType == DownloadType.EXPORT) {
new FileExportUtils().exportFile(file.getFileName(),
file.getMimeType(),

View file

@ -568,6 +568,7 @@ public final class EncryptionUtils {
File encryptedFile = new File(file.getAbsolutePath() + ".enc");
encryptFileWithGivenCipher(file, encryptedFile, cipher);
String authenticationTagString = getAuthenticationTag(cipher);
Log_OC.d("", "KAVGAM!!: " + authenticationTagString);
return new EncryptedFile(encryptedFile, authenticationTagString);
}
@ -600,33 +601,35 @@ public final class EncryptionUtils {
inputStream.close();
}
public static File decryptFile(File encryptedFile,
public static void decryptFile(Cipher cipher,
File encryptedFile,
File decryptedFile,
String authenticationTag,
Cipher cipher,
ArbitraryDataProvider arbitraryDataProvider,
User user) throws InvalidParameterSpecException {
File decryptedFile = new File(encryptedFile.getAbsolutePath().replace(".enc", "_decrypted"));
try (FileInputStream inputStream = new FileInputStream(encryptedFile);
FileOutputStream fileOutputStream = new FileOutputStream(decryptedFile);
CipherInputStream cipherInputStream = new CipherInputStream(inputStream, cipher)) {
User user) throws IOException,
BadPaddingException, IllegalBlockSizeException, InvalidParameterSpecException {
FileInputStream inputStream = new FileInputStream(encryptedFile);
FileOutputStream outputStream = new FileOutputStream(decryptedFile);
byte[] buffer = new byte[4096];
int bytesRead;
while ((bytesRead = cipherInputStream.read(buffer)) != -1) {
fileOutputStream.write(buffer, 0, bytesRead);
while ((bytesRead = inputStream.read(buffer)) != -1) {
byte[] output = cipher.update(buffer, 0, bytesRead);
if (output != null) {
outputStream.write(output);
}
} catch (Exception e) {
Log_OC.d(TAG, "Error caught at decryptFile(): " + e.getLocalizedMessage());
}
byte[] output = cipher.doFinal();
if (output != null) {
outputStream.write(output);
}
inputStream.close();
outputStream.close();
if (!getAuthenticationTag(cipher).equals(authenticationTag)) {
reportE2eError(arbitraryDataProvider, user);
throw new SecurityException("Tag not correct");
}
return decryptedFile;
}
// FIXME Decryption is broken