复制代码 代码如下:
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
private void btencoding_Click(object sender, EventArgs e)
{
if (mbox.Text == "")
{
MessageBox.Show("加密文字信息不能为空!");
return;
}
if (publickey.Text == "")
{
MessageBox.Show("请生成公钥!");
return;
}
try
{
string pubKey = publickey.Text;
byte[] mw = ASCIIEncoding.ASCII.GetBytes(mbox.Text);
RSACryptoServiceProvider crypt = new RSACryptoServiceProvider();
crypt.FromXmlString(pubKey);
mw = crypt.Encrypt(mw, false);
string encryttext = Convert.ToBase64String(mw);//加密后的结果怎样处理解决显示乱码问题
cbox.Text = encryttext;
}
catch
{
MessageBox.Show("请检查是否打开公匙或者公匙是否损坏!");
}
}
private void btdecoding_Click(object sender, EventArgs e)
{
if (cbox.Text == "")
{
MessageBox.Show("请生成密钥!");
return;
}
try
{
RSACryptoServiceProvider crypt = new RSACryptoServiceProvider();
byte[] bytes = Convert.FromBase64String(cbox.Text);//从密文框中取出的字符串正确处理才能解密
string prtKey = privatekey.Text;
crypt.FromXmlString(prtKey);
byte[] decryptbyte = crypt.Decrypt(bytes, false);
string decrypttext = Encoding.Default.GetString(decryptbyte);
mbox.Text = decrypttext;
}
catch (CryptographicException ex)
{
//MessageBox.Show("请检查是否打开私匙或者私匙是否损坏!");
MessageBox.Show(ex.ToString());
} if (cbox.Text == "")
{
MessageBox.Show("请生成密钥!");
return;
}
}
【c# rsa注册实现加密文字】相关文章: