?? 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 + -