加密---非对称 加密CryptographRSA

论坛 期权论坛 脚本     
匿名网站用户   2020-12-21 09:35   85   0
using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;


namespace WLJTLongtengGenesAndSupreme.Security
{
public class CryptographRSA
{
/// <summary>
/// 产生密钥和公钥
/// </summary>
/// <param name="xmlKeys">产生的公共秘钥</param>
/// <param name="xmlPublicKey">产生的私有秘钥</param>
public void RSAKey(out string xmlKeys, out string xmlPublicKey)
{
try
{
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
xmlKeys = rsa.ToXmlString(true);
xmlPublicKey = rsa.ToXmlString(false);
}
catch (Exception ex)
{
throw ex;
}
}


/// <summary>
/// RSA的加密函数
/// </summary>
/// <param name="xmlPublicKey">公共秘钥</param>
/// <param name="m_strEncryptString">待加密的字符串文本</param>
/// <returns>加密完后的字符串</returns>
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;
}
}


/// <summary>
/// RSA的加密函数
/// </summary>
/// <param name="xmlPublicKey">公共秘钥</param>
/// <param name="EncryptString">待加密的字节数组</param>
/// <returns>加密完后的字符串</returns>
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;
}
}




/// <summary>
/// RSA的解密函数
/// </summary>
/// <param name="xmlPrivateKey">私有秘钥</param>
/// <param name="m_strDecryptString">待解密的字符串文本</param>
/// <returns></returns>
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);


//XP版本以上版本Decrypt(byte[] rgb, bool fOAEP)的第二个参数为true
DypherTextBArray = rsa.Decrypt(PlainTextBArray, false);
Result = (new UnicodeEncoding()).GetString(DypherTextBArray);
return Result;
}
catch (Exception ex)
{
throw ex;
}
}


/// <summary>
/// RSA的解密函数
/// </summary>
/// <param name="xmlPrivateKey">私有秘钥</param>
/// <param name="DecryptString">待解密的字符数组</param>
/// <returns>解密后的字符串</returns>
public string RSADecrypt(string xmlPrivateKey, byte[] DecryptString)
{
try
{
byte[] DypherTextBArray;
string Result;
System.Security.Cryptography.RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
rsa.FromXmlString(xmlPrivateKey);
//XP版本以上版本Decrypt(byte[] rgb, bool fOAEP)的第二个参数为true
DypherTextBArray = rsa.Decrypt(DecryptString, false);
Result = (new UnicodeEncoding()).GetString(DypherTextBArray);
return Result;
}
catch (Exception ex)
{
throw ex;
}
}


}
}
分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:1136255
帖子:227251
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP