PBE с MD5 и DES в C # - PullRequest
       26

PBE с MD5 и DES в C #

0 голосов
/ 20 марта 2012

Как мне указать ввод и вывод текста с помощью этого кода? Мне нужно открыть файл и прочитать его содержимое (что я знаю, как это сделать), а затем расшифровать его с помощью этого кода.

    public string DecryptUsernamePassword(string cipherText)
    {
        if (string.IsNullOrEmpty(cipherText))
        {
            return cipherText;
        }

        byte[] salt = new byte[]
        {
            (byte)0xc7,
            (byte)0x73,
            (byte)0x21,
            (byte)0x8c,
            (byte)0x7e,
            (byte)0xc8,
            (byte)0xee,
            (byte)0x99
        };

        PKCSKeyGenerator crypto = new PKCSKeyGenerator("PASSWORD HERE", salt, 20, 1);

        ICryptoTransform cryptoTransform = crypto.Decryptor;
        byte[] cipherBytes = System.Convert.FromBase64String(cipherText);
        byte[] clearBytes = cryptoTransform.TransformFinalBlock(cipherBytes, 0, cipherBytes.Length);
        return Encoding.UTF8.GetString(clearBytes);
    }

cipherText - это зашифрованный текст, а clearBytes - это незашифрованные байты, но мне нужно использовать textBox с формами C # для ввода и вывода.

Вот как это должно работать: textBox1.Text (вход) -> байты -> ^ выше ^ строка -> байты -> textBox2.Text (выход) Все работает tbh, пока мой ввод зашифрованный текст и мой вывод расшифрованного текста.

1 Ответ

0 голосов
/ 20 марта 2012

На основании ваших комментариев, если я все еще правильно понимаю вопрос. Сделайте это в своем собственном классе:

 public class UsernameDecryptor
 {
      public string Decrypt(string cipherText)
      {
           if (string.IsNullOrEmpty(cipherText))
                return cipherText;


           byte[] salt = new byte[]
           {
                (byte)0xc7,
                (byte)0x73,
                 (byte)0x21,
                (byte)0x8c,
                (byte)0x7e,
                (byte)0xc8,
                (byte)0xee,
                (byte)0x99
            };

            PKCSKeyGenerator crypto = new PKCSKeyGenerator("PASSWORD HERE", salt, 20, 1);

            ICryptoTransform cryptoTransform = crypto.Decryptor;
            byte[] cipherBytes = System.Convert.FromBase64String(cipherText);
            byte[] clearBytes = cryptoTransform.TransformFinalBlock(cipherBytes, 0, cipherBytes.Length);

            return Encoding.UTF8.GetString(clearBytes);
      }
 }

Затем внутри вашего обработчика кнопок:

private void button1_Click (object sender, System.EventArgs e)
{
     UsernameDecryptor decryptor = new UsernameDecryptor();

     string result = decryptor.Decrypt(inputTextBox.Text);

     outputTextBox.Text = result;
}
...