非对称加密具有一组密钥对,一个是对外公开的,任何人都可以获取,称为公钥;
一个是自行保管并严格保密的,称为私钥。
规则如下:
由某人A的公钥加密消息,只能由A的私钥进行解密;
由A的私钥加密的消息,只能由A的公钥解密。
发送方,接收方都持有公钥/私钥对,因此一共会有四个密钥。非堆成加密的好处在于,私钥自己持有,公钥完全公开。
步骤:
1,接收方公布自己的公钥,任何人都可以获得。
2,发送方使用接收方的公钥对消息进行加密,然后发送。
3,接收方使用自己的私钥对消息进行解密。
缺点:
1,无法确定消息是由谁发送的。
/* //加密模式 */ namespace 非对称加密 { class Program { static void Main(string[] args) { string plainText = "hello world"; string publicKey = "<RSAKeyValue><Modulus>qA89WuhLCmvYHJqw+mfjRZ6Ep8xuicvRkVWkuufRHBAmLgXt2lHThSSBsZhFEECHqvwGFF+OybGj1Ki72A3h056tM6yRNppJFaJGGrnsjsWPPNa14E6f+ZBvC/MZRISLGNTDxhbHuvNSMx+hLU+Skz+B75RCMoYuEOuP8GxFEqs=</Modulus><Exponent>AQAB</Exponent></RSAKeyValue>"; string encryptedText = RSACryptoHelper.Encrypt(publicKey, plainText); //机密啦~~ Console.WriteLine(encryptedText); string privateKey = "<RSAKeyValue><Modulus>qA89WuhLCmvYHJqw+mfjRZ6Ep8xuicvRkVWkuufRHBAmLgXt2lHThSSBsZhFEECHqvwGFF+OybGj1Ki72A3h056tM6yRNppJFaJGGrnsjsWPPNa14E6f+ZBvC/MZRISLGNTDxhbHuvNSMx+hLU+Skz+B75RCMoYuEOuP8GxFEqs=</Modulus><Exponent>AQAB</Exponent><P>7VxYssHAiMZtnVzgK3h3U9llNZSa5MCK4/iLvATQ5h3+yHegT0t+q2Tv844QUxcBPvkkrF+uvSb043Nw65KoTw==</P><Q>tUG739ddGWVrsBxle1ZmuABVBzeUNUiCOsbcGP/LsmbScdnk46rIfjVZ3NGlfptbAv4I7MPatr8Je1O5GL485Q==</Q><DP>ZyFXnJuYhxUILXZcJCccWb88PVKLFlceQb0NIa1KAqIHwJxReAKKT/f0VfNk3mVBclYX/Bk6uA7EGktfRcub+w==</DP><DQ>rLubBiNgBo6/hFJbZ6GcPCec4EbYB7s02DygjXZfsYEJdhQ3a7taW+QN4kEsHK6CmiRrbu7qpJMDvzK3R1wr/Q==</DQ><InverseQ>a0q3ffhjSHdaZW0QrkqZNUNSQ+j5/ltPS9zaJQiVhO2abaYaGwKaVVsbuD7cB+i4EasAw4uQHrk456Vkw/HQnw==</InverseQ><D>DyXIfvAfC2JrCTD8MKW6e2TtSf6IHA1t5y6T+XC5jVD7T/yi0qG7ce23bt1tpChc0hGDLsTqJs3HGXzX6YJez0Frz37UuNNsNyrhh3cAnxQuAwaCZMF7tPYOQbmgXP5OFpgaIjUmhMwysmm9WdrTocE6h39t5QlLy7g6xH9MJ+k=</D></RSAKeyValue>"; string clearText = RSACryptoHelper.Decrypt(privateKey, encryptedText); //解密 Console.WriteLine(clearText); Console.ReadKey(); } } public class RSACryptoHelper { #region 密钥获取方法——暂未封装进来 //方法一: //string privateKey= provider.ToXmlString(true);//获得公/私钥对 //string publicKey= provider.ToXmlString(false);//获得公钥对 //方法二: //RSAParameters privateKey = provider.ExportParameters(true);//获得公钥私钥对 //RSAParameters publicKey = provider.ExportParameters(false);//获得公钥 #endregion /// <summary> /// 发送方加密 /// </summary> /// <param name="publicKeyXml">The public key XML.</param> /// <param name="plainText">The plain text.</param> /// <returns>System.String.</returns> /// <remarks>Editor:v-liuhch CreateTime:2015/5/16 22:06:54</remarks> public static string Encrypt(string publicKeyXml, string plainText) { RSACryptoServiceProvider provider = new RSACryptoServiceProvider(); provider.FromXmlString(publicKeyXml); //使用公钥初始化对象 byte[] plainData = Encoding.Default.GetBytes(plainText); byte[] encryptedData = provider.Encrypt(plainData, true);//对数据进行加密 return Convert.ToBase64String(encryptedData); } /// <summary> /// 接收方解密 /// </summary> /// <param name="privateKeyXml">The private key XML.</param> /// <param name="encryptedText">The encrypted text.</param> /// <returns>System.String.</returns> /// <remarks>Editor:v-liuhch CreateTime:2015/5/16 22:11:09</remarks> public static string Decrypt(string privateKeyXml, string encryptedText) { RSACryptoServiceProvider provider = new RSACryptoServiceProvider(); provider.FromXmlString(privateKeyXml);//使用私钥对数据进行初始化 byte[] encryptedData = Convert.FromBase64String(encryptedText); byte[] plainData = provider.Decrypt(encryptedData, true); //对数据进行解密 string plainText = Encoding.Default.GetString(plainData); //明文 return plainText; } } }
原文地址:http://blog.csdn.net/lhc1105/article/details/45790149