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

  • 相关阅读:
    安装触摸板驱动导致系统无法开机
    TensorBoard的使用
    TensorFlow 编程基础
    在Anaconda3下安装(CPU版)TensorFlow(清华镜像源)
    C 程序
    CodeBlocks 断点调试
    数字图像处理之复原处理
    数字图像处理之频域图像增强
    数字图像处理之傅里叶变换
    算法导论中的四种基本排序
  • 原文地址:https://www.cnblogs.com/xzhblogs/p/5799057.html
Copyright © 2020-2023  润新知