diff --git a/lib/core/configuration.dart b/lib/core/configuration.dart index 0c51ff6a9..bcd031f72 100644 --- a/lib/core/configuration.dart +++ b/lib/core/configuration.dart @@ -73,9 +73,9 @@ class Configuration { if (!correctPassphrase) { throw Exception("Incorrect passphrase"); } - final key = CryptoUtil.decryptFromBase64( - attributes.encryptedKey, base64.encode(kek), attributes.encryptedKeyIV); - await setKey(key); + final key = CryptoUtil.aesDecrypt(base64.decode(attributes.encryptedKey), + kek, base64.decode(attributes.encryptedKeyIV)); + await setKey(base64.encode(key)); } String getHttpEndpoint() { diff --git a/lib/file_upload_manager.dart b/lib/file_upload_manager.dart index d81743f68..fa6814c8f 100644 --- a/lib/file_upload_manager.dart +++ b/lib/file_upload_manager.dart @@ -53,7 +53,7 @@ class FileUploadManager { final tempDirectory = Configuration.instance.getTempDirectory(); final encryptedFilePath = tempDirectory + encryptedFileName; await CryptoUtil.encryptDataToFile( - await getBytesFromDisk(file), encryptedFilePath, base64EncodedKey); + await getBytesFromDisk(file), encryptedFilePath, key); final fileUploadURL = await getUploadURL(); String fileObjectKey = @@ -65,7 +65,7 @@ class FileUploadManager { file.generatedID.toString() + "_thumbnail.aes"; final encryptedThumbnailPath = tempDirectory + encryptedThumbnailName; await CryptoUtil.encryptDataToFile( - thumbnailData, encryptedThumbnailPath, base64EncodedKey); + thumbnailData, encryptedThumbnailPath, key); final thumbnailUploadURL = await getUploadURL(); String thumbnailObjectKey = @@ -73,7 +73,7 @@ class FileUploadManager { final metadata = jsonEncode(file.getMetadata()); final encryptedMetadata = await CryptoUtil.encryptDataToData( - utf8.encode(metadata), base64EncodedKey); + utf8.encode(metadata), key); final data = { "fileObjectKey": fileObjectKey, "thumbnailObjectKey": thumbnailObjectKey, diff --git a/lib/models/file.dart b/lib/models/file.dart index 56a4298e7..82e9f89fa 100644 --- a/lib/models/file.dart +++ b/lib/models/file.dart @@ -1,3 +1,6 @@ +import 'dart:convert'; +import 'dart:typed_data'; + import 'package:photo_manager/photo_manager.dart'; import 'package:path/path.dart'; import 'package:photos/core/configuration.dart'; @@ -135,12 +138,12 @@ class File { Configuration.instance.getToken(); } - String getKey() { + Uint8List getKey() { if (encryptedKey == null) { return null; } - return CryptoUtil.decryptFromBase64( - encryptedKey, Configuration.instance.getBase64EncodedKey(), encryptedKeyIV); + return CryptoUtil.aesDecrypt(base64.decode(encryptedKey), + Configuration.instance.getKey(), base64.decode(encryptedKeyIV)); } @override diff --git a/lib/photo_sync_manager.dart b/lib/photo_sync_manager.dart index 27d86ad05..ab0bea1ec 100644 --- a/lib/photo_sync_manager.dart +++ b/lib/photo_sync_manager.dart @@ -281,18 +281,20 @@ class PhotoSyncManager { if (response != null) { Bus.instance.fire(RemoteSyncEvent(true)); final diff = response.data["diff"] as List; - for (final json in diff) { + for (final fileItem in diff) { final file = File(); - file.uploadedFileID = json["id"]; - file.ownerID = json["ownerID"]; - file.updationTime = json["updationTime"]; + file.uploadedFileID = fileItem["id"]; + file.ownerID = fileItem["ownerID"]; + file.updationTime = fileItem["updationTime"]; file.isEncrypted = true; - file.encryptedKey = json["encryptedKey"]; - file.encryptedKeyIV = json["encryptedKeyIV"]; - final key = CryptoUtil.decryptFromBase64(file.encryptedKey, - Configuration.instance.getBase64EncodedKey(), file.encryptedKeyIV); + file.encryptedKey = fileItem["encryptedKey"]; + file.encryptedKeyIV = fileItem["encryptedKeyIV"]; + final key = CryptoUtil.aesDecrypt( + base64.decode(file.encryptedKey), + Configuration.instance.getKey(), + base64.decode(file.encryptedKeyIV)); Map metadata = jsonDecode(utf8.decode( - await CryptoUtil.decryptDataToData(json["metadata"], key))); + await CryptoUtil.decryptDataToData(fileItem["metadata"], key))); file.applyMetadata(metadata); files.add(file); } diff --git a/lib/utils/crypto_util.dart b/lib/utils/crypto_util.dart index 8c0bc4a0d..9bd43381b 100644 --- a/lib/utils/crypto_util.dart +++ b/lib/utils/crypto_util.dart @@ -31,18 +31,18 @@ class CryptoUtil { .bytes; } - static String decryptFromBase64( - String base64CipherText, String base64Key, String base64IV) { - final encrypter = AES(Key.fromBase64(base64Key), mode: AESMode.cbc); - return utf8.decode(encrypter.decrypt( - Encrypted.fromBase64(base64CipherText), - iv: IV.fromBase64(base64IV), - )); + static Uint8List aesDecrypt( + Uint8List cipherText, Uint8List key, Uint8List iv) { + final encrypter = AES(Key(key), mode: AESMode.cbc); + return encrypter.decrypt( + Encrypted(cipherText), + iv: IV(iv), + ); } static Future encryptFileToFile( - String sourcePath, String destinationPath, String key) async { - final args = Map(); + String sourcePath, String destinationPath, Uint8List key) async { + final args = Map(); args["key"] = key; args["source"] = sourcePath; args["destination"] = destinationPath; @@ -50,20 +50,19 @@ class CryptoUtil { } static Future encryptDataToFile( - Uint8List source, String destinationPath, String base64EncodedKey) async { + Uint8List source, String destinationPath, Uint8List key) async { final args = Map(); - args["key"] = base64EncodedKey; + args["key"] = key; args["source"] = source; args["destination"] = destinationPath; return Computer().compute(runEncryptDataToFile, param: args); } static Future encryptDataToData( - Uint8List source, String base64EncodedKey) async { + Uint8List source, Uint8List key) async { final destinationPath = Configuration.instance.getTempDirectory() + Uuid().v4(); - return encryptDataToFile(source, destinationPath, base64EncodedKey) - .then((value) { + return encryptDataToFile(source, destinationPath, key).then((value) { final file = io.File(destinationPath); final data = file.readAsBytesSync(); file.deleteSync(); @@ -71,52 +70,50 @@ class CryptoUtil { }); } - static Future decryptFileToFile(String sourcePath, - String destinationPath, String base64EncodedKey) async { - final args = Map(); - args["key"] = base64EncodedKey; + static Future decryptFileToFile( + String sourcePath, String destinationPath, Uint8List key) async { + final args = Map(); + args["key"] = key; args["source"] = sourcePath; args["destination"] = destinationPath; return Computer().compute(runDecryptFileToFile, param: args); } - static Future decryptFileToData( - String sourcePath, String base64EncodedKey) { - final args = Map(); - args["key"] = base64EncodedKey; + static Future decryptFileToData(String sourcePath, Uint8List key) { + final args = Map(); + args["key"] = key; args["source"] = sourcePath; return Computer().compute(runDecryptFileToData, param: args); } - static Future decryptDataToData( - Uint8List source, String base64EncodedKey) { + static Future decryptDataToData(Uint8List source, Uint8List key) { final sourcePath = Configuration.instance.getTempDirectory() + Uuid().v4(); final file = io.File(sourcePath); file.writeAsBytesSync(source); - return decryptFileToData(sourcePath, base64EncodedKey).then((value) { + return decryptFileToData(sourcePath, key).then((value) { file.deleteSync(); return value; }); } } -Future runEncryptFileToFile(Map args) { - final encrypter = getEncrypter(args["key"]); +Future runEncryptFileToFile(Map args) { + final encrypter = getEncrypter(base64.encode(args["key"] as Uint8List)); return encrypter.encryptFile(args["source"], args["destination"]); } Future runEncryptDataToFile(Map args) { - final encrypter = getEncrypter(args["key"]); + final encrypter = getEncrypter(base64.encode(args["key"] as Uint8List)); return encrypter.encryptDataToFile(args["source"], args["destination"]); } Future runDecryptFileToFile(Map args) async { - final encrypter = getEncrypter(args["key"]); + final encrypter = getEncrypter(base64.encode(args["key"] as Uint8List)); return encrypter.decryptFile(args["source"], args["destination"]); } Future runDecryptFileToData(Map args) async { - final encrypter = getEncrypter(args["key"]); + final encrypter = getEncrypter(base64.encode(args["key"] as Uint8List)); return encrypter.decryptDataFromFile(args["source"]); }