• C# RSA


    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,不知道怎么改掉

  • 相关阅读:
    JavaScript 创建和浅析自定义对象
    JavaScript Object对象
    JavaScript String对象
    JavaScript Math和Number对象
    using kafkacat reset kafka offset
    windows 10 enterprize LTSC
    avro-1.8.1 serialize BigDecimal and Short error fix.
    Ubuntu Navicat for MySQL安装以及破解方案
    Kafka Connect Architecture
    python 读写配置文件
  • 原文地址:https://www.cnblogs.com/xzhblogs/p/5799057.html
Copyright © 2020-2023  润新知