




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
RSA加密解密及RSA簽名和驗證1.RSA加密解密:(1)獲取密鑰,這?是產(chǎn)?密鑰,實際應?中可以從各種存儲介質(zhì)上讀取密鑰(2)加密(3)解密2.RSA簽名和驗證(1)獲取密鑰,這?是產(chǎn)?密鑰,實際應?中可以從各種存儲介質(zhì)上讀取密鑰(2)獲取待簽名的Hash碼(3)獲取簽名的字符串(4)驗證3.公鑰與私鑰的理解:(1)私鑰?來進?解密和簽名,是給???的。(2)公鑰由本?公開,?于加密和驗證簽名,是給別??的。(3)當該?戶發(fā)送?件時,?私鑰簽名,別??他給的公鑰驗證簽名,可以保證該信息是由他發(fā)送的。當該?戶接受?件時,別??他的公鑰加密,他?私鑰解密,可以保證該信息只能由他接收到。classRSACryption{#regionRSA加密解密#regionRSA的密鑰產(chǎn)?///<summary>///RSA產(chǎn)?密鑰///</summary>///<paramname="xmlKeys">私鑰</param>///<paramname="xmlPublicKey">公鑰</param>publicvoidRSAKey(outstringxmlKeys,outstringxmlPublicKey){try{System.Security.Cryptography.RSACryptoServiceProviderrsa=newRSACryptoServiceProvider();xmlKeys=rsa.ToXmlString(true);xmlPublicKey=rsa.ToXmlString(false);}catch(Exceptionex){throwex;}}#endregion#regionRSA加密函數(shù)//##############################################################################//RSA?式加密//KEY必須是XML的形式,返回的是字符串//該加密?式有長度限制的!//##############################################################################///<summary>///RSA的加密函數(shù)///</summary>///<paramname="xmlPublicKey">公鑰</param>///<paramname="encryptString">待加密的字符串</param>///<returns></returns>publicstringRSAEncrypt(stringxmlPublicKey,stringencryptString){try{byte[]PlainTextBArray;byte[]CypherTextBArray;stringResult;System.Security.Cryptography.RSACryptoServiceProviderrsa=newRSACryptoServiceProvider();rsa.FromXmlString(xmlPublicKey);PlainTextBArray=(newUnicodeEncoding()).GetBytes(encryptString);CypherTextBArray=rsa.Encrypt(PlainTextBArray,false);Result=Convert.ToBase64String(CypherTextBArray);returnResult;}catch(Exceptionex){throwex;}}///<summary>///RSA的加密函數(shù)///</summary>///<paramname="xmlPublicKey">公鑰</param>///<paramname="EncryptString">待加密的字節(jié)數(shù)組</param>///<returns></returns>publicstringRSAEncrypt(stringxmlPublicKey,byte[]EncryptString){try{byte[]CypherTextBArray;stringResult;System.Security.Cryptography.RSACryptoServiceProviderrsa=newRSACryptoServiceProvider();rsa.FromXmlString(xmlPublicKey);CypherTextBArray=rsa.Encrypt(EncryptString,false);Result=Convert.ToBase64String(CypherTextBArray);returnResult;}catch(Exceptionex){throwex;}}#endregion#regionRSA的解密函數(shù)///<summary>///RSA的解密函數(shù)///</summary>///<paramname="xmlPrivateKey">私鑰</param>///<paramname="decryptString">待解密的字符串</param>///<returns></returns>publicstringRSADecrypt(stringxmlPrivateKey,stringdecryptString){try{byte[]PlainTextBArray;byte[]DypherTextBArray;stringResult;System.Security.Cryptography.RSACryptoServiceProviderrsa=newRSACryptoServiceProvider();rsa.FromXmlString(xmlPrivateKey);PlainTextBArray=Convert.FromBase64String(decryptString);DypherTextBArray=rsa.Decrypt(PlainTextBArray,false);Result=(newUnicodeEncoding()).GetString(DypherTextBArray);returnResult;}catch(Exceptionex){throwex;}}///<summary>///RSA的解密函數(shù)///</summary>///<paramname="xmlPrivateKey">私鑰</param>///<paramname="DecryptString">待解密的字節(jié)數(shù)組</param>///<returns></returns>publicstringRSADecrypt(stringxmlPrivateKey,byte[]DecryptString){try{byte[]DypherTextBArray;stringResult;System.Security.Cryptography.RSACryptoServiceProviderrsa=newRSACryptoServiceProvider();rsa.FromXmlString(xmlPrivateKey);DypherTextBArray=rsa.Decrypt(DecryptString,false);Result=(newUnicodeEncoding()).GetString(DypherTextBArray);returnResult;}catch(Exceptionex){throwex;}}#endregion#endregion#regionRSA數(shù)字簽名#region獲取Hash描述表///<summary>///獲取Hash描述表///</summary>///<paramname="strSource">待簽名的字符串</param>///<paramname="HashData">Hash描述</param>///<returns></returns>publicboolGetHash(stringstrSource,refbyte[]HashData){try{byte[]Buffer;System.Security.Cryptography.HashAlgorithmMD5=System.Security.Cryptography.HashAlgorithm.Create("MD5");System.Text.Encoding.GetEncoding("GB2312").GetBytes(strSource);Buffer=HashData=MD5.ComputeHash(Buffer);returntrue;}catch(Exceptionex){throwex;}}///<summary>///獲取Hash描述表///</summary>///<paramname="strSource">待簽名的字符串</param>///<paramname="strHashData">Hash描述</param>///<returns></returns>publicboolGetHash(stringstrSource,refstringstrHashData){try{//從字符串中取得Hash描述byte[]Buffer;byte[]HashData;System.Security.Cryptography.HashAlgorithmMD5=System.Security.Cryptography.HashAlgorithm.Create("MD5");System.Text.Encoding.GetEncoding("GB2312").GetBytes(strSource);Buffer=HashData=MD5.ComputeHash(Buffer);strHashData=Convert.ToBase64String(HashData);returntrue;}catch(Exceptionex){throwex;}}///<summary>///獲取Hash描述表///</summary>///<paramname="objFile">待簽名的?件</param>///<paramname="HashData">Hash描述</param>///<returns></returns>publicboolGetHash(System.IO.FileStreamobjFile,refbyte[]HashData){try{//從?件中取得Hash描述System.Security.Cryptography.HashAlgorithmMD5=System.Security.Cryptography.HashAlgorithm.Create("MD5");HashData=MD5.ComputeHash(objFile);objFile.Close();returntrue;}catch(Exceptionex){throwex;}}///<summary>///獲取Hash描述表///</summary>///<paramname="objFile">待簽名的?件</param>///<paramname="strHashData">Hash描述</param>///<returns></returns>publicboolGetHash(System.IO.FileStreamobjFile,refstringstrHashData){try{//從?件中取得Hash描述byte[]HashData;System.Security.Cryptography.HashAlgorithmMD5=System.Security.Cryptography.HashAlgorithm.Create("MD5");HashData=MD5.ComputeHash(objFile);objFile.Close();strHashData=Convert.ToBase64String(HashData);returntrue;}catch(Exceptionex){throwex;}}#endregion#regionRSA簽名///<summary>///RSA簽名///<paramname="strKeyPrivate">私鑰</param>///<paramname="HashbyteSignature">待簽名Hash描述</param>///<paramname="EncryptedSignatureData">簽名后的結(jié)果</param>///<returns></returns>publicboolSignatureFormatter(stringstrKeyPrivate,byte[]HashbyteSignature,refbyte[]EncryptedSignatureData){try{System.Security.Cryptography.RSACryptoServiceProviderRSA=newSystem.Security.Cryptography.RSACryptoServiceProvider();RSA.FromXmlString(strKeyPrivate);System.Security.Cryptography.RSAPKCS1SignatureFormatterRSAFormatter=newSystem.Security.Cryptography.RSAPKCS1SignatureFormatter(RSA);//設(shè)置簽名的算法為MD5RSAFormatter.SetHashAlgorithm("MD5");//執(zhí)?簽名EncryptedSignatureData=RSAFormatter.CreateSignature(HashbyteSignature);returntrue;}catch(Exceptionex){throwex;}}///<summary>///RSA簽名///</summary>///<paramname="strKeyPrivate">私鑰</param>///<paramname="HashbyteSignature">待簽名Hash描述</param>///<paramname="m_strEncryptedSignatureData">簽名后的結(jié)果</param>///<returns></returns>publicboolSignatureFormatter(stringstrKeyPrivate,byte[]HashbyteSignature,refstringstrEncryptedSignatureData){try{byte[]EncryptedSignatureData;System.Security.Cryptography.RSACryptoServiceProviderRSA=newSystem.Security.Cryptography.RSACryptoServiceProvider();RSA.FromXmlString(strKeyPrivate);System.Security.Cryptography.RSAPKCS1SignatureFormatterRSAFormatter=newSystem.Security.Cryptography.RSAPKCS1SignatureFormatter(RSA);//設(shè)置簽名的算法為MD5RSAFormatter.SetHashAlgorithm("MD5");//執(zhí)?簽名EncryptedSignatureData=RSAFormatter.CreateSignature(HashbyteSignature);strEncryptedSignatureData=Convert.ToBase64String(EncryptedSignatureData);returntrue;}catch(Exceptionex){throwex;}}///<summary>///RSA簽名///</summary>///<paramname="strKeyPrivate">私鑰</param>///<paramname="strHashbyteSignature">待簽名Hash描述</param>///<paramname="EncryptedSignatureData">簽名后的結(jié)果</param>///<returns></returns>publicboolSignatureFormatter(stringstrKeyPrivate,stringstrHashbyteSignature,refbyte[]EncryptedSignatureData){try{byte[]HashbyteSignature;HashbyteSignature=Convert.FromBase64String(strHashbyteSignature);System.Security.Cryptography.RSACryptoServiceProviderRSA=newSystem.Security.Cryptography.RSACryptoServiceProvider();RSA.FromXmlString(strKeyPrivate);System.Security.Cryptography.RSAPKCS1SignatureFormatterRSAFormatter=newSystem.Security.Cryptography.RSAPKCS1SignatureFormatter(RSA);//設(shè)置簽名的算法為MD5RSAFormatter.SetHashAlgorithm("MD5");//執(zhí)?簽名EncryptedSignatureData=RSAFormatter.CreateSignature(HashbyteSignature);returntrue;}catch(Exceptionex){throwex;}}///RSA簽名///</summary>///<paramname="strKeyPrivate">私鑰</param>///<paramname="strHashbyteSignature">待簽名Hash描述</param>///<paramname="strEncryptedSignatureData">簽名后的結(jié)果</param>///<returns></returns>publicboolSignatureFormatter(stringstrKeyPrivate,stringstrHashbyteSignature,refstringstrEncryptedSignatureData){try{byte[]HashbyteSignature;byte[]EncryptedSignatureData;HashbyteSignature=Convert.FromBase64String(strHashbyteSignature);System.Security.Cryptography.RSACryptoServiceProviderRSA=newSystem.Security.Cryptography.RSACryptoServiceProvider();RSA.FromXmlString(strKeyPrivate);System.Security.Cryptography.RSAPKCS1SignatureFormatterRSAFormatter=newSystem.Security.Cryptography.RSAPKCS1SignatureFormatter(RSA);//設(shè)置簽名的算法為MD5RSAFormatter.SetHashAlgorithm("MD5");//執(zhí)?簽名EncryptedSignatureData=RSAFormatter.CreateSignature(HashbyteSignature);strEncryptedSignatureData=Convert.ToBase64String(EncryptedSignatureData);returntrue;}catch(Exceptionex){throwex;}}#endregion#regionRSA簽名驗證///<summary>///RSA簽名驗證///</summary>///<paramname="strKeyPublic">公鑰</param>///<paramname="HashbyteDeformatter">Hash描述</param>///<paramname="DeformatterData">簽名后的結(jié)果</param>///<returns></returns>publicboolSignatureDeformatter(stringstrKeyPublic,byte[]HashbyteDeformatter,byte[]DeformatterData){try{System.Security.Cryptography.RSACryptoServiceProviderRSA=newSystem.Security.Cryptography.RSACryptoServiceProvider();RSA.FromXmlString(strKeyPublic);System.Security.Cryptography.RSAPKCS1SignatureDeformatterRSADeformatter=newSystem.Security.Cryptography.RSAPKCS1SignatureDeformatter(RSA);//指定解密的時候HASH算法為MD5RSADeformatter.SetHashAlgorithm("MD5");if(RSADeformatter.VerifySignature(HashbyteDeformatter,DeformatterData)){returntrue;}else{returnfalse;}}catch(Exceptionex){throwex;}}///<summary>///RSA簽名驗證///</summary>///<paramname="strKeyPublic">公鑰</param>///<paramname="strHashbyteDeformatter">Hash描述</param>///<paramname="DeformatterData">簽名后的結(jié)果</param>///<returns></returns>publicboolSignatureDeformatter(stringstrKeyPublic,stringstrHashbyteDeformatter,byte[]DeformatterData){try{byte[]HashbyteDeformatter;HashbyteDeformatter=Convert.FromBase64String(strHashbyteDeformatter);System.Security.Cryptography.RSACryptoServiceProviderRSA=newSystem.Security.Cryptography.RSACryptoServiceProvider();RSA.FromXmlString(strKeyPublic);System.Security.Cryptography.RSAPKCS1SignatureDeformatterRSADeformatter=newSystem.Security.Cryptography.RSAPKCS1SignatureDeformatter(RSA);//指定解密的時候HASH算法為MD5RSADeformatter.SetHashAlgorithm("MD5");if(RSADeformatter.VerifySignature(HashbyteDeformatter,DeformatterData)){returntrue;}else{returnfalse;}}catch(Exceptionex){throwex;}}///<summary>///RSA簽名驗證///</summary>///<paramname="strKeyPublic">公鑰</param>///<paramname="HashbyteDeformatter">Hash描述</param>///<paramname="strDeformatterData">簽名后的結(jié)果</param>///<returns></returns>publicboolSignatureDeformatter(stringstrKeyPublic,byte[]HashbyteDeformatter,stringstrDeformatterData){try{byte[]DeformatterData;System.Security.Cryptography.RSACryptoServiceProviderRSA=newSystem.Security.Cryptography.RSACryptoServiceProvider();RSA.FromXmlString(strKeyPublic);System.Security.Cryptography.RSAPKCS1SignatureDeformatterRSADeformatter=newSystem.Security.Cryptography.RSAPKCS1SignatureDeformatter(RSA);//指定解密的時候HASH算法為MD5RSADef
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 物業(yè)開會發(fā)言稿
- 房東托付中介合同合集4篇
- 體育跨學科教學的課堂教學模式
- 物理跨學科教學的教學目標設(shè)定策略
- 教改課題申報書修改意見
- 老年人健康服務(wù)的信息化建設(shè)策略
- 工業(yè)旅游的營銷策略與推廣途徑
- 眼科護理病例演講課件
- 初三班主任工作計劃第二學期10篇
- 典當合同范本
- 人工智能訓練師(中級數(shù)據(jù)標注員)理論考試題庫(含答案)
- 醫(yī)療器械委托生產(chǎn)控制程序
- 法院電子卷宗制度
- 光伏發(fā)電施工勞務(wù)分包合同模板
- 【紅樓夢中的林黛玉性格探析5200字(論文)】
- 2024年度《冠心病》全套課件(完整版)
- (2024年)財務(wù)報表分析培訓講義
- 融合安全數(shù)據(jù)底座分析中臺需求
- 大林和小林課件知識講稿
- 正面吊安全操作規(guī)程培訓
- 第六部分+攝影構(gòu)圖002
評論
0/150
提交評論