RSA的加解密过程--(转自CSDN,学习用)
生活随笔
收集整理的這篇文章主要介紹了
RSA的加解密过程--(转自CSDN,学习用)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
RSA的加解密過程
?public?void?StartDemo()
?{
??//RSA的加解密過程:
??//??有?rsa1?和?rsa2?兩個RSA對象。
??//??現在要?rsa2?發送一段信息給?rsa1,?則先由?rsa1?發送“公鑰”給?rsa2
??//??rsa2?獲取得公鑰之后,用來加密要發送的數據內容。
??//??rsa1?獲取加密后的內容后,用自己的私鑰解密,得出原始的數據內容。
??RSACryptoServiceProvider?rsa1?=?new?RSACryptoServiceProvider();
??RSACryptoServiceProvider?rsa2?=?new?RSACryptoServiceProvider();
??string?publickey;
??publickey=rsa1.ToXmlString(false);??//導出?rsa1?的公鑰
??string?plaintext;
??plaintext="你好嗎?這是用于測試的字符串。";??//原始數據
??Console.WriteLine("原始數據是:\n{0}\n",plaintext);
??rsa2.FromXmlString(publickey);?//rsa2?導入?rsa1?的公鑰,用于加密信息
??//rsa2開始加密
??byte[]?cipherbytes;
??cipherbytes=rsa2.Encrypt(
???Encoding.UTF8.GetBytes(plaintext),
???false);
??/**//*//*/
??Console.WriteLine("加密后的數據是:");
??for(int?i=0;?i<?cipherbytes.Length;?i++)
??{
???Console.Write("{0:X2}?",cipherbytes[i]);
??}
??Console.WriteLine("\n");
??/**//*//*/
??//rsa1開始解密
??byte[]?plaintbytes;
??plaintbytes?=?rsa1.Decrypt(cipherbytes,false);
??Console.WriteLine("解密后的數據是:");
??Console.WriteLine(Encoding.UTF8.GetString(plaintbytes));
??Console.ReadLine();
?}
}
?public?void?StartDemo()
?{
??//RSA的加解密過程:
??//??有?rsa1?和?rsa2?兩個RSA對象。
??//??現在要?rsa2?發送一段信息給?rsa1,?則先由?rsa1?發送“公鑰”給?rsa2
??//??rsa2?獲取得公鑰之后,用來加密要發送的數據內容。
??//??rsa1?獲取加密后的內容后,用自己的私鑰解密,得出原始的數據內容。
??RSACryptoServiceProvider?rsa1?=?new?RSACryptoServiceProvider();
??RSACryptoServiceProvider?rsa2?=?new?RSACryptoServiceProvider();
??string?publickey;
??publickey=rsa1.ToXmlString(false);??//導出?rsa1?的公鑰
??string?plaintext;
??plaintext="你好嗎?這是用于測試的字符串。";??//原始數據
??Console.WriteLine("原始數據是:\n{0}\n",plaintext);
??rsa2.FromXmlString(publickey);?//rsa2?導入?rsa1?的公鑰,用于加密信息
??//rsa2開始加密
??byte[]?cipherbytes;
??cipherbytes=rsa2.Encrypt(
???Encoding.UTF8.GetBytes(plaintext),
???false);
??/**//*//*/
??Console.WriteLine("加密后的數據是:");
??for(int?i=0;?i<?cipherbytes.Length;?i++)
??{
???Console.Write("{0:X2}?",cipherbytes[i]);
??}
??Console.WriteLine("\n");
??/**//*//*/
??//rsa1開始解密
??byte[]?plaintbytes;
??plaintbytes?=?rsa1.Decrypt(cipherbytes,false);
??Console.WriteLine("解密后的數據是:");
??Console.WriteLine(Encoding.UTF8.GetString(plaintbytes));
??Console.ReadLine();
?}
}
轉載于:https://www.cnblogs.com/hanguoji/archive/2006/12/04/581173.html
總結
以上是生活随笔為你收集整理的RSA的加解密过程--(转自CSDN,学习用)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: [HDOJ]1005. Number S
- 下一篇: 一张内涵图