[DEncrypt] RSACryption--RSA加密/解密字符串 (转载)
这个类是关于加密,解密的操作,文件的一些高级操作
1.RSACryption RSA 的密钥产生
2.RSACryption RSA的加密函数
3.RSACryption RSA的解密函数
4.RSACryption 获取Hash描述表
5.RSACryption RSA签名
6.RSACryption RSA签名认证
看下面代码吧
- /// <summary>
- /// 类说明:Assistant
- /// 编 码 人:苏飞
- /// 联系方式:361983679
- /// 更新网站:[url=http://www.cckan.net/thread-655-1-1.html]http://www.cckan.net/thread-655-1-1.html[/url]
- /// </summary>
- 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描述表
- 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
- }
- }
[DEncrypt] RSACryption--RSA加密/解密字符串 (转载)的更多相关文章
- RSA加密解密及RSA签名和验证及证书
RSA加密解密及RSA签名和验证及证书 公钥是给别人的 发送密文使用公钥加密 验证签名使用公钥验证 私钥是自己保留的 接受密文使用私钥解密 发送签名使用私钥签名 上述过程逆转是不行的,比如使用私钥加密 ...
- RSA加密解密及RSA签名和验证
原文:RSA加密解密及RSA签名和验证 1.RSA加密解密: (1)获取密钥,这里是产生密钥,实际应用中可以从各种存储介质上读取密钥 (2)加密 (3)解密2.RSA签名和验证 (1)获取密钥,这里是 ...
- 最通俗易懂的RSA加密解密指导
前言 RSA加密算法是一种非对称加密算法,简单来说,就是加密时使用一个钥匙,解密时使用另一个钥匙. 因为加密的钥匙是公开的,所又称公钥,解密的钥匙是不公开的,所以称为私钥. 密钥 关于RSA加密有很多 ...
- C# Java间进行RSA加密解密交互
原文:C# Java间进行RSA加密解密交互 这里,讲一下RSA算法加解密在C#和Java之间交互的问题,这两天纠结了很久,也看了很多其他人写的文章,颇受裨益,但没能解决我的实际问题,终于,还是被我捣 ...
- C# Java间进行RSA加密解密交互(二)
原文:C# Java间进行RSA加密解密交互(二) 接着前面一篇文章C# Java间进行RSA加密解密交互,继续探讨这个问题. 在前面,虽然已经实现了C# Java间进行RSA加密解密交互,但是还是与 ...
- C# Java间进行RSA加密解密交互(三)
原文:C# Java间进行RSA加密解密交互(三) 接着前面一篇C# Java间进行RSA加密解密交互(二)说吧,在上篇中为了实现 /** * RSA加密 * @param text--待加密的明文 ...
- RSA加密解密和读取公钥、私钥
/// <summary> /// RSA加密解密及RSA签名和验证 /// </summary> public class RSADE { ...
- RSA加密解密及RSA加签验签
RSA安全性应用场景说明 在刚接触RSA的时候,会混淆RSA加密解密和RSA加签验签的概念.简单来说加密解密是公钥加密私钥解密,持有公钥(多人持有)可以对数据加密,但是只有持有私钥(一人持有)才可以解 ...
- RSA加密解密算法
/** * RSA加密解密算法 * Class Rsa */ class Rsa { /** * 获取pem格式的公钥 * @param $public_key 公钥文件路径或者字符串 * @retu ...
随机推荐
- [LeetCode#218] The Skyline Problem
Problem: A city's skyline is the outer contour of the silhouette formed by all the buildings in that ...
- statspack系列3
原文:http://jonathanlewis.wordpress.com/2006/12/27/analysing-statspack-3/ 作者:Jonathan Lewis 下面的例子中的结果并 ...
- Android Loader详解三:重启与回调
重启装载器 当你使用initLoader()时,如果指定ID的装载器已经存在,则它使用这个装载器.如果不存在呢,它将创建一个新的.但是有时你却是想丢弃旧的然后开始新的数据. 要想丢弃旧数据,你应使用r ...
- RobotFramework+Selenium2library+Appium+Python+RIDE安装指南
最近在测试APP+WEB,想找一个好的自动化测试工具.然后发现RIDE这工具,框架比较自由,支持中文,有测试报告. 一个好的自动化测试就应该包含:Case管理+脚本的编写+自动生产报告. 如此一想,这 ...
- AppStore IAP 客户端校验代码
-(BOOL)putStringToItunes:(SKPaymentTransaction*)transaction { NSData * iapData = transaction.transac ...
- Python的模块,模块的使用、安装,别名,作用域等概念
所谓的模块就是将不同功能的函数分别放到不同的文件中,这样不仅有利于函数的维护,也方便了函数的调用.在Python中,一个.py文件就是一个模块(Module). 在模块的上层有一个叫做包(Packag ...
- C++ 把输出结果写入文件/从文件中读取数据
先包含头文文件 #include<fstream> 输出到文件 ofstream fout; //声明一个输出流对象 fout.open("output.txt"); ...
- Yii rabc角色权限管理文章推荐
yii的这个rbac太通用,太灵活,有时候理解起来有困难.也是初学这个,推荐一个不错的文章:http://www.yiiframework.com/wiki/136/getting-to-unders ...
- 利用URL重写实现参数目录化
参数目录化,就是将 类似 http://www.abc.com/store/store.aspx?id=1024 这样的网址,对外改为 http://www.abc.com/1024. 要实现这种功能 ...
- 加速数组操作(Array)
Measure-Command { $ar = @() for ($x=0; $x -lt 10000; $x++) { $ar += $x } }执行结果:3.301s Measure-Comman ...