Esempio n. 1
0
        private void Encrypt(object sender, RoutedEventArgs e)
        {
            bool fail = false;

            if (Data_Box.Text == "")
            {
                Error_Box.Content = "Please enter some text\n";
                fail = true;
            }
            this.text = Data_Box.Text;
            if (filePath == "" || filePath == null)
            {
                Error_Box.Content += "Please select a file";
                fail = true;
            }
            if ((entropyPath == "" || entropyPath == null) && isEntropyExternal == true)
            {
                Error_Box.Content += "Please select an entropy file";
                fail = true;
            }
            if (fail == true)
            {
                return;
            }


            int ID  = 0;
            var rng = new Random();

Loop:
            ID = rng.Next(int.MaxValue);

            foreach (var item in SettingsPage.IDArray)
            {
                if (ID == item)
                {
                    rng.Next(ID);
                    goto Loop;
                }
            }

            using (var BinWriter = new BinaryWriter(File.Create(filePath)))
            {
                BinWriter.Write(ID);
            }

            entropyPath = DecryptPage.EntropyPath = String.Format(Environment.GetFolder(Environment.SpecialFolder.MyComputer) + @"\DataProtector\{0}", ID.ToString());
            _LastLength = SecureDataProtector.ProtectDataToFile(Encoding.UTF8.GetBytes(text), filePath, entropyPath);
            Application.Current.MainWindow.Content = new EncryptionSuccesfulPage();
        }
Esempio n. 2
0
        private void Decrypt(object sender, RoutedEventArgs e)
        {
            var entropy = new byte[16];
            int ID;

            using (var BinReader = new BinaryReader(File.Open(filePath, FileMode.Open)))
            {
                ID           = BinReader.ReadInt32();
                EntropyPath += ID.ToString() + ".dat";
            }
            using (var BinReader = new BinaryReader(File.Open(EntropyPath, FileMode.Open)))
            {
                entropy = BinReader.ReadBytes(16);
            }
            long length = new FileInfo(filePath).Length;

            data = SecureDataProtector.UnprotectDataFromFile(entropy, filePath, length);
            Decrypted_Data_Box.Text = Encoding.UTF8.GetString(data);
        }