zoukankan      html  css  js  c++  java
  • 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 js 身份证号码 检测 规则 18位 15位 简单飞扬
    司马法 简单飞扬
    实现身份证的15位转18位 简单飞扬
    JAVA验证身份证号码 简单飞扬
    页面验证的类型 简单飞扬
    模拟MSN和QQ的上线提示效果 区别IE和FF浏览器 简单飞扬
    孙子兵法 简单飞扬
    吴子 简单飞扬
    C# WPF MVVM 实战 2.1
  • 原文地址:https://www.cnblogs.com/xzhblogs/p/5799057.html
Copyright © 2011-2022 走看看