public void Encrypt(string path) { string keyPath = KeyStorage.Instance().GetKeyPath(); Console.WriteLine($"Encrypting the file using {keyPath} key"); using (var rsa = new RSACryptoServiceProvider()) { try { rsa.FromXmlString(File.ReadAllText(keyPath)); byte[] bytesToEncrypt = File.ReadAllBytes(path); byte[] encrypted = rsa.Encrypt(bytesToEncrypt, false); var pureFileName = Path.GetFileName(path).Split('.')[0]; File.WriteAllBytes($"{pureFileName}_ENCRYPTED.txt", encrypted); Process.Start("explorer.exe", Directory.GetCurrentDirectory()); } catch (Exception exception) { MessageBox.Show("Nieprawidłowy klucz!"); } } }
public void Decrypt() { string keyPath = KeyStorage.Instance().GetKeyPath(); Console.WriteLine($"Decrypting the file using {keyPath} key"); using (var rsa = new RSACryptoServiceProvider()) { rsa.FromXmlString(File.ReadAllText(keyPath)); byte[] bytesToDecrypt = File.ReadAllBytes(filePath); try { byte[] decrypted = rsa.Decrypt(bytesToDecrypt, false); var decryptedString = Encoding.UTF8.GetString(decrypted); var pureFileName = Path.GetFileName(filePath).Split('.')[0].Split('_')[0]; var extension = Path.GetFileName(filePath).Split('.')[1]; File.WriteAllText($"{pureFileName}_DECRYPTED.{extension}", decryptedString); Process.Start("explorer.exe", Directory.GetCurrentDirectory()); } catch (Exception exception) { MessageBox.Show("Nieprawidłowy klucz!"); } } }
private void RefreshAvailableKeys() { KeysListBox.Items.Clear(); foreach (var keyFileName in KeyStorage.GetKeysInDirectory()) { var item = new ListBoxItem(); var displayedName = ""; var pureName = keyFileName.Split('.')[0].Split('_')[0]; var fileSufix = keyFileName.Split('.')[0].Split('_')[1]; displayedName += pureName; if (fileSufix == "PRIVATE") { displayedName = "🔒 " + displayedName; } else { displayedName = " " + displayedName; } item.Selected += KeyStorage.Instance().ChooseKey; item.Selected += ChangeButtonsState; item.MouseDoubleClick += DisplayKeyInfo; item.Content = displayedName; item.Tag = keyFileName; KeysListBox.Items.Add(item); } }
private void CopyButton_OnClick(object sender, RoutedEventArgs e) { var keyPath = KeyStorage.Instance().GetKeyPath(); Clipboard.SetText(File.ReadAllText(keyPath)); Close(); }
public static KeyStorage Instance() { if (_instance == null) { _instance = new KeyStorage(); } return(_instance); }
private void InitializeData() { var keyPath = KeyStorage.Instance().GetKeyPath(); KeyNameText.Text = "Nazwa: " + Path.GetFileName(keyPath); KeyCreationDateText.Text = "Stworzono: " + new FileInfo(keyPath).CreationTime.ToString(); var rsa = new RSACryptoServiceProvider(); rsa.FromXmlString(File.ReadAllText(keyPath)); KeySizeText.Text = "Rozmiar: " + rsa.KeySize.ToString(); }
private void GenerateKeysButton_OnClick(object sender, RoutedEventArgs e) { Console.WriteLine("Generating asymetric keys..."); var selected = (ComboBoxItem)AvailableSizeOptions.SelectedItem; var rsa = new RSACryptoServiceProvider((int)selected.Tag); var publicKey = rsa.ToXmlString(false); var privateKey = rsa.ToXmlString(true); var fileName = $"{KeyName.Text}_PRIVATE"; KeyStorage.SaveKeyToFile(fileName, privateKey); fileName = $"{KeyName.Text}_PUBLIC"; KeyStorage.SaveKeyToFile(fileName, publicKey); refreshAvailableKeys.Invoke(); Close(); }