码迷,mamicode.com
首页 > Windows程序 > 详细

C# RSA

时间:2016-08-23 14:43:09      阅读:407      评论:0      收藏:0      [点我收藏+]

标签:

RSA加解密

using System.Security.Cryptography;

技术分享
    private void button18_Click(object sender, EventArgs e)//RSA加密
        {
            bool RSA_Mode = radioButton13.Checked;                      //填充方式true 为oaep false为pkcs#1 1.5
            string RSA_Message = richTextBox15.Text;
            richTextBox12.Text = "";
            byte[] source = Encoding.Default.GetBytes(RSA_Message);      //明文转换为byte
            byte[] ciphertext;                                           //密文byte数组
            string publickey = richTextBox17.Text;                       //string密钥
            RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
            try
            {
                rsa.FromXmlString(publickey);                                //导入string密钥  
                ciphertext = rsa.Encrypt(source , RSA_Mode);                       //加密
                StringBuilder sb = new StringBuilder();
                foreach (byte b in ciphertext)
                {
                    sb.AppendFormat("{0:X2}", b);
                }
                richTextBox12.Text = sb.ToString();

               

            }
            catch { MessageBox.Show("加密失败,请检查密钥"); }
        }

    }
RSA加密
技术分享
private void button17_Click(object sender, EventArgs e)//RSA解密
        {
            bool RSA_Mode = radioButton13.Checked;
            string RSA_Ciphertext = richTextBox12.Text;
            richTextBox15.Text = "";
            string privatekey = richTextBox16.Text;
            byte[] ciphertext = new byte[RSA_Ciphertext.Length / 2];
            try
            {
                for (int x = 0; x < RSA_Ciphertext.Length / 2; x++)
                {
                    int i = (Convert.ToInt32(RSA_Ciphertext.Substring(x * 2, 2), 16));
                    ciphertext[x] = (byte)i;
                }
            }
            catch { MessageBox.Show("密文不正确!"); }
            byte[] source;    //原文byte数组
            RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
            try
            {
                rsa.FromXmlString(privatekey);                          //设置私钥
                source = rsa.Decrypt(ciphertext, RSA_Mode);                    //解密,得到byte数组
                richTextBox15.Text = Encoding.Default.GetString(source);    //返回结果
            }
            catch { MessageBox.Show("密钥不正确"); }
            
        }
RSA解密

c#中RSA密钥不是数字,是XML格式的密钥

是用特殊的BASE64加密的

http://www.cnblogs.com/midea0978/archive/2007/05/22/755826.html

C#默认公钥是65537也就是AQAB,不知道怎么改掉

C# RSA

标签:

原文地址:http://www.cnblogs.com/xzhblogs/p/5799057.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!