📄 form1.cs
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Security.Cryptography;
using System.Text;
using System.IO;
namespace RSA
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
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());
}
}
private void button1_Click(object sender, EventArgs e)
{
RSACryptoServiceProvider rs = new RSACryptoServiceProvider();
publicKey.Text = rs.ToXmlString(false);
privateKey.Text = rs.ToXmlString(true);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -