zoukankan      html  css  js  c++  java
  • c#公钥加密私钥解密和验证

    public partial class Form1 : Form {
            private string Estring;
            private string priKey;
            private string pubKey;
            public Form1()
            {
                InitializeComponent();
                RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
                priKey = rsa.ToXmlString(true);
                pubKey = rsa.ToXmlString(false);
            }
    
            public static string RSAEncrypt(string publickey,string content) {
                RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
                byte[] cipherbytes;
                rsa.FromXmlString(publickey);
                cipherbytes = rsa.Encrypt(Encoding.UTF8.GetBytes(content), false);
                return Convert.ToBase64String(cipherbytes);
    
            }
    
            public static string RSADecrypt(string privateKey, string content)
            {
                RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
                byte[] cipherbytes;
                rsa.FromXmlString(privateKey);
                cipherbytes = rsa.Decrypt(Convert.FromBase64String(content), false);
                return Encoding.UTF8.GetString(cipherbytes);
    
            }
    
            private void button1_Click(object sender, EventArgs e) {
                Estring = RSAEncrypt(pubKey, "Hi Bob");
    
            }
    
            private void button2_Click(object sender, EventArgs e) {
                string DString;
                DString = RSADecrypt(priKey, Estring);
            }
    
            private void button3_Click(object sender, EventArgs e)
            {
                byte[] messagebytes = Encoding.UTF8.GetBytes("luo罗");
                RSACryptoServiceProvider oRSA = new RSACryptoServiceProvider();
                string privatekey = oRSA.ToXmlString(true);
                string publickey = oRSA.ToXmlString(false);
    
                //私钥签名  
                RSACryptoServiceProvider oRSA3 = new RSACryptoServiceProvider();
                oRSA3.FromXmlString(privatekey);
                byte[] AOutput = oRSA3.SignData(messagebytes, "SHA1");
                //公钥验证  
                RSACryptoServiceProvider oRSA4 = new RSACryptoServiceProvider();
                oRSA4.FromXmlString(publickey);
                bool bVerify = oRSA4.VerifyData(messagebytes, "SHA1", AOutput);  
                
            }
        }
  • 相关阅读:
    selenium 详解
    验证码破解
    Jupyter Notebook 安装
    分布式
    selenium +scrapy 实现网易新闻
    scrapy 反扒措施_2
    Typora简单使用
    20155215 2016-2017-2 《Java程序设计》第4周学习总结
    20155215 2016-2017-2 《Java程序设计》第3周学习总结
    20155215 2006-2007-2 《Java程序设计》第2周学习总结
  • 原文地址:https://www.cnblogs.com/ycdx2001/p/3081659.html
Copyright © 2011-2022 走看看