C#自定义RSA加密解密及RSA签名和验证类实例
程序员文章站
2023-12-04 17:30:16
本文实例讲述了c#自定义rsa加密解密及rsa签名和验证类。分享给大家供大家参考。具体分析如下:
这个c#类自定义rsa加密解密及rsa签名和验证,包含了rsa加密、解密...
本文实例讲述了c#自定义rsa加密解密及rsa签名和验证类。分享给大家供大家参考。具体分析如下:
这个c#类自定义rsa加密解密及rsa签名和验证,包含了rsa加密、解密及签名所需的相关函数,带有详细的注释说明。
using system; using system.text; using system.security.cryptography; namespace dotnet.utilities { /// <summary> /// rsa加密解密及rsa签名和验证 /// </summary> public class rsacryption { public rsacryption() { } #region rsa 加密解密 #region rsa 的密钥产生 /// <summary> /// rsa 的密钥产生 产生私钥 和公钥 /// </summary> /// <param name="xmlkeys"></param> /// <param name="xmlpublickey"></param> public void rsakey(out string xmlkeys,out string xmlpublickey) { system.security.cryptography.rsacryptoserviceprovider rsa=new rsacryptoserviceprovider(); xmlkeys=rsa.toxmlstring(true); xmlpublickey = rsa.toxmlstring(false); } #endregion #region rsa的加密函数 //############################################################################## //rsa 方式加密 //说明key必须是xml的行式,返回的是字符串 //在有一点需要说明!!该加密方式有 长度 限制的!! //############################################################################## //rsa的加密函数 string public string rsaencrypt(string xmlpublickey,string m_strencryptstring ) { byte[] plaintextbarray; byte[] cyphertextbarray; string result; rsacryptoserviceprovider rsa=new rsacryptoserviceprovider(); rsa.fromxmlstring(xmlpublickey); plaintextbarray = (new unicodeencoding()).getbytes(m_strencryptstring); cyphertextbarray = rsa.encrypt(plaintextbarray, false); result=convert.tobase64string(cyphertextbarray); return result; } //rsa的加密函数 byte[] public string rsaencrypt(string xmlpublickey,byte[] encryptstring ) { byte[] cyphertextbarray; string result; rsacryptoserviceprovider rsa=new rsacryptoserviceprovider(); rsa.fromxmlstring(xmlpublickey); cyphertextbarray = rsa.encrypt(encryptstring, false); result=convert.tobase64string(cyphertextbarray); return result; } #endregion #region rsa的解密函数 //rsa的解密函数 string public string rsadecrypt(string xmlprivatekey, string m_strdecryptstring ) { byte[] plaintextbarray; byte[] dyphertextbarray; string result; system.security.cryptography.rsacryptoserviceprovider rsa=new rsacryptoserviceprovider(); rsa.fromxmlstring(xmlprivatekey); plaintextbarray =convert.frombase64string(m_strdecryptstring); dyphertextbarray=rsa.decrypt(plaintextbarray, false); result=(new unicodeencoding()).getstring(dyphertextbarray); return result; } //rsa的解密函数 byte public string rsadecrypt(string xmlprivatekey, byte[] decryptstring ) { byte[] dyphertextbarray; string result; system.security.cryptography.rsacryptoserviceprovider rsa=new rsacryptoserviceprovider(); rsa.fromxmlstring(xmlprivatekey); dyphertextbarray=rsa.decrypt(decryptstring, false); result=(new unicodeencoding()).getstring(dyphertextbarray); return result; } #endregion #endregion #region rsa数字签名 #region 获取hash描述表 //获取hash描述表 ,sharejs.com public bool gethash(string m_strsource, ref byte[] hashdata) { //从字符串中取得hash描述 byte[] buffer; system.security.cryptography.hashalgorithm md5 = system.security.cryptography.hashalgorithm.create("md5"); buffer = system.text.encoding.getencoding("gb2312").getbytes(m_strsource); hashdata = md5.computehash(buffer); return true; } //获取hash描述表 public bool gethash(string m_strsource, ref string strhashdata) { //从字符串中取得hash描述 byte[] buffer; byte[] hashdata; system.security.cryptography.hashalgorithm md5 = system.security.cryptography.hashalgorithm.create("md5"); buffer = system.text.encoding.getencoding("gb2312").getbytes(m_strsource); hashdata = md5.computehash(buffer); strhashdata = convert.tobase64string(hashdata); return true; } //获取hash描述表 public bool gethash(system.io.filestream objfile, ref byte[] hashdata) { //从文件中取得hash描述 system.security.cryptography.hashalgorithm md5 = system.security.cryptography.hashalgorithm.create("md5"); hashdata = md5.computehash(objfile); objfile.close(); return true; } //获取hash描述表 public bool gethash(system.io.filestream objfile, ref string strhashdata) { //从文件中取得hash描述 byte[] hashdata; system.security.cryptography.hashalgorithm md5 = system.security.cryptography.hashalgorithm.create("md5"); hashdata = md5.computehash(objfile); objfile.close(); strhashdata = convert.tobase64string(hashdata); return true; } #endregion #region rsa签名 //rsa签名 public bool signatureformatter(string p_strkeyprivate, byte[] hashbytesignature, ref byte[] encryptedsignaturedata) { system.security.cryptography.rsacryptoserviceprovider rsa = new system.security.cryptography.rsacryptoserviceprovider(); rsa.fromxmlstring(p_strkeyprivate); system.security.cryptography.rsapkcs1signatureformatter rsaformatter = new system.security.cryptography.rsapkcs1signatureformatter(rsa); //设置签名的算法为md5 rsaformatter.sethashalgorithm("md5"); //执行签名 encryptedsignaturedata = rsaformatter.createsignature(hashbytesignature); return true; } //rsa签名 public bool signatureformatter(string p_strkeyprivate, byte[] hashbytesignature, ref string m_strencryptedsignaturedata) { byte[] encryptedsignaturedata; system.security.cryptography.rsacryptoserviceprovider rsa = new system.security.cryptography.rsacryptoserviceprovider(); rsa.fromxmlstring(p_strkeyprivate); system.security.cryptography.rsapkcs1signatureformatter rsaformatter = new system.security.cryptography.rsapkcs1signatureformatter(rsa); //设置签名的算法为md5 rsaformatter.sethashalgorithm("md5"); //执行签名 encryptedsignaturedata = rsaformatter.createsignature(hashbytesignature); m_strencryptedsignaturedata = convert.tobase64string(encryptedsignaturedata); return true; } //rsa签名 public bool signatureformatter(string p_strkeyprivate, string m_strhashbytesignature, ref byte[] encryptedsignaturedata) { byte[] hashbytesignature; hashbytesignature = convert.frombase64string(m_strhashbytesignature); system.security.cryptography.rsacryptoserviceprovider rsa = new system.security.cryptography.rsacryptoserviceprovider(); rsa.fromxmlstring(p_strkeyprivate); system.security.cryptography.rsapkcs1signatureformatter rsaformatter = new system.security.cryptography.rsapkcs1signatureformatter(rsa); //设置签名的算法为md5 rsaformatter.sethashalgorithm("md5"); //执行签名 encryptedsignaturedata = rsaformatter.createsignature(hashbytesignature); return true; } //rsa签名 public bool signatureformatter(string p_strkeyprivate, string m_strhashbytesignature, ref string m_strencryptedsignaturedata) { byte[] hashbytesignature; byte[] encryptedsignaturedata; hashbytesignature = convert.frombase64string(m_strhashbytesignature); system.security.cryptography.rsacryptoserviceprovider rsa = new system.security.cryptography.rsacryptoserviceprovider(); rsa.fromxmlstring(p_strkeyprivate); system.security.cryptography.rsapkcs1signatureformatter rsaformatter = new system.security.cryptography.rsapkcs1signatureformatter(rsa); //设置签名的算法为md5 rsaformatter.sethashalgorithm("md5"); //执行签名 encryptedsignaturedata = rsaformatter.createsignature(hashbytesignature); m_strencryptedsignaturedata = convert.tobase64string(encryptedsignaturedata); return true; } #endregion #region rsa 签名验证 public bool signaturedeformatter(string p_strkeypublic, byte[] hashbytedeformatter, byte[] deformatterdata) { system.security.cryptography.rsacryptoserviceprovider rsa = new system.security.cryptography.rsacryptoserviceprovider(); rsa.fromxmlstring(p_strkeypublic); system.security.cryptography.rsapkcs1signaturedeformatter rsadeformatter = new system.security.cryptography.rsapkcs1signaturedeformatter(rsa); //指定解密的时候hash算法为md5 rsadeformatter.sethashalgorithm("md5"); if(rsadeformatter.verifysignature(hashbytedeformatter,deformatterdata)) { return true; } else { return false; } } public bool signaturedeformatter(string p_strkeypublic, string p_strhashbytedeformatter, byte[] deformatterdata) { byte[] hashbytedeformatter; hashbytedeformatter = convert.frombase64string(p_strhashbytedeformatter); system.security.cryptography.rsacryptoserviceprovider rsa = new system.security.cryptography.rsacryptoserviceprovider(); rsa.fromxmlstring(p_strkeypublic); system.security.cryptography.rsapkcs1signaturedeformatter rsadeformatter = new system.security.cryptography.rsapkcs1signaturedeformatter(rsa); //指定解密的时候hash算法为md5 rsadeformatter.sethashalgorithm("md5"); if(rsadeformatter.verifysignature(hashbytedeformatter,deformatterdata)) { return true; } else { return false; } } public bool signaturedeformatter(string p_strkeypublic, byte[] hashbytedeformatter, string p_strdeformatterdata) { byte[] deformatterdata; system.security.cryptography.rsacryptoserviceprovider rsa = new system.security.cryptography.rsacryptoserviceprovider(); rsa.fromxmlstring(p_strkeypublic); system.security.cryptography.rsapkcs1signaturedeformatter rsadeformatter = new system.security.cryptography.rsapkcs1signaturedeformatter(rsa); //指定解密的时候hash算法为md5 rsadeformatter.sethashalgorithm("md5"); deformatterdata =convert.frombase64string(p_strdeformatterdata); if(rsadeformatter.verifysignature(hashbytedeformatter,deformatterdata)) { return true; } else { return false; } } public bool signaturedeformatter(string p_strkeypublic, string p_strhashbytedeformatter, string p_strdeformatterdata) { byte[] deformatterdata; byte[] hashbytedeformatter; hashbytedeformatter = convert.frombase64string(p_strhashbytedeformatter); system.security.cryptography.rsacryptoserviceprovider rsa = new system.security.cryptography.rsacryptoserviceprovider(); rsa.fromxmlstring(p_strkeypublic); system.security.cryptography.rsapkcs1signaturedeformatter rsadeformatter = new system.security.cryptography.rsapkcs1signaturedeformatter(rsa); //指定解密的时候hash算法为md5 rsadeformatter.sethashalgorithm("md5"); deformatterdata =convert.frombase64string(p_strdeformatterdata); if(rsadeformatter.verifysignature(hashbytedeformatter,deformatterdata)) { return true; } else { return false; } } #endregion #endregion } }
希望本文所述对大家的c#程序设计有所帮助。
上一篇: C#操作CSV文件类实例
下一篇: C#生成随机ArrayList的方法