private void buttonSym_Click(object sender, EventArgs e) { if (comboBoxSym.SelectedIndex < 0) //walidacja comboBox { MessageBox.Show("Wybierz wartość dla pola comboBox 'Co chcesz zrobić?'"); } else if (string.IsNullOrWhiteSpace(textBoxInputSym.Text)) //walidacja textBox { MessageBox.Show("Wypełnij pole 'Wprowadź tekst'"); } else { string textSym = textBoxInputSym.Text; //wczytanie wartości z textBoxa string password = "******"; //stworzenie hasła byte[] Salt = Symmetric.GenerateSalt(); //wygenerowanie soli byte[] IV = Symmetric.GenerateIV(); //wygenerowanie IV byte[] key = Symmetric.CreateKey(password, Salt); //stworzenie klucza string Encrypted = Symmetric.EncryptString(textSym, key, IV); //funkcja szyfrowania string Decrypted = Symmetric.DecryptString(Encrypted, key, IV); //funkcja deszyfrowania if (comboBoxSym.SelectedIndex == 1) { textBoxResultSym.Text = Decrypted; } //odszyfrowywanie if (comboBoxSym.SelectedIndex == 0) { textBoxResultSym.Text = Encrypted; } //zaszyfrowywanie } }
public string decrypt(string code) { try { var sym = new Symmetric(_p); sym.Key.Text = _k; var d = new Encryption.Data(); d.Hex = code; return sym.Decrypt(d).Text; } catch (Exception) { return string.Empty; } }
private void buttonSymFile_Click(object sender, EventArgs e) { OpenFileDialog dialog = new OpenFileDialog(); dialog.Filter = "Pliki textowe (txt)|*.txt"; //połączenie z plikiem if (dialog.ShowDialog() == DialogResult.OK) //sprawdzenie połączenia { textBoxInputSym.Text = File.ReadAllText(dialog.FileName); //przypisanie wartości z pliku do textBoxa "Wprowadź tekst" string toEncrypt = File.ReadAllText(dialog.FileName); //przypisanie wartości z pliku do zmiennej string password = "******"; //stworznie hasła byte[] Salt = Symmetric.GenerateSalt(); //wygenerowanie soli byte[] IV = Symmetric.GenerateIV(); //wygenerowanie IV byte[] key = Symmetric.CreateKey(password, Salt); //stworzenie klucza string Encrypted = Symmetric.EncryptString(toEncrypt, key, IV); //zaszyfrowanie treści textBoxResultSym.Text = Encrypted; //przypisanie wyniku } }
public string EncryptData(string data, EncryptType encryptType) { string encryptedString = ""; if (data == "null") { encryptedString = "null"; } if (data != null && data != string.Empty && data.ToLower() != "null") { switch (encryptType) { case EncryptType.Symmetric: Symmetric.Provider p = Symmetric.Provider.TripleDES; Symmetric sym = new Symmetric(p); //sym.Key.Text = symmetricKey; Data encryptedData = null; encryptedData = sym.Encrypt(new Data(data)); encryptedString = encryptedData.Base64; break; case EncryptType.Asymmetric: Asymmetric asym = new Asymmetric(); Asymmetric.PublicKey pubkey = new Asymmetric.PublicKey(); Asymmetric.PrivateKey privkey = new Asymmetric.PrivateKey(); asym.GenerateNewKeyset(ref pubkey, ref privkey); Data EncryptedData = null; EncryptedData = asym.Encrypt(new Data(data), pubkey); encryptedString = EncryptedData.Base64; break; case EncryptType.Hash: Hash.Provider phash = Hash.Provider.SHA1; Hash hash = new Hash(phash); EncryptedData = hash.Calculate(new Data(data)); encryptedString = EncryptedData.Base64; break; } } return encryptedString; }
public string DecryptData(string encryptedString, EncryptType encryptType) { string data = ""; if (encryptedString != null && encryptedString != string.Empty && data.ToLower() != "null") { switch (encryptType) { case EncryptType.Symmetric: Symmetric.Provider p = Symmetric.Provider.TripleDES; Data symdecryptedData = null; p = Symmetric.Provider.TripleDES; Symmetric sym2 = new Symmetric(p); //sym2.Key.Text = symmetricKey; System.Text.ASCIIEncoding symEncoding = new System.Text.ASCIIEncoding(); Byte[] symBytes = symEncoding.GetBytes(encryptedString); Data symencryptedData = null; symencryptedData = new Data(); symencryptedData.Base64 = encryptedString; symdecryptedData = sym2.Decrypt(symencryptedData); data = symdecryptedData.Text; break; case EncryptType.Asymmetric: System.Text.ASCIIEncoding asymEncoding = new System.Text.ASCIIEncoding(); Data asymdecryptedData = null; Asymmetric asym2 = new Asymmetric(); Byte[] asymBytes = asymEncoding.GetBytes(encryptedString); Data asymencryptedData = null; asymencryptedData = new Data(); asymencryptedData.Base64 = encryptedString; asymdecryptedData = asym2.Decrypt(asymencryptedData); data = asymdecryptedData.Text; break; case EncryptType.Hash: //hashing is one way break; } } return data; }
public string encrypt(string data) { var sym = new Symmetric(_p); sym.Key.Text = _k; return sym.Encrypt(new Encryption.Data(data)).ToHex(); }
public EncryptHelper(Symmetric.Provider provider, string key) { _p = provider; _k = key; }