C#自定义RSA加密解密及RSA签名和验证类实例
本文实例讲述了C#自定义RSA加密解密及RSA签名和验证类。分享给大家供大家参考。具体分析如下:
这个C#类自定义RSA加密解密及RSA签名和验证,包含了RSA加密、解密及签名所需的相关函数,带有详细的注释说明。
usingSystem; usingSystem.Text; usingSystem.Security.Cryptography; namespaceDotNet.Utilities { ///<summary> ///RSA加密解密及RSA签名和验证 ///</summary> publicclassRSACryption { publicRSACryption() { } #regionRSA加密解密 #regionRSA的密钥产生 ///<summary> ///RSA的密钥产生产生私钥和公钥 ///</summary> ///<paramname="xmlKeys"></param> ///<paramname="xmlPublicKey"></param> publicvoidRSAKey(outstringxmlKeys,outstringxmlPublicKey) { System.Security.Cryptography.RSACryptoServiceProviderrsa=newRSACryptoServiceProvider(); xmlKeys=rsa.ToXmlString(true); xmlPublicKey=rsa.ToXmlString(false); } #endregion #regionRSA的加密函数 //############################################################################## //RSA方式加密 //说明KEY必须是XML的行式,返回的是字符串 //在有一点需要说明!!该加密方式有长度限制的!! //############################################################################## //RSA的加密函数string publicstringRSAEncrypt(stringxmlPublicKey,stringm_strEncryptString) { byte[]PlainTextBArray; byte[]CypherTextBArray; stringResult; RSACryptoServiceProviderrsa=newRSACryptoServiceProvider(); rsa.FromXmlString(xmlPublicKey); PlainTextBArray=(newUnicodeEncoding()).GetBytes(m_strEncryptString); CypherTextBArray=rsa.Encrypt(PlainTextBArray,false); Result=Convert.ToBase64String(CypherTextBArray); returnResult; } //RSA的加密函数byte[] publicstringRSAEncrypt(stringxmlPublicKey,byte[]EncryptString) { byte[]CypherTextBArray; stringResult; RSACryptoServiceProviderrsa=newRSACryptoServiceProvider(); rsa.FromXmlString(xmlPublicKey); CypherTextBArray=rsa.Encrypt(EncryptString,false); Result=Convert.ToBase64String(CypherTextBArray); returnResult; } #endregion #regionRSA的解密函数 //RSA的解密函数string publicstringRSADecrypt(stringxmlPrivateKey,stringm_strDecryptString) { byte[]PlainTextBArray; byte[]DypherTextBArray; stringResult; System.Security.Cryptography.RSACryptoServiceProviderrsa=newRSACryptoServiceProvider(); rsa.FromXmlString(xmlPrivateKey); PlainTextBArray=Convert.FromBase64String(m_strDecryptString); DypherTextBArray=rsa.Decrypt(PlainTextBArray,false); Result=(newUnicodeEncoding()).GetString(DypherTextBArray); returnResult; } //RSA的解密函数byte publicstringRSADecrypt(stringxmlPrivateKey,byte[]DecryptString) { byte[]DypherTextBArray; stringResult; System.Security.Cryptography.RSACryptoServiceProviderrsa=newRSACryptoServiceProvider(); rsa.FromXmlString(xmlPrivateKey); DypherTextBArray=rsa.Decrypt(DecryptString,false); Result=(newUnicodeEncoding()).GetString(DypherTextBArray); returnResult; } #endregion #endregion #regionRSA数字签名 #region获取Hash描述表 //获取Hash描述表,sharejs.com publicboolGetHash(stringm_strSource,refbyte[]HashData) { //从字符串中取得Hash描述 byte[]Buffer; System.Security.Cryptography.HashAlgorithmMD5=System.Security.Cryptography.HashAlgorithm.Create("MD5"); Buffer=System.Text.Encoding.GetEncoding("GB2312").GetBytes(m_strSource); HashData=MD5.ComputeHash(Buffer); returntrue; } //获取Hash描述表 publicboolGetHash(stringm_strSource,refstringstrHashData) { //从字符串中取得Hash描述 byte[]Buffer; byte[]HashData; System.Security.Cryptography.HashAlgorithmMD5=System.Security.Cryptography.HashAlgorithm.Create("MD5"); Buffer=System.Text.Encoding.GetEncoding("GB2312").GetBytes(m_strSource); HashData=MD5.ComputeHash(Buffer); strHashData=Convert.ToBase64String(HashData); returntrue; } //获取Hash描述表 publicboolGetHash(System.IO.FileStreamobjFile,refbyte[]HashData) { //从文件中取得Hash描述 System.Security.Cryptography.HashAlgorithmMD5=System.Security.Cryptography.HashAlgorithm.Create("MD5"); HashData=MD5.ComputeHash(objFile); objFile.Close(); returntrue; } //获取Hash描述表 publicboolGetHash(System.IO.FileStreamobjFile,refstringstrHashData) { //从文件中取得Hash描述 byte[]HashData; System.Security.Cryptography.HashAlgorithmMD5=System.Security.Cryptography.HashAlgorithm.Create("MD5"); HashData=MD5.ComputeHash(objFile); objFile.Close(); strHashData=Convert.ToBase64String(HashData); returntrue; } #endregion #regionRSA签名 //RSA签名 publicboolSignatureFormatter(stringp_strKeyPrivate,byte[]HashbyteSignature,refbyte[]EncryptedSignatureData) { System.Security.Cryptography.RSACryptoServiceProviderRSA=newSystem.Security.Cryptography.RSACryptoServiceProvider(); RSA.FromXmlString(p_strKeyPrivate); System.Security.Cryptography.RSAPKCS1SignatureFormatterRSAFormatter=newSystem.Security.Cryptography.RSAPKCS1SignatureFormatter(RSA); //设置签名的算法为MD5 RSAFormatter.SetHashAlgorithm("MD5"); //执行签名 EncryptedSignatureData=RSAFormatter.CreateSignature(HashbyteSignature); returntrue; } //RSA签名 publicboolSignatureFormatter(stringp_strKeyPrivate,byte[]HashbyteSignature,refstringm_strEncryptedSignatureData) { byte[]EncryptedSignatureData; System.Security.Cryptography.RSACryptoServiceProviderRSA=newSystem.Security.Cryptography.RSACryptoServiceProvider(); RSA.FromXmlString(p_strKeyPrivate); System.Security.Cryptography.RSAPKCS1SignatureFormatterRSAFormatter=newSystem.Security.Cryptography.RSAPKCS1SignatureFormatter(RSA); //设置签名的算法为MD5 RSAFormatter.SetHashAlgorithm("MD5"); //执行签名 EncryptedSignatureData=RSAFormatter.CreateSignature(HashbyteSignature); m_strEncryptedSignatureData=Convert.ToBase64String(EncryptedSignatureData); returntrue; } //RSA签名 publicboolSignatureFormatter(stringp_strKeyPrivate,stringm_strHashbyteSignature,refbyte[]EncryptedSignatureData) { byte[]HashbyteSignature; HashbyteSignature=Convert.FromBase64String(m_strHashbyteSignature); System.Security.Cryptography.RSACryptoServiceProviderRSA=newSystem.Security.Cryptography.RSACryptoServiceProvider(); RSA.FromXmlString(p_strKeyPrivate); System.Security.Cryptography.RSAPKCS1SignatureFormatterRSAFormatter=newSystem.Security.Cryptography.RSAPKCS1SignatureFormatter(RSA); //设置签名的算法为MD5 RSAFormatter.SetHashAlgorithm("MD5"); //执行签名 EncryptedSignatureData=RSAFormatter.CreateSignature(HashbyteSignature); returntrue; } //RSA签名 publicboolSignatureFormatter(stringp_strKeyPrivate,stringm_strHashbyteSignature,refstringm_strEncryptedSignatureData) { byte[]HashbyteSignature; byte[]EncryptedSignatureData; HashbyteSignature=Convert.FromBase64String(m_strHashbyteSignature); System.Security.Cryptography.RSACryptoServiceProviderRSA=newSystem.Security.Cryptography.RSACryptoServiceProvider(); RSA.FromXmlString(p_strKeyPrivate); System.Security.Cryptography.RSAPKCS1SignatureFormatterRSAFormatter=newSystem.Security.Cryptography.RSAPKCS1SignatureFormatter(RSA); //设置签名的算法为MD5 RSAFormatter.SetHashAlgorithm("MD5"); //执行签名 EncryptedSignatureData=RSAFormatter.CreateSignature(HashbyteSignature); m_strEncryptedSignatureData=Convert.ToBase64String(EncryptedSignatureData); returntrue; } #endregion #regionRSA签名验证 publicboolSignatureDeformatter(stringp_strKeyPublic,byte[]HashbyteDeformatter,byte[]DeformatterData) { System.Security.Cryptography.RSACryptoServiceProviderRSA=newSystem.Security.Cryptography.RSACryptoServiceProvider(); RSA.FromXmlString(p_strKeyPublic); System.Security.Cryptography.RSAPKCS1SignatureDeformatterRSADeformatter=newSystem.Security.Cryptography.RSAPKCS1SignatureDeformatter(RSA); //指定解密的时候HASH算法为MD5 RSADeformatter.SetHashAlgorithm("MD5"); if(RSADeformatter.VerifySignature(HashbyteDeformatter,DeformatterData)) { returntrue; } else { returnfalse; } } publicboolSignatureDeformatter(stringp_strKeyPublic,stringp_strHashbyteDeformatter,byte[]DeformatterData) { byte[]HashbyteDeformatter; HashbyteDeformatter=Convert.FromBase64String(p_strHashbyteDeformatter); System.Security.Cryptography.RSACryptoServiceProviderRSA=newSystem.Security.Cryptography.RSACryptoServiceProvider(); RSA.FromXmlString(p_strKeyPublic); System.Security.Cryptography.RSAPKCS1SignatureDeformatterRSADeformatter=newSystem.Security.Cryptography.RSAPKCS1SignatureDeformatter(RSA); //指定解密的时候HASH算法为MD5 RSADeformatter.SetHashAlgorithm("MD5"); if(RSADeformatter.VerifySignature(HashbyteDeformatter,DeformatterData)) { returntrue; } else { returnfalse; } } publicboolSignatureDeformatter(stringp_strKeyPublic,byte[]HashbyteDeformatter,stringp_strDeformatterData) { byte[]DeformatterData; System.Security.Cryptography.RSACryptoServiceProviderRSA=newSystem.Security.Cryptography.RSACryptoServiceProvider(); RSA.FromXmlString(p_strKeyPublic); System.Security.Cryptography.RSAPKCS1SignatureDeformatterRSADeformatter=newSystem.Security.Cryptography.RSAPKCS1SignatureDeformatter(RSA); //指定解密的时候HASH算法为MD5 RSADeformatter.SetHashAlgorithm("MD5"); DeformatterData=Convert.FromBase64String(p_strDeformatterData); if(RSADeformatter.VerifySignature(HashbyteDeformatter,DeformatterData)) { returntrue; } else { returnfalse; } } publicboolSignatureDeformatter(stringp_strKeyPublic,stringp_strHashbyteDeformatter,stringp_strDeformatterData) { byte[]DeformatterData; byte[]HashbyteDeformatter; HashbyteDeformatter=Convert.FromBase64String(p_strHashbyteDeformatter); System.Security.Cryptography.RSACryptoServiceProviderRSA=newSystem.Security.Cryptography.RSACryptoServiceProvider(); RSA.FromXmlString(p_strKeyPublic); System.Security.Cryptography.RSAPKCS1SignatureDeformatterRSADeformatter=newSystem.Security.Cryptography.RSAPKCS1SignatureDeformatter(RSA); //指定解密的时候HASH算法为MD5 RSADeformatter.SetHashAlgorithm("MD5"); DeformatterData=Convert.FromBase64String(p_strDeformatterData); if(RSADeformatter.VerifySignature(HashbyteDeformatter,DeformatterData)) { returntrue; } else { returnfalse; } } #endregion #endregion } }
希望本文所述对大家的C#程序设计有所帮助。