【加密】RSA加密之实现
private void btn_RSA_Click(object sender, EventArgs e) { //第一种方法调用 this.textBox1.Text = RSAEncrypt("", "key");//密文 this.textBox2.Text = RSADecrypt("", this.textBox1.Text); //第二种方法调用 //this.textBox1.Text = RSAEncrypt2("key", "MyKey"); //this.textBox2.Text = RSADecrypt2(this.textBox1.Text, "MyKey"); //第三种方法调用 //string publickey = ""; //string privatekey = ""; //this.textBox1.Text = RSA_Encrypt("key", out publickey, out privatekey); //this.textBox2.Text = RSA_Decrypt(this.textBox1.Text, privatekey); }
/// <summary> /// RSA加密 /// </summary> /// <param name="publickey"></param> /// <param name="content"></param> /// <returns></returns> public static string RSAEncrypt(string publickey, string content) { publickey = @"<RSAKeyValue><Modulus>5m9m14XH3oqLJ8bNGw9e4rGpXpcktv9MSkHSVFVMjHbfv+SJ5v0ubqQxa5YjLN4vc49z7SVju8s0X4gZ6AzZTn06jzWOgyPRV54Q4I0DCYadWW4Ze3e+BOtwgVU1Og3qHKn8vygoj40J6U85Z/PTJu3hN1m75Zr195ju7g9v4Hk=</Modulus><Exponent>AQAB</Exponent></RSAKeyValue>"; RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(); rsa.FromXmlString(publickey); byte[] cipherbytes = rsa.Encrypt(Encoding.UTF8.GetBytes(content), false); return Convert.ToBase64String(cipherbytes); } /// <summary> /// RSA解密 /// </summary> /// <param name="privatekey"></param> /// <param name="content"></param> /// <returns></returns> public static string RSADecrypt(string privatekey, string content) { privatekey = @"<RSAKeyValue><Modulus>5m9m14XH3oqLJ8bNGw9e4rGpXpcktv9MSkHSVFVMjHbfv+SJ5v0ubqQxa5YjLN4vc49z7SVju8s0X4gZ6AzZTn06jzWOgyPRV54Q4I0DCYadWW4Ze3e+BOtwgVU1Og3qHKn8vygoj40J6U85Z/PTJu3hN1m75Zr195ju7g9v4Hk=</Modulus><Exponent>AQAB</Exponent><P>/hf2dnK7rNfl3lbqghWcpFdu778hUpIEBixCDL5WiBtpkZdpSw90aERmHJYaW2RGvGRi6zSftLh00KHsPcNUMw==</P><Q>6Cn/jOLrPapDTEp1Fkq+uz++1Do0eeX7HYqi9rY29CqShzCeI7LEYOoSwYuAJ3xA/DuCdQENPSoJ9KFbO4Wsow==</Q><DP>ga1rHIJro8e/yhxjrKYo/nqc5ICQGhrpMNlPkD9n3CjZVPOISkWF7FzUHEzDANeJfkZhcZa21z24aG3rKo5Qnw==</DP><DQ>MNGsCB8rYlMsRZ2ek2pyQwO7h/sZT8y5ilO9wu08Dwnot/7UMiOEQfDWstY3w5XQQHnvC9WFyCfP4h4QBissyw==</DQ><InverseQ>EG02S7SADhH1EVT9DD0Z62Y0uY7gIYvxX/uq+IzKSCwB8M2G7Qv9xgZQaQlLpCaeKbux3Y59hHM+KpamGL19Kg==</InverseQ><D>vmaYHEbPAgOJvaEXQl+t8DQKFT1fudEysTy31LTyXjGu6XiltXXHUuZaa2IPyHgBz0Nd7znwsW/S44iql0Fen1kzKioEL3svANui63O3o5xdDeExVM6zOf1wUUh/oldovPweChyoAdMtUzgvCbJk1sYDJf++Nr0FeNW1RB1XG30=</D></RSAKeyValue>"; RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(); rsa.FromXmlString(privatekey); byte[] cipherbytes = rsa.Decrypt(Convert.FromBase64String(content), false); return Encoding.UTF8.GetString(cipherbytes); }
/// <summary> /// 加密 /// </summary> /// <param name="s"></param> /// <param name="key"></param> /// <returns></returns> public string RSAEncrypt2(string s, string key) { if (string.IsNullOrEmpty(s)) throw new ArgumentException("An empty string value cannot be encrypted."); if (string.IsNullOrEmpty(key)) throw new ArgumentException("Cannot encrypt using an empty key. Please supply an encryption key."); CspParameters cspp = new CspParameters(); cspp.KeyContainerName = key; RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(cspp); rsa.PersistKeyInCsp = true; byte[] bytes = rsa.Encrypt(System.Text.UTF8Encoding.UTF8.GetBytes(s), true); return BitConverter.ToString(bytes); } /// <summary> /// 解密 /// </summary> /// <param name="s"></param> /// <param name="key"></param> /// <returns></returns> public string RSADecrypt2(string s, string key) { if (string.IsNullOrEmpty(s)) throw new ArgumentException("An empty string value cannot be encrypted."); if (string.IsNullOrEmpty(key)) throw new ArgumentException("Cannot decrypt using an empty key. Please supply a decryption key."); CspParameters cspp = new CspParameters(); cspp.KeyContainerName = key; RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(cspp); rsa.PersistKeyInCsp = true; string[] decryptArray = s.Split(new string[] { "-" }, StringSplitOptions.None); byte[] decryptByteArray = Array.ConvertAll<string, byte>(decryptArray, (a => Convert.ToByte(byte.Parse(a, System.Globalization.NumberStyles.HexNumber)))); byte[] bytes = rsa.Decrypt(decryptByteArray, true); return System.Text.UTF8Encoding.UTF8.GetString(bytes); }
UnicodeEncoding ByteConverter = new UnicodeEncoding(); //RSA加密,随机生成公私钥对并作为输出参返回 public string RSA_Encrypt(string content,out string publickey,out string privatekey) { publickey = ""; privatekey = ""; try { //方法1 //RSACryptoServiceProvider RSA = new RSACryptoServiceProvider(); //publickey = Convert.ToBase64String(RSA.ExportCspBlob(false)); //privatekey = Convert.ToBase64String(RSA.ExportCspBlob(true)); //方法2 //RSACryptoServiceProvider RSA = new RSACryptoServiceProvider(); //publickey = RSA.ToXmlString(false); //privatekey = RSA.ToXmlString(true); //方法3 CspParameters cspp = new CspParameters(); cspp.KeyContainerName = "mykey"; RSACryptoServiceProvider RSA = new RSACryptoServiceProvider(cspp); byte[] bytes_Cypher_Text = RSA.Encrypt(ByteConverter.GetBytes(content),false); return Convert.ToBase64String(bytes_Cypher_Text); } catch (CryptographicException e) { Console.WriteLine(e.Message); return null; } } //RSA解密 public string RSA_Decrypt(string str_Cypher_Text, string privatekey) { try { //方法1 //RSACryptoServiceProvider RSA = new RSACryptoServiceProvider(); //byte[] bytes_Public_Key = Convert.FromBase64String(privatekey); //RSA.ImportCspBlob(bytes_Public_Key); //方法2 //RSACryptoServiceProvider RSA = new RSACryptoServiceProvider(); //RSA.FromXmlString(privatekey); //方法3 CspParameters cspp = new CspParameters(); cspp.KeyContainerName = "mykey"; RSACryptoServiceProvider RSA = new RSACryptoServiceProvider(cspp); byte[] bytes_Content = RSA.Decrypt(Convert.FromBase64String(str_Cypher_Text), false); return ByteConverter.GetString(bytes_Content); } catch (CryptographicException e) { Console.WriteLine(e.ToString()); return null; } }