1、RSA简介
RSA公开密钥密码体制是一种使用不同的加密密钥与解密密钥,“由已知加密密钥推导出解密密钥在计算上是不可行的”密码体制
在公开密钥密码体制中,加密密钥(即公开密钥)PK是公开信息,而解密密钥(即秘密密钥)SK是需要保密的。加密算法E和解密算法D也都是公开的。虽然解密密钥SK是由公开密钥PK决定的,但却不能根据PK计算出SK。RSA应用广泛,解密密钥称为公钥,私钥加密后只能用公钥解密,,当然也可以用公钥加密,用私钥解密。密钥尺寸大,加解密速度慢,一般用来加密少量数据,比如DES加密时使用的密钥。
2、RSA加解密的实现代码
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Security.Cryptography; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication { class Program { static void Main(string[] args) { KeyValuePair<string, string> encryptDecrypt = RsaEncrypt.GetKeyPair(); string rsaEn1 = RsaEncrypt.Encrypt("cjavapy", encryptDecrypt.Key);//key是加密的 Console.WriteLine("rsaEn1 = " + rsaEn1); string rsaDe1 = RsaEncrypt.Decrypt(rsaEn1, encryptDecrypt.Value);//value 解密的 不能反过来用的 Console.WriteLine("rsaDe1 = " + rsaDe1); Console.ReadKey(); } } /// <summary> /// RSA /// 可逆非对称加密 /// 非对称加密算法的优点是密钥管理很方便,缺点是速度慢。 /// </summary> public class RsaEncrypt { /// <summary> /// 获取加密/解密对 /// 给你一个,是无法推算出另外一个的 /// /// Encrypt Decrypt /// </summary> /// <returns>Encrypt Decrypt</returns> public static KeyValuePair<string, string> GetKeyPair() { RSACryptoServiceProvider RSA = new RSACryptoServiceProvider(); string publicKey = RSA.ToXmlString(false); string privateKey = RSA.ToXmlString(true); return new KeyValuePair<string, string>(publicKey, privateKey); } /// <summary> /// 加密:内容+加密key /// </summary> /// <param name="content"></param> /// <param name="encryptKey">加密key</param> /// <returns></returns> public static string Encrypt(string content, string encryptKey) { RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(); rsa.FromXmlString(encryptKey); UnicodeEncoding ByteConverter = new UnicodeEncoding(); byte[] DataToEncrypt = ByteConverter.GetBytes(content); byte[] resultBytes = rsa.Encrypt(DataToEncrypt, false); return Convert.ToBase64String(resultBytes); } /// <summary> /// 解密 内容+解密key /// </summary> /// <param name="content"></param> /// <param name="decryptKey">解密key</param> /// <returns></returns> public static string Decrypt(string content, string decryptKey) { byte[] dataToDecrypt = Convert.FromBase64String(content); RSACryptoServiceProvider RSA = new RSACryptoServiceProvider(); RSA.FromXmlString(decryptKey); byte[] resultBytes = RSA.Decrypt(dataToDecrypt, false); UnicodeEncoding ByteConverter = new UnicodeEncoding(); return ByteConverter.GetString(resultBytes); } /// <summary> /// 可以合并在一起的,,每次产生一组新的密钥 /// </summary> /// <param name="content"></param> /// <param name="encryptKey">加密key</param> /// <param name="decryptKey">解密key</param> /// <returns>加密后结果</returns> private static string Encrypt(string content, out string publicKey, out string privateKey) { RSACryptoServiceProvider rsaProvider = new RSACryptoServiceProvider(); publicKey = rsaProvider.ToXmlString(false); privateKey = rsaProvider.ToXmlString(true); UnicodeEncoding ByteConverter = new UnicodeEncoding(); byte[] DataToEncrypt = ByteConverter.GetBytes(content); byte[] resultBytes = rsaProvider.Encrypt(DataToEncrypt, false); return Convert.ToBase64String(resultBytes); } } }