Пример #1
0
 public EncryptedRecordDataModel(EncryptedDictionary encrypted, string display, T openData, string pairingKey)
 {
     _encrypted = encrypted;
     Display    = display;
     OpenData   = openData;
     PairingKey = pairingKey;
 }
Пример #2
0
        public void AddNewRecord()
        {
            var openData = new OpenCryptoKeysData()
            {
                CanTrade = CanTrade, CanWithdraw = CanWithdraw, IPs = IPs, Passphrase = Passphrase
            };

            if (UseExpireDate)
            {
                openData.ExpireDate = ExpireDate;
            }
            var encrypted = EncryptedRecordDataModel <OpenCryptoKeysData> .Create(EncryptedDictionary, Display, Key ?? string.Empty, Secret ?? string.Empty, openData);

            Records.Add(encrypted);
            if (EncryptedDictionary.KeyPassword != null)
            {
                EncryptedDictionary.EncryptAll();
                EncryptedDictionary.Save(FilenameSecret);
                OpenRecords.Add(new OpenRecordDataModel <OpenCryptoKeysData>()
                {
                    Display = Display, PairingKey = encrypted.PairingKey, OpenData = openData
                });
                SaveOpenRecords(FilenameOpen);
            }

            Display       = Key = Secret = Passphrase = IPs = null;
            UseExpireDate = CanTrade = CanWithdraw = false;
            foreach (var value in new string[] { nameof(Display), nameof(Key), nameof(Secret), nameof(Passphrase), nameof(IPs),
                                                 nameof(UseExpireDate), nameof(CanTrade), nameof(CanWithdraw) })
            {
                PropertyChanged(this, new PropertyChangedEventArgs(value));
            }
        }
Пример #3
0
        public void DoActiveStateAction(SecureString password)
        {
            switch (ActionButtonText)
            {
            case ACTION_DECRYPT:
                if (password.Length > 0)
                {
                    ActionButtonText = ACTION_STOP;
                    var localTaskId = _taskId;
                    _encryptionTask = Task.Run(() =>
                    {
                        var pwd = new System.Net.NetworkCredential(string.Empty, password).Password;
                        EncryptedDictionary.DecryptKey(pwd, () => localTaskId == _taskId);
                        if (ActionButtonText == ACTION_STOP && localTaskId == _taskId)
                        {
                            ActionButtonText       = ACTION_ENCRYPT;
                            var wasAnyNotEncrypted = EncryptedDictionary.Dictionary.Values.Any(x => x.Encrypted == null);
                            EncryptedDictionary.DecryptPart();
                            foreach (var record in Records)
                            {
                                record.NotifyEncryptionChange();
                            }
                            if (wasAnyNotEncrypted)
                            {
                                EncryptedDictionary.Save(FilenameSecret);
                                SaveOpenRecords(FilenameOpen);
                            }
                            IsUnlocked = true;
                            ClearPassword?.Invoke();
                        }
                    }).ContinueWith(x => OnDecryption());
                }
                break;

            case ACTION_STOP:
                if (_encryptionTask != null)
                {
                    ++_taskId;
                    _encryptionTask  = null;
                    ActionButtonText = ACTION_DECRYPT;
                }
                break;

            case ACTION_ENCRYPT:
                ActionButtonText = ACTION_DECRYPT;
                EncryptedDictionary.EncryptAll();
                EncryptedDictionary.ClearKeyAndPartialEncryption();
                IsUnlocked = false;
                foreach (var record in Records)
                {
                    record.NotifyEncryptionChange();
                }
                OnEncryption();
                break;
            }
        }
Пример #4
0
 public FakeObject(EncryptedDictionary encrypted)
 {
     Dict = new Dictionary <string, FakeItem>();
     Dict.Add("itemName", new FakeItem(encrypted)
     {
         Key = "localKey", Text = "Something"
     });
     Dict.Add("itemNameCopy", new FakeItem(encrypted)
     {
         Key = "localKeyCopy", Text = "Something"
     });
 }
Пример #5
0
 public EncryptedRecordDataModel(string originalKey, string originalSecret, string display, T openData, string pairingKey)
 {
     PairingKey = pairingKey;
     OpenData   = openData;
     Display    = display;
     _encrypted = new EncryptedDictionary();
     _encrypted.Dictionary.Add(pairingKey + "_key", new EncryptedNode()
     {
         Original = originalKey
     });
     _encrypted.Dictionary.Add(pairingKey + "_secret", new EncryptedNode()
     {
         Original = originalSecret
     });
 }
Пример #6
0
 public SafeViewerCryptoKeysViewModel()
 {
     OpenRecords         = new List <OpenRecordDataModel <OpenCryptoKeysData> >();
     Records             = new ObservableCollection <EncryptedRecordDataModel <OpenCryptoKeysData> >();
     ActionButtonText    = ACTION_DECRYPT;
     EncryptedDictionary = EncryptedDictionary.LoadOrCreate(FilenameSecret);
     if (EncryptedDictionary.EncryptionService.IsInitializationRunning)
     {
         ActionButtonText = ACTION_WAIT;
         IsActionEnabled  = false;
         EncryptedDictionary.EncryptionService.InitializationCallback = () =>
         {
             ActionButtonText = ACTION_DECRYPT;
             IsActionEnabled  = true;
             Iterations       = EncryptedDictionary.EncryptionService.PasswordIterations;
             EncryptedDictionary.Save(FilenameSecret);
         };
     }
     else
     {
         Iterations = EncryptedDictionary.EncryptionService.PasswordIterations;
         foreach (var value in EncryptedDictionary.Dictionary)
         {
             var keySecret = value.Key.Split('_');
             if (keySecret.Length > 1 && keySecret[1] == "key")
             {
                 var secret = keySecret[0];
                 Records.Add(new EncryptedRecordDataModel <OpenCryptoKeysData>(EncryptedDictionary, null, null, secret));
             }
         }
         LoadOpenRecords(FilenameOpen);
         foreach (var openRecord in OpenRecords)
         {
             var encrypted = Records.FirstOrDefault(x => x.PairingKey == openRecord.PairingKey);
             if (encrypted != null)
             {
                 encrypted.OpenData = openRecord.OpenData;
                 encrypted.Display  = openRecord.Display;
             }
         }
     }
     Exchanges = new ObservableCollection <string>();
 }
Пример #7
0
 public SafeViewerViewModel()
 {
     OpenRecords         = new List <OpenRecordDataModel <object> >();
     Records             = new ObservableCollection <EncryptedRecordDataModel <object> >();
     ActionButtonText    = ACTION_DECRYPT;
     EncryptedDictionary = EncryptedDictionary.LoadOrCreate("secrets.json");
     if (EncryptedDictionary.EncryptionService.IsInitializationRunning)
     {
         ActionButtonText = ACTION_WAIT;
         IsActionEnabled  = false;
         EncryptedDictionary.EncryptionService.InitializationCallback = () =>
         {
             ActionButtonText = ACTION_DECRYPT;
             IsActionEnabled  = true;
             Iterations       = EncryptedDictionary.EncryptionService.PasswordIterations;
             EncryptedDictionary.Save("secrets.json");
         };
     }
     else
     {
         Iterations = EncryptedDictionary.EncryptionService.PasswordIterations;
         foreach (var value in EncryptedDictionary.Dictionary)
         {
             var keySecret = value.Key.Split('_');
             if (keySecret.Length > 1 && keySecret[1] == "key")
             {
                 Records.Add(new EncryptedRecordDataModel <object>(EncryptedDictionary, null, null, keySecret[0]));
             }
         }
         LoadOpenRecords("open_data.json");
         // TO-DO: pair encrypted records with open records
         foreach (var openRecord in OpenRecords)
         {
             var encrypted = Records.FirstOrDefault(x => x.PairingKey == openRecord.PairingKey);
             if (encrypted != null)
             {
                 encrypted.OpenData = openRecord.OpenData;
                 encrypted.Display  = openRecord.Display;
             }
         }
     }
 }
Пример #8
0
 public void DeleteRecord(EncryptedRecordDataModel <OpenCryptoKeysData> record)
 {
     for (int i = OpenRecords.Count - 1; i >= 0; --i)
     {
         if (OpenRecords[i].PairingKey == record.PairingKey)
         {
             OpenRecords.RemoveAt(i);
             break;
         }
     }
     SaveOpenRecords(FilenameOpen);
     for (int i = Records.Count - 1; i >= 0; --i)
     {
         if (Records[i].PairingKey == record.PairingKey)
         {
             Records.RemoveAt(i);
             break;
         }
     }
     record.Remove();
     EncryptedDictionary.Save(FilenameSecret);
 }
Пример #9
0
        public static EncryptedRecordDataModel <T> Create(EncryptedDictionary encrypted, string display, string key, string secret, T openData)
        {
            var    result        = new EncryptedRecordDataModel <T>(encrypted);
            var    rnd           = new Random();
            string newPairingKey = null;

            do
            {
                newPairingKey = rnd.Next(int.MaxValue).ToString();
            } while (encrypted.Dictionary.ContainsKey(newPairingKey));
            result.PairingKey = newPairingKey;
            result.Display    = display;
            encrypted.Dictionary.Add(newPairingKey + "_key", new EncryptedNode()
            {
                Original = key
            });
            encrypted.Dictionary.Add(newPairingKey + "_secret", new EncryptedNode()
            {
                Original = secret
            });
            result.OpenData = openData;
            return(result);
        }
Пример #10
0
 public FakeItem(EncryptedDictionary encrypted)
 {
     _encrypted = encrypted;
 }
Пример #11
0
        static void Main(string[] args)
        {
            Console.WriteLine("Hello, this is simple encryption application that ilustrates how to use the encryption classes.");
            string filename  = "encrypt.json";
            var    encrypted = EncryptedDictionary.LoadOrCreate(filename);
            var    fakeO     = new FakeObject(encrypted);

            if (encrypted.EncryptionService.IsInitializationRunning)
            {
                Console.WriteLine("Initalization of encryption.");
                while (encrypted.EncryptionService.IsInitializationRunning)
                {
                    System.Threading.Thread.Sleep(1000);
                    Console.Write(".");
                }
                Console.WriteLine();
                Console.WriteLine("Initalization " + (encrypted.EncryptionService.IsInitializationRunning ? "is still running" : "finished"));
                Console.WriteLine("Write a password:"******"We are decrypting your password. This will take one minute.");
                encrypted.DecryptKey(password);

                Console.WriteLine("Write a secret:");
                var secret = Console.ReadLine();
                fakeO.Dict["itemName"].Secret     = secret;
                fakeO.Dict["itemNameCopy"].Secret = secret;

                encrypted.EncryptAll();
                encrypted.Save(filename);
            }
            else
            {
                try
                {
                    Console.WriteLine("Write a password:"******"We are decrypting your password. This will take one minute.");
                    encrypted.DecryptKey(password); // Not needed because DecryptPart will do this
                    encrypted.DecryptPart();

                    Console.WriteLine("Your two secrets are:");
                    Console.WriteLine(fakeO.Dict["itemName"].Secret);
                    Console.WriteLine(fakeO.Dict["itemNameCopy"].Secret);
                } catch (Exception ex)
                {
                    Console.WriteLine("Error: Wrong password.");
                    Console.WriteLine("Write a password:"******"We are decrypting your password. This will take one minute.");
                    encrypted.DecryptKey(password); // Not needed because DecryptPart will do this
                    encrypted.DecryptPart(password);

                    Console.WriteLine("Your two secrets are:");
                    Console.WriteLine(fakeO.Dict["itemName"].Secret);
                    Console.WriteLine(fakeO.Dict["itemNameCopy"].Secret);
                }

                Console.WriteLine("Delete encrypted file Y/N:");
                var yesNo = Console.ReadLine();
                if (yesNo.Contains("y") || yesNo.Contains("Y"))
                {
                    System.IO.File.Delete(filename);
                }
            }

            Console.WriteLine("Press enter to end.");
            Console.ReadLine();
        }
Пример #12
0
 public EncryptedRecordDataModel(EncryptedDictionary encrypted)
 {
     _encrypted = encrypted;
 }