c# RSA加密
获取密钥并存储至密钥容器
1 /// <summary> 2 /// 生成密钥 3 /// </summary> 4 public static RSAKey GenerateRSAKey() 5 { 6 RSAKey RSAKEY = new RSAKey(); 7 //密钥存储容器 8 CspParameters csp = new CspParameters(); 9 csp.KeyContainerName = "loginInfo";//容器名称 10 //如果密钥容器不存在,则会自动创建,并将 rsa 产生的密钥存入其中;如果已经存在,则会读取其中的密钥给 rsa,未加容器表示创建临时容器存储密钥 11 //RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();//表示创建临时容器存储密钥 12 RSACryptoServiceProvider RSA = new RSACryptoServiceProvider(csp); 13 RSAKEY.PrivateKey = RSA.ToXmlString(true); //生成私钥 14 RSAKEY.PublicKey = RSA.ToXmlString(false); //生成公钥 15 return RSAKEY; 16 }
清除密钥容器(获取密钥后删除密钥容器)
1 /// <summary> 2 /// 生成密钥 3 /// </summary> 4 public static RSAKey GenerateRSAKey() 5 { 6 RSAKey RSAKEY = new RSAKey(); 7 //密钥存储容器 8 CspParameters csp = new CspParameters(); 9 csp.KeyContainerName = "loginInfo";//容器名称 10 //如果密钥容器不存在,则会自动创建,并将 rsa 产生的密钥存入其中;如果已经存在,则会读取其中的密钥给 rsa,未加容器表示创建临时容器存储密钥 11 //RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();//表示创建临时容器存储密钥 12 RSACryptoServiceProvider RSA = new RSACryptoServiceProvider(csp); 13 RSAKEY.PrivateKey = RSA.ToXmlString(true); //生成私钥 14 RSAKEY.PublicKey = RSA.ToXmlString(false); //生成公钥 15 //清除密钥容器 16 RSA.PersistKeyInCsp = false; 17 RSA.Clear(); 18 return RSAKEY; 19 }
清除容器
RSA.PersistKeyInCsp = false;
RSA.Clear();
公钥加密私钥解密方式
信息加密(非容器获取密钥)
1 /// <summary> 2 /// RSA加密 3 /// </summary> 4 /// <param name="publickey">公钥</param> 5 /// <param name="content">加密内容</param> 6 /// <returns></returns> 7 public static string RSAEncrypt(string publickey, string content) 8 { 9 RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(); 10 byte[] cipherbytes; 11 rsa.FromXmlString(publickey); 12 cipherbytes = rsa.Encrypt(Encoding.UTF8.GetBytes(content), false); 13 return Convert.ToBase64String(cipherbytes); 14 }
信息加密(使用容器获取密钥)
1 /// <summary> 2 /// RSA加密 3 /// </summary> 4 /// <param name="publickey">公钥</param> 5 /// <param name="content">加密内容</param> 6 /// <returns></returns> 7 public static string RSAEncrypt(string content) 8 { 9 CspParameters csp = new CspParameters(); 10 csp.KeyContainerName = "loginInfo"; 11 RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(csp); 12 byte[] cipherbytes; 13 cipherbytes = rsa.Encrypt(Encoding.UTF8.GetBytes(content), false); 14 return Convert.ToBase64String(cipherbytes); 15 }
信息解密(非容器获取密钥)
1 /// <summary> 2 /// RSA解密 3 /// </summary> 4 /// <param name="privatekey">私钥</param> 5 /// <param name="content">解密内容</param> 6 /// <returns></returns> 7 public static string RSADecrypt( string content) 8 { 9 RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(); 10 byte[] cipherbytes; 11 rsa.FromXmlString(privatekey); 12 cipherbytes = rsa.Decrypt(Convert.FromBase64String(content), false); 13 14 return Encoding.UTF8.GetString(cipherbytes); 15 }
信息解密(容器获取密钥)
1 /// <summary> 2 /// RSA解密 3 /// </summary> 4 /// <param name="privatekey">私钥</param> 5 /// <param name="content">解密内容</param> 6 /// <returns></returns> 7 public static string RSADecrypt(string content) 8 { 9 CspParameters csp = new CspParameters(); 10 csp.KeyContainerName = "loginInfo"; 11 RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(csp); 12 byte[] cipherbytes; 13 cipherbytes = rsa.Decrypt(Convert.FromBase64String(content), false); 14 15 return Encoding.UTF8.GetString(cipherbytes); 16 }