Beispiel #1
0
        public static DecryptionResult DecryptText(string inputText, string password)
        {
            ReaderWriterLockSlim locker           = new ReaderWriterLockSlim();
            DecryptionResult     decryptionResult = null;

            try
            {
                locker.EnterReadLock();
                byte[] EncryptedBytes = null;
                try
                {
                    EncryptedBytes = System.Convert.FromBase64String(inputText);
                }
                catch (Exception)
                {
                    decryptionResult = new DecryptionResult()
                    {
                        Result          = false,
                        Error           = "Text is not a valid encrypted message",
                        DecryptedString = null
                    };
                    return(decryptionResult);
                }
                byte[] passwordBytes = System.Text.Encoding.UTF8.GetBytes(password);
                passwordBytes = SHA256.Create().ComputeHash(passwordBytes);
                byte[] DecryptedBytes = DecryptionService.GetDecryptedByteArray(EncryptedBytes, passwordBytes);
                try
                {
                    decryptionResult = new DecryptionResult()
                    {
                        DecryptedString = System.Text.Encoding.UTF8.GetString(DecryptedBytes),
                        Result          = true,
                        Error           = null
                    };
                }
                catch (Exception)
                {
                    decryptionResult = new DecryptionResult()
                    {
                        Result          = false,
                        Error           = "Text is not a valid encrypted message",
                        DecryptedString = null
                    };
                    return(decryptionResult);
                }
            }
            catch (Exception ex)
            {
                decryptionResult.Result = false;
                decryptionResult.Error  = ex.Message;
            }
            finally
            {
                locker.ExitReadLock();
            }
            return(decryptionResult);
        }
Beispiel #2
0
 protected override void OnHandleIntent(Intent intent)
 {
     Task.Run(async() =>
     {
         try
         {
             var TempAllNotes = await NotesDatabase.GetAllNotesList();
             var AllNotes     = new List <ExportNoteJsonModel>();
             if (TempAllNotes != null)
             {
                 string password = await SecureStorage.GetAsync("AppPassword");
                 if (password == null)
                 {
                     return;
                 }
                 for (int i = 0; i < TempAllNotes.Count; i++)
                 {
                     try
                     {
                         var DecryptionResult = DecryptionService.DecryptText(TempAllNotes[i].NoteContent, password);
                         if (DecryptionResult.Result)
                         {
                             AllNotes.Add(new ExportNoteJsonModel(TempAllNotes[i].NoteTitle, DecryptionResult.DecryptedString, TempAllNotes[i].NoteCreationTime, TempAllNotes[i].NoteLastModifiedTime));
                         }
                     }
                     catch (Exception)
                     {
                     }
                 }
             }
             System.Text.Json.JsonSerializerOptions jsonSerializerOptions = new JsonSerializerOptions()
             {
                 WriteIndented = true
             };
             var CryptoAppPath = System.IO.Path.Combine(Android.OS.Environment.ExternalStorageDirectory.Path, "Crypto App");
             if (!Directory.Exists(CryptoAppPath))
             {
                 System.IO.Directory.CreateDirectory(CryptoAppPath);
             }
             string ExportFilePath = System.IO.Path.Combine(CryptoAppPath, "CryptoApp_Notes_Export_" + DateTime.Now.ToString("ddMMyyyyHmmss") + ".json");
             File.WriteAllText(ExportFilePath, System.Text.Json.JsonSerializer.Serialize(AllNotes, jsonSerializerOptions));
             Xamarin.Essentials.MainThread.BeginInvokeOnMainThread(() =>
             {
                 Toast.MakeText(Android.App.Application.Context, "Note exported", ToastLength.Long).Show();
             });
         }
         catch (Exception)
         {
             Xamarin.Essentials.MainThread.BeginInvokeOnMainThread(() =>
             {
                 Toast.MakeText(Android.App.Application.Context, "Note could not be exported", ToastLength.Long).Show();
             });
         }
     });
 }
Beispiel #3
0
        public static DecryptionResult DecryptFile(string inputFile, string password)
        {
            DecryptionResult     decryptionResult = null;
            ReaderWriterLockSlim locker           = new ReaderWriterLockSlim();

            try
            {
                locker.EnterReadLock();
                byte[] FileContentBytes = File.ReadAllBytes(inputFile);
                int    ctr = 0;
                for (int i = 999; i >= 0; i--)
                {
                    if (FileContentBytes[i] != 0)
                    {
                        ctr = i;
                        break;
                    }
                }
                int ctr2 = 1000;
                for (int i = 1999; i >= 1000; i--)
                {
                    if (FileContentBytes[i] != 0)
                    {
                        ctr2 = i;
                        break;
                    }
                }
                int    FileNameBytesLength  = ctr + 1;
                int    ExtensionBytesLength = ctr2 + 1 - 1000;
                byte[] passwordBytes        = SHA256.Create().ComputeHash(System.Text.Encoding.ASCII.GetBytes(password));
                byte[] SaltedHashedPassword = SHA256.Create().ComputeHash(System.Text.Encoding.ASCII.GetBytes(password + "CryptoApp"));
                byte[] FileNameBytes        = new byte[FileNameBytesLength];
                byte[] ExtensionBytes       = new byte[ExtensionBytesLength];
                byte[] StoredHashedPassword = new byte[32];
                byte[] EncryptedBytes       = new byte[FileContentBytes.Length - 2032];
                System.Buffer.BlockCopy(FileContentBytes, 2000, StoredHashedPassword, 0, 32);
                bool ArePasswordsSame = true;
                for (int i = 0; i < 32; i++)
                {
                    if (SaltedHashedPassword[i] != StoredHashedPassword[i])
                    {
                        ArePasswordsSame = false;
                        break;
                    }
                }
                if (ArePasswordsSame == false)
                {
                    decryptionResult = new DecryptionResult()
                    {
                        Result          = false,
                        Error           = "Password",
                        DecryptedString = null
                    };
                    return(decryptionResult);
                }
                System.Buffer.BlockCopy(FileContentBytes, 0, FileNameBytes, 0, FileNameBytesLength);
                System.Buffer.BlockCopy(FileContentBytes, 1000, ExtensionBytes, 0, ExtensionBytesLength);
                System.Buffer.BlockCopy(FileContentBytes, 2032, EncryptedBytes, 0, EncryptedBytes.Length);
                byte[] DecryptedBytes       = DecryptionService.GetDecryptedByteArray(EncryptedBytes, passwordBytes);
                string CurrentDirectoryPath = Path.Combine(Android.OS.Environment.ExternalStorageDirectory.Path, "Crypto App");
                if (!Directory.Exists(CurrentDirectoryPath))
                {
                    System.IO.Directory.CreateDirectory(CurrentDirectoryPath);
                }
                string CurrentFileName      = System.Text.Encoding.ASCII.GetString(FileNameBytes);
                string CurrentFileExtension = System.Text.Encoding.ASCII.GetString(ExtensionBytes);
                string WritePath            = System.IO.Path.Combine(CurrentDirectoryPath, CurrentFileName + CurrentFileExtension);
                if (System.IO.File.Exists(WritePath))
                {
                    Int64 ctr3          = 1;
                    var   TempWritePath = System.IO.Path.Combine(CurrentDirectoryPath, CurrentFileName + "_" + ctr3.ToString() + CurrentFileExtension);
                    while (System.IO.File.Exists(TempWritePath) && ctr3 < int.MaxValue)
                    {
                        ctr3          = ctr3 + 1;
                        TempWritePath = System.IO.Path.Combine(CurrentDirectoryPath, CurrentFileName + "_" + ctr3.ToString() + CurrentFileExtension);
                    }
                    WritePath = TempWritePath;
                }
                File.WriteAllBytes(WritePath, DecryptedBytes);
                decryptionResult = new DecryptionResult()
                {
                    Result          = true,
                    Error           = null,
                    DecryptedString = System.IO.Path.GetFileName(WritePath)
                };
            }
            catch (Exception ex)
            {
                decryptionResult = new DecryptionResult()
                {
                    Result          = false,
                    Error           = ex.Message,
                    DecryptedString = null
                };
            }
            finally
            {
                locker.ExitReadLock();
            }
            return(decryptionResult);
        }