Esempio n. 1
0
        private void buttonAsym_Click(object sender, EventArgs e)
        {
            if (comboBoxAsym.SelectedIndex < 0)                                             //walidacja textBox
            {
                MessageBox.Show("Wybierz wartość dla pola comboBox 'Co chcesz zrobić?'");
            }
            else if (string.IsNullOrWhiteSpace(textBoxInputAsym.Text))                      //walidacja comboBox
            {
                MessageBox.Show("Wypełnij pole 'Wprowadź tekst'");
            }
            else
            {
                var cryptoServiceProvider = new RSACryptoServiceProvider(2048);             //długość klucza
                var privateKey            = cryptoServiceProvider.ExportParameters(true);   //klucz prywatny
                var publicKey             = cryptoServiceProvider.ExportParameters(false);  //klucz publiczny

                string publicKeyString  = Asymmetric.GetKeyString(publicKey);               //przypisanie klucza publicznego do stringa
                string privateKeyString = Asymmetric.GetKeyString(privateKey);              //przypisanie klucza prywatnego do stringa

                textBoxPublicKey.Text  = publicKeyString;                                   //wypisanie klucza publicznego w textBoxie
                textBoxPrivateKey.Text = privateKeyString;                                  //wypisanie klucza prywatnego w textBoxie
                string inputText     = textBoxInputAsym.Text;                               //przypisanie zawartości textBoxa do zmiennej
                string encryptedText = Asymmetric.Encrypt(inputText, publicKeyString);      //funkcja szyfrowania
                string decryptedText = Asymmetric.Decrypt(encryptedText, privateKeyString); //funkcja deszyfrowania

                if (comboBoxAsym.SelectedIndex == 0)
                {
                    textBoxResultAsym.Text = encryptedText;
                }
                if (comboBoxAsym.SelectedIndex == 1)
                {
                    textBoxResultAsym.Text = decryptedText;
                }
            }
        }
Esempio n. 2
0
        private void buttonAsymFile_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
            {
                textBoxInputAsym.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
                var    cryptoServiceProvider = new RSACryptoServiceProvider(2048);            //długość klucza
                var    privateKey            = cryptoServiceProvider.ExportParameters(true);  //klucz prywatny
                var    publicKey             = cryptoServiceProvider.ExportParameters(false); //klucz publiczny

                string publicKeyString  = Asymmetric.GetKeyString(publicKey);                 //przypisanie klucza publicznego do stringa
                string privateKeyString = Asymmetric.GetKeyString(privateKey);                //przypisanie klucza prywatnego do stringa

                textBoxPublicKey.Text  = publicKeyString;                                     //wypisanie klucza publicznego w textBoxie
                textBoxPrivateKey.Text = privateKeyString;                                    //wypisanie klucza prywatnego w textBoxie
                string encryptedText = Asymmetric.Encrypt(toEncrypt, publicKeyString);
                textBoxResultAsym.Text = encryptedText;                                       //wypisywanie zaszyfrowanego wyniku w textBoxie
            }
        }
 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;
 }