protected override void OnHandleIntent(Intent intent) { Task.Run(async() => { try { int noteID = int.Parse(intent.GetStringExtra(GlobalConstants.NOTE_ID)); string noteContent = intent.GetStringExtra(GlobalConstants.NOTE_CONTENT); DateTime noteUpdateTime = JsonSerializer.Deserialize <DateTime>(intent.GetStringExtra(GlobalConstants.NOTE_UPDATION_DATE)); string password = await SecureStorage.GetAsync("AppPassword"); if (password == null) { throw new Exception("App's password missing"); } NoteModel noteModel = await NotesDatabase.GetNoteWithID(noteID); if (noteModel == null) { throw new Exception("Note doesn't exist"); } EncryptionResult encryptionResult = EncryptionService.EncryptText(noteContent, password); if (encryptionResult.Result) { noteModel.LastEncrypted = DateTime.Now; noteModel.NoteContent = encryptionResult.EncryptedString; noteModel.NoteLastModifiedTime = noteUpdateTime; await NotesDatabase.SaveOrUpdateNode(noteModel); } else { throw new Exception(encryptionResult.Error); } } catch (Exception) { Xamarin.Essentials.MainThread.BeginInvokeOnMainThread(() => { Toast.MakeText(Android.App.Application.Context, "Note could not be updated", ToastLength.Long).Show(); }); } }); }
public static EncryptionResult EncryptFile(string inputFile, string password) { EncryptionResult encryptionResult = null; try { byte[] encryptedBytes = File.ReadAllBytes(inputFile); byte[] passwordToByteArray = System.Text.Encoding.ASCII.GetBytes(password); passwordToByteArray = SHA256.Create().ComputeHash(passwordToByteArray); byte[] SaltedHashedPassword = SHA256.Create().ComputeHash(System.Text.Encoding.ASCII.GetBytes(password + "CryptoApp")); byte[] encryptedByteArray = EncryptionService.GetEncryptedByteArray(encryptedBytes, passwordToByteArray); string CurrentDirectoryPath = System.IO.Path.GetDirectoryName(inputFile); string CurrentFileName = Path.GetFileNameWithoutExtension(inputFile); string CurrentFileExtension = Path.GetExtension(inputFile); byte[] FileNameBytes = System.Text.Encoding.ASCII.GetBytes(CurrentFileName); byte[] ExtensionBytes = System.Text.Encoding.ASCII.GetBytes(CurrentFileExtension); int FileNameBytesLength = FileNameBytes.Length; int ExtensionBytesLength = ExtensionBytes.Length; FileNameBytesLength = (FileNameBytesLength >= 1000) ? 999 : FileNameBytesLength; ExtensionBytesLength = (ExtensionBytesLength >= 1000) ? 999 : ExtensionBytesLength; byte[] FileContentBytes = new byte[1000 + 1000 + 32 + encryptedByteArray.Length]; System.Buffer.BlockCopy(FileNameBytes, 0, FileContentBytes, 0, FileNameBytesLength); System.Buffer.BlockCopy(ExtensionBytes, 0, FileContentBytes, 1000, ExtensionBytesLength); System.Buffer.BlockCopy(SaltedHashedPassword, 0, FileContentBytes, 2000, 32); System.Buffer.BlockCopy(encryptedByteArray, 0, FileContentBytes, 2032, encryptedByteArray.Length); var CryptoAppPath = Path.Combine(Android.OS.Environment.ExternalStorageDirectory.Path, "Crypto App"); if (!Directory.Exists(CryptoAppPath)) { System.IO.Directory.CreateDirectory(CryptoAppPath); } var EncryptedFilesPath = Path.Combine(CryptoAppPath, "Encrypted Files"); if (!Directory.Exists(EncryptedFilesPath)) { System.IO.Directory.CreateDirectory(EncryptedFilesPath); } string WritePath = System.IO.Path.Combine(EncryptedFilesPath, CurrentFileName + ".senc"); if (System.IO.File.Exists(WritePath)) { Int64 ctr3 = 1; var TempWritePath = System.IO.Path.Combine(EncryptedFilesPath, CurrentFileName + "_" + ctr3.ToString() + ".senc"); while (System.IO.File.Exists(TempWritePath) && ctr3 < int.MaxValue) { ctr3 = ctr3 + 1; TempWritePath = System.IO.Path.Combine(EncryptedFilesPath, CurrentFileName + "_" + ctr3.ToString() + ".senc"); } WritePath = TempWritePath; } File.WriteAllBytes(WritePath, FileContentBytes); encryptionResult = new EncryptionResult() { Result = true, Error = null, EncryptedString = CurrentFileName + ".senc" }; } catch (Exception ex) { encryptionResult = new EncryptionResult() { Result = false, Error = ex.Message, EncryptedString = null, }; } return(encryptionResult); }