RSA加解密算法
一、什么是RSA
RSA公開密鑰密碼體制。所謂的公開密鑰密碼體制就是使用不同的加密密鑰與解密密鑰,是一種“由已知加密密鑰推導(dǎo)出解密密鑰在計算上是不可行的”密碼體制。
在公開密鑰密碼體制中,加密密鑰(即公開密鑰)PK是公開信息,而解密密鑰(即秘密密鑰)SK是需要保密的。加密算法E和解密算法D也都是公開的。雖然密鑰SK是由公開密鑰PK決定的,但卻不能根據(jù)PK計算出SK。正是基于這種理論,1978年出現(xiàn)了著名的RSA算法,它通常是先生成一對RSA 密鑰,其中之一是保密密鑰,由用戶保存;另一個為公開密鑰,可對外公開,甚至可在網(wǎng)絡(luò)服務(wù)器中注冊。為提高保密強度,RSA密鑰至少為500位長,一般推薦使用1024位。這就使加密的計算量很大。為減少計算量,在傳送信息時,常采用傳統(tǒng)加密方法 與公開密鑰加密方法相結(jié)合的方式,即信息采用改進(jìn)的DES或IDEA對話密鑰加密,然后使用RSA密鑰加密對話密鑰和信息摘要。對方收到信息后,用不同的 密鑰解密并可核對信息摘要。
RSA算法是第一個能同時用于加密和數(shù)字簽名的算法,也易于理解和操作。RSA是被研究得最廣泛的公鑰算法,從提出到現(xiàn)在的三十多年里,經(jīng)歷了各種攻擊的考驗,逐漸為人們接受,普遍認(rèn)為是目前最優(yōu)秀的公鑰方案之一。
二、RSA算法密鑰長度的選擇
1. 非對稱加密算法中1024 bit密鑰的強度相當(dāng)于對稱加密算法80bit密鑰的強度。
2. 密鑰長度增長一倍,公鑰操作所需時間增加約4倍,私鑰操作所需時間增加約8倍,公私鑰生成時間約增長16倍。
3. 一次能加密的密文長度與密鑰長度成正比,加密后的密文長度跟密鑰長度相同(RSA加密內(nèi)容的長度有限制,和密鑰長度有關(guān),這是由它的算法決定的)
a、加密的明文長度不能超過RSA密鑰的長度減去11byte,比如密鑰長度是1024位的,1024位=1024bit=128byte,128-11=117byte,所以明文長度不能超過117byte,如果長度超過該值將會拋出異常。
b、加密后密文的長度為密鑰的長度,如密鑰長度為1024bit(128Byte),最后生成的密文固定為 1024bit(128Byte)。
三、C#中的RSA加解密
.NET Framework 類庫提供了System.Security 命名空間,System.Security 命名空間提供公共語言運行時安全系統(tǒng)的基礎(chǔ)結(jié)構(gòu),包括權(quán)限的基類,而該命名空間下提供了RSACryptoServiceProvider類來執(zhí)行RSA算法的不對稱加密和解密。
1.密鑰對的生成:
a、根據(jù)RSACryptoServiceProvider直接生成
/// <summary> /// 生成密鑰 /// </summary> public RSAKey GenerateRSAKey() { RSAKey RSAKEY = new RSAKey(); RSACryptoServiceProvider RSA = new RSACryptoServiceProvider(); RSAKEY.PrivateKey = RSA.ToXmlString(true); //生成私鑰 RSAKEY.PublicKey = RSA.ToXmlString(false); //生成公鑰 RSA.Clear(); return RSAKEY; }b、通過Makecert證書創(chuàng)建工具生成安全證書
makecert -r -pe -n "CN=RSAKey" -b 03/31/2005 -e 12/31/2012 -sky exchange -ss my可通過"Visual Studio命令提示行"執(zhí)行以上命令生成證書。
查看生成的證書:
運行->輸入mmc打開控制臺->選擇文件->添加/刪除管理單元->在彈出框左側(cè)找到證書->選中證書添加->選擇我的用戶賬戶->完成確定
此時就可以在對應(yīng)位置查看到我們剛剛創(chuàng)建的名為RSAKey的證書了,如下圖:
![]()
最終我們可以將證書導(dǎo)出為:
![]()
其中RSAKey.cer中含有加密用的公鑰,RSAKey.pfx中含有解密用的私鑰。
2.創(chuàng)建加解密RSA
/// <summary> /// 創(chuàng)建加密RSA /// </summary> /// <param name="publicKey">公鑰</param> /// <returns></returns> private RSACryptoServiceProvider CreateEncryptRSA(string publicKey) { try { RSACryptoServiceProvider RSA = new RSACryptoServiceProvider(); RSA.FromXmlString(publicKey); return RSA; } catch (CryptographicException ex) { throw ex; } } /// <summary> /// 創(chuàng)建解密RSA /// </summary> /// <param name="privateKey">私鑰</param> /// <returns></returns> private RSACryptoServiceProvider CreateDecryptRSA(string privateKey) { try { RSACryptoServiceProvider RSA = new RSACryptoServiceProvider(); RSA.FromXmlString(privateKey); return RSA; } catch (CryptographicException ex) { throw ex; } } /// <summary> /// 根據(jù)安全證書創(chuàng)建加密RSA /// </summary> /// <param name="certfile">公鑰文件</param> /// <returns></returns> private RSACryptoServiceProvider X509CertCreateEncryptRSA(string certfile) { try { X509Certificate2 x509Cert = new X509Certificate2(certfile); RSACryptoServiceProvider RSA = (RSACryptoServiceProvider)x509Cert.PublicKey.Key; return RSA; } catch (CryptographicException ex) { throw ex; } } /// <summary> /// 根據(jù)私鑰文件創(chuàng)建解密RSA /// </summary> /// <param name="keyfile">私鑰文件</param> /// <param name="password">訪問含私鑰文件的密碼</param> /// <returns></returns> private RSACryptoServiceProvider X509CertCreateDecryptRSA(string keyfile, string password) { try { X509Certificate2 x509Cert = new X509Certificate2(keyfile, password); RSACryptoServiceProvider RSA = (RSACryptoServiceProvider)x509Cert.PrivateKey; return RSA; } catch (CryptographicException ex) { throw ex; } }其中所提及的私鑰文件和公鑰文件就是根據(jù)Makecert證書創(chuàng)建工具生成安全證書,而X509CertCreateDecryptRSA方法中的參數(shù)password是我們導(dǎo)出私鑰文件所設(shè)置的訪問密碼,如果沒有改密碼即使有私鑰證書也沒辦法解密。
3.RSA加解密
/// <summary> /// 加密 /// </summary> /// <param name="dataToEncrypt">待加密數(shù)據(jù)</param> /// <param name="publicKey">公鑰</param> /// <returns></returns> public string Encrypt(string dataToEncrypt, string publicKey) { Encoding encoder = Encoding.UTF8; byte[] _dataToEncrypt = encoder.GetBytes(dataToEncrypt); return this.Encrypt(_dataToEncrypt, publicKey); } /// <summary> /// 加密 /// </summary> /// <param name="dataToEncrypt">待加密數(shù)據(jù)</param> /// <param name="publicKey">公鑰</param> /// <returns></returns> public string Encrypt(byte[] dataToEncrypt, string publicKey) { using (RSACryptoServiceProvider RSA = this.CreateEncryptRSA(publicKey)) { byte[] encryptedData = RSA.Encrypt(dataToEncrypt, false); return this.BytesToHexString(encryptedData); } } /// <summary> /// 根據(jù)安全證書加密 /// </summary> /// <param name="dataToEncrypt"></param> /// <param name="certfile"></param> /// <returns></returns> public string X509CertEncrypt(string dataToEncrypt, string certfile) { Encoding encoder = Encoding.UTF8; byte[] _dataToEncrypt = encoder.GetBytes(dataToEncrypt); return this.X509CertEncrypt(_dataToEncrypt, certfile); } /// <summary> /// 根據(jù)安全證書加密 /// </summary> /// <param name="dataToEncrypt">待加密數(shù)據(jù)</param> /// <param name="certfile">安全證書</param> /// <returns></returns> public string X509CertEncrypt(byte[] dataToEncrypt, string certfile) { if (!File.Exists(certfile)) { throw new ArgumentNullException(certfile, "加密證書未找到"); } using (RSACryptoServiceProvider RSA = this.X509CertCreateEncryptRSA(certfile)) { byte[] encryptedData = RSA.Encrypt(dataToEncrypt, false); return this.BytesToHexString(encryptedData); } } /// <summary> /// 解密 /// </summary> /// <param name="encryptedData">待解密數(shù)據(jù)</param> /// <param name="privateKey">私鑰</param> /// <returns></returns> public string Decrypt(string encryptedData, string privateKey) { using (RSACryptoServiceProvider RSA = this.CreateDecryptRSA(privateKey)) { Encoding encoder = Encoding.UTF8; byte[] _encryptedData = HexStringToBytes(encryptedData); byte[] decryptedData = RSA.Decrypt(_encryptedData, false); return encoder.GetString(decryptedData); } } /// <summary> /// 解密 /// </summary> /// <param name="encryptedData">待解密數(shù)據(jù)</param> /// <param name="keyfile">私鑰文件</param> /// <param name="password">訪問私鑰文件密碼</param> /// <returns></returns> public string X509CertDecrypt(string encryptedData, string keyfile, string password) { if (!File.Exists(keyfile)) { throw new ArgumentNullException(keyfile, "解密證書未找到"); } using (RSACryptoServiceProvider RSA = this.X509CertCreateDecryptRSA(keyfile, password)) { Encoding encoder = Encoding.UTF8; byte[] _encryptedData = HexStringToBytes(encryptedData); byte[] decryptedData = RSA.Decrypt(_encryptedData, false); return encoder.GetString(decryptedData); } }最后整理了一個簡單的Demo:
![]()
Demo下載:RSACrypto.rar
參考資料:
http://dustin.iteye.com/blog/763931
http://baike.baidu.com/view/539299.htm
http://www.cnblogs.com/yjmyzz/archive/2008/08/20/1272098.html