📄 rsa加密解密及rsa签名和验证.txt
字号:
此Demo包含两个文件,建立一个解决方案,然后建立两个文件,一个为Form,一个为Class,把代码分别复制进去即可
RSA正确的执行过程:
加密解密:
1、获取密钥,这里是产生密钥,实际应用中可以从各种存储介质上读取密钥
2、加密
3、解密
签名和验证:
签名:
1、获取密钥,这里是产生密钥,实际应用中可以从各种存储介质上读取密钥
2、获取待签名的Hash码
3、签名
其中,1和2的步骤无所谓,在本例中,我们将对txtSource里的内容进行签名,也可以对文件进行签名
验证签名:
1、获取密钥,这里是产生密钥,实际应用中可以从各种存储介质上读取密钥
2、获取待验证签名的Hash码
3、获取签名的字串,这里签名的字串存储在m_strEncryptedSignatureData变量中,在DEMO中必须通过签名才能获得这个字串,因此需要先执行签名,当然也可以更改之后通过别的方式获得
4、验证
其中,1和2的步骤无所谓,在本例中,我们将对txtSource里的内容进行签名验证,也可以对文件进行签名验证
如果是文件,取得文件之后把文件的内容以byte[]的方式代入即可
///////////////////////////////////////////////////////////////////////////////////////////////////////////
//RSACryption.cs
///////////////////////////////////////////////////////////////////////////////////////////////////////////
using System;
using System.Text;
using System.Security.Cryptography;
namespace RSAApplication
{
/// <summary>
/// RSACryption 的摘要说明。
/// </summary>
public class RSACryption
{
#region 构造函数
public RSACryption()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
#endregion
#region RSA 加密解密
#region RSA 的密钥产生
//RSA 的密钥产生
//产生私钥 和公钥
public void RSAKey(out string xmlKeys,out string xmlPublicKey)
{
try
{
System.Security.Cryptography.RSACryptoServiceProvider rsa=new RSACryptoServiceProvider();
xmlKeys=rsa.ToXmlString(true);
xmlPublicKey = rsa.ToXmlString(false);
}
catch(Exception ex)
{
throw ex;
}
}
#endregion
#region RSA的加密函数
//##############################################################################
//RSA 方式加密
//说明KEY必须是XML的行式,返回的是字符串
//在有一点需要说明!!该加密方式有 长度 限制的!!
//##############################################################################
//RSA的加密函数
public string RSAEncrypt(string xmlPublicKey,string m_strEncryptString )
{
try
{
byte[] PlainTextBArray;
byte[] CypherTextBArray;
string Result;
System.Security.Cryptography.RSACryptoServiceProvider rsa=new RSACryptoServiceProvider();
rsa.FromXmlString(xmlPublicKey);
PlainTextBArray = (new UnicodeEncoding()).GetBytes(m_strEncryptString);
CypherTextBArray = rsa.Encrypt(PlainTextBArray, false);
Result=Convert.ToBase64String(CypherTextBArray);
return Result;
}
catch(Exception ex)
{
throw ex;
}
}
//RSA的加密函数
public string RSAEncrypt(string xmlPublicKey,byte[] EncryptString )
{
try
{
byte[] CypherTextBArray;
string Result;
System.Security.Cryptography.RSACryptoServiceProvider rsa=new RSACryptoServiceProvider();
rsa.FromXmlString(xmlPublicKey);
CypherTextBArray = rsa.Encrypt(EncryptString, false);
Result=Convert.ToBase64String(CypherTextBArray);
return Result;
}
catch(Exception ex)
{
throw ex;
}
}
#endregion
#region RSA的解密函数
//RSA的解密函数
public string RSADecrypt(string xmlPrivateKey, string m_strDecryptString )
{
try
{
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;
}
catch(Exception ex)
{
throw ex;
}
}
//RSA的解密函数
public string RSADecrypt(string xmlPrivateKey, byte[] DecryptString )
{
try
{
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;
}
catch(Exception ex)
{
throw ex;
}
}
#endregion
#endregion
#region RSA数字签名
#region 获取Hash描述表
//获取Hash描述表
public bool GetHash(string m_strSource, ref byte[] HashData)
{
try
{
//从字符串中取得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;
}
catch(Exception ex)
{
throw ex;
}
}
//获取Hash描述表
public bool GetHash(string m_strSource, ref string strHashData)
{
try
{
//从字符串中取得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;
}
catch(Exception ex)
{
throw ex;
}
}
//获取Hash描述表
public bool GetHash(System.IO.FileStream objFile, ref byte[] HashData)
{
try
{
//从文件中取得Hash描述
System.Security.Cryptography.HashAlgorithm MD5 = System.Security.Cryptography.HashAlgorithm.Create("MD5");
HashData = MD5.ComputeHash(objFile);
objFile.Close();
return true;
}
catch(Exception ex)
{
throw ex;
}
}
//获取Hash描述表
public bool GetHash(System.IO.FileStream objFile, ref string strHashData)
{
try
{
//从文件中取得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;
}
catch(Exception ex)
{
throw ex;
}
}
#endregion
#region RSA签名
//RSA签名
public bool SignatureFormatter(string p_strKeyPrivate, byte[] HashbyteSignature, ref byte[] EncryptedSignatureData)
{
try
{
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;
}
catch(Exception ex)
{
throw ex;
}
}
//RSA签名
public bool SignatureFormatter(string p_strKeyPrivate, byte[] HashbyteSignature, ref string m_strEncryptedSignatureData)
{
try
{
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;
}
catch(Exception ex)
{
throw ex;
}
}
//RSA签名
public bool SignatureFormatter(string p_strKeyPrivate, string m_strHashbyteSignature, ref byte[] EncryptedSignatureData)
{
try
{
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;
}
catch(Exception ex)
{
throw ex;
}
}
//RSA签名
public bool SignatureFormatter(string p_strKeyPrivate, string m_strHashbyteSignature, ref string m_strEncryptedSignatureData)
{
try
{
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;
}
catch(Exception ex)
{
throw ex;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -