Skip to content

Commit

Permalink
Increase performance
Browse files Browse the repository at this point in the history
Signed-off-by: alperozturk <alper_ozturk@proton.me>
  • Loading branch information
alperozturk96 committed Mar 22, 2024
1 parent c5bae2d commit a87c0e7
Showing 1 changed file with 40 additions and 31 deletions.
71 changes: 40 additions & 31 deletions app/src/main/java/com/owncloud/android/utils/EncryptionUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -564,7 +564,7 @@ public static byte[] decodeStringToBase64Bytes(String string) {
return Base64.decode(string, Base64.NO_WRAP);
}

public static EncryptedFile encryptFile(File file, Cipher cipher) throws IOException, InvalidParameterSpecException {
public static EncryptedFile encryptFile(File file, Cipher cipher) throws InvalidParameterSpecException {
File encryptedFile = new File(file.getAbsolutePath() + ".enc");
encryptFileWithGivenCipher(file, encryptedFile, cipher);
String authenticationTagString = getAuthenticationTag(cipher);
Expand All @@ -584,50 +584,59 @@ public static Cipher getCipher(int mode, byte[] encryptionKeyBytes, byte[] iv) t
return cipher;
}

public static void encryptFileWithGivenCipher(File inputFile, File encryptedFile, Cipher cipher) throws IOException {
FileInputStream inputStream = new FileInputStream(inputFile);
FileOutputStream fileOutputStream = new FileOutputStream(encryptedFile);
CipherOutputStream outputStream = new CipherOutputStream(fileOutputStream, cipher);
public static void encryptFileWithGivenCipher(File inputFile, File encryptedFile, Cipher cipher) {
try( FileInputStream inputStream = new FileInputStream(inputFile);
FileOutputStream fileOutputStream = new FileOutputStream(encryptedFile);
CipherOutputStream outputStream = new CipherOutputStream(fileOutputStream, cipher)) {
byte[] buffer = new byte[4096];
int bytesRead;

byte[] buffer = new byte[4096];
int bytesRead;
while ((bytesRead = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}

while ((bytesRead = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
outputStream.close();
inputStream.close();

outputStream.close();
inputStream.close();
Log_OC.d(TAG, encryptedFile.getName() + "encrypted successfully");
} catch (IOException exception) {
Log_OC.d(TAG, "Error caught at encryptFileWithGivenCipher(): " + exception.getLocalizedMessage());
}
}

public static void decryptFile(Cipher cipher,
File encryptedFile,
File decryptedFile,
String authenticationTag,
ArbitraryDataProvider arbitraryDataProvider,
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 = inputStream.read(buffer)) != -1) {
byte[] output = cipher.update(buffer, 0, bytesRead);
User user) {
try (FileInputStream inputStream = new FileInputStream(encryptedFile);
FileOutputStream outputStream = new FileOutputStream(decryptedFile)) {

byte[] buffer = new byte[4096];
int bytesRead;
while ((bytesRead = inputStream.read(buffer)) != -1) {
byte[] output = cipher.update(buffer, 0, bytesRead);
if (output != null) {
outputStream.write(output);
}
}
byte[] output = cipher.doFinal();
if (output != null) {
outputStream.write(output);
}
}
byte[] output = cipher.doFinal();
if (output != null) {
outputStream.write(output);
}
inputStream.close();
outputStream.close();
inputStream.close();
outputStream.close();

if (!getAuthenticationTag(cipher).equals(authenticationTag)) {
reportE2eError(arbitraryDataProvider, user);
throw new SecurityException("Tag not correct");
if (!getAuthenticationTag(cipher).equals(authenticationTag)) {
reportE2eError(arbitraryDataProvider, user);
throw new SecurityException("Tag not correct");
}

Log_OC.d(TAG, encryptedFile.getName() + "decrypted successfully");
} catch (IOException | BadPaddingException | IllegalBlockSizeException | InvalidParameterSpecException |
SecurityException exception) {
Log_OC.d(TAG, "Error caught at decryptFile(): " + exception.getLocalizedMessage());
}
}

Expand Down

0 comments on commit a87c0e7

Please sign in to comment.