Extract cipher from decryption

Signed-off-by: alperozturk <alper_ozturk@proton.me>
This commit is contained in:
alperozturk 2024-03-22 09:11:02 +01:00 committed by Alper Öztürk
parent b8af027528
commit c2f7d00505
2 changed files with 12 additions and 26 deletions

View file

@ -51,6 +51,8 @@ import java.util.Iterator;
import java.util.Set; import java.util.Set;
import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicBoolean;
import javax.crypto.Cipher;
import static com.owncloud.android.utils.EncryptionUtils.decodeStringToBase64Bytes; import static com.owncloud.android.utils.EncryptionUtils.decodeStringToBase64Bytes;
/** /**
@ -265,9 +267,9 @@ public class DownloadFileOperation extends RemoteOperation {
byte[] authenticationTag = decodeStringToBase64Bytes(authenticationTagString); byte[] authenticationTag = decodeStringToBase64Bytes(authenticationTagString);
try { try {
byte[] decryptedBytes = EncryptionUtils.decryptFile(tmpFile, Cipher cipher = EncryptionUtils.getCipher(Cipher.DECRYPT_MODE, key, iv);
key, byte[] decryptedBytes = EncryptionUtils.decryptFile(cipher,
iv, tmpFile,
authenticationTag, authenticationTag,
new ArbitraryDataProviderImpl(operationContext), new ArbitraryDataProviderImpl(operationContext),
user); user);

View file

@ -579,7 +579,7 @@ public final class EncryptionUtils {
InvalidAlgorithmParameterException, NoSuchPaddingException, InvalidKeyException, IOException, InvalidParameterSpecException { InvalidAlgorithmParameterException, NoSuchPaddingException, InvalidKeyException, IOException, InvalidParameterSpecException {
File file = new File(ocFile.getStoragePath()); File file = new File(ocFile.getStoragePath());
Cipher cipher = getEncoderCipher(encryptionKeyBytes, iv); Cipher cipher = getCipher(Cipher.ENCRYPT_MODE, encryptionKeyBytes, iv);
return encryptFile(file, cipher); return encryptFile(file, cipher);
} }
@ -591,11 +591,11 @@ public final class EncryptionUtils {
return new EncryptedFile(encryptedFile, authenticationTagString); return new EncryptedFile(encryptedFile, authenticationTagString);
} }
private static Cipher getEncoderCipher(byte[] encryptionKeyBytes, byte[] iv) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidAlgorithmParameterException, InvalidKeyException { public static Cipher getCipher(int mode, byte[] encryptionKeyBytes, byte[] iv) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidAlgorithmParameterException, InvalidKeyException {
Cipher cipher = Cipher.getInstance(AES_CIPHER); Cipher cipher = Cipher.getInstance(AES_CIPHER);
Key key = new SecretKeySpec(encryptionKeyBytes, AES); Key key = new SecretKeySpec(encryptionKeyBytes, AES);
GCMParameterSpec spec = new GCMParameterSpec(128, iv); GCMParameterSpec spec = new GCMParameterSpec(128, iv);
cipher.init(Cipher.ENCRYPT_MODE, key, spec); cipher.init(mode, key, spec);
return cipher; return cipher;
} }
@ -616,29 +616,13 @@ public final class EncryptionUtils {
} }
// FIXME Decryption is broken // FIXME Decryption is broken
/** public static byte[] decryptFile(
* @param file encrypted file Cipher cipher,
* @param encryptionKeyBytes key from metadata File file,
* @param iv initialization vector from metadata
* @param authenticationTag authenticationTag from metadata
* @return decrypted byte[]
*/
public static byte[] decryptFile(File file,
byte[] encryptionKeyBytes,
byte[] iv,
byte[] authenticationTag, byte[] authenticationTag,
ArbitraryDataProvider arbitraryDataProvider, ArbitraryDataProvider arbitraryDataProvider,
User user) User user)
throws NoSuchAlgorithmException, throws BadPaddingException, IllegalBlockSizeException, IOException {
InvalidAlgorithmParameterException, NoSuchPaddingException, InvalidKeyException,
BadPaddingException, IllegalBlockSizeException, IOException {
Cipher cipher = Cipher.getInstance(AES_CIPHER);
Key key = new SecretKeySpec(encryptionKeyBytes, AES);
GCMParameterSpec spec = new GCMParameterSpec(128, iv);
cipher.init(Cipher.DECRYPT_MODE, key, spec);
RandomAccessFile randomAccessFile = new RandomAccessFile(file, "r"); RandomAccessFile randomAccessFile = new RandomAccessFile(file, "r");
byte[] fileBytes = new byte[(int) randomAccessFile.length()]; byte[] fileBytes = new byte[(int) randomAccessFile.length()];
randomAccessFile.readFully(fileBytes); randomAccessFile.readFully(fileBytes);