如何使用此代码指定文本输入和输出? 我需要打开一个文件并读取其内容(我知道该怎么做),然后使用此代码对其进行解密。
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是未加密的字节,但我需要使用带有C#表单的textBox进行输入和输出。
这是它需要工作的方式:textBox1.Text(输入) - > bytes - > ^上面^字符串 - > bytes - > textBox2.Text(输出)只要我的输入是加密文本而我的输出是解密文本,任何东西都可以工作。
答案 0 :(得分:0)
根据您的意见,假设我仍然正确理解这个问题。把它变成自己的班级:
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;
}