首页 > 代码库 > RSA算法
RSA算法
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
public class RSA
{
public static string privateKey = "1kmeYVlbhShyZuk=</D></RSAKeyValue>";自己生成
public static string publicKey = "</RSAKeyValue>";公
//-------------------------------------
private RSACryptoServiceProvider rsacryptoServiceProvider_0 = new RSACryptoServiceProvider();
public void rsakey(string string_0)
{
this.rsacryptoServiceProvider_0.FromXmlString(string_0);
}
public byte[] EncryptByPublicKey(string dataStr)
{
//取得公钥参数
RSAParameters rsaparameters = this.rsacryptoServiceProvider_0.ExportParameters(true);
byte[] keyN = rsaparameters.Modulus;
byte[] keyE = rsaparameters.D;
//大整数N
BigInteger biN = new BigInteger(keyN);
//公钥大素数
BigInteger biE = new BigInteger(keyE);
//加密
return EncryptString(dataStr, biE, biN);
}
/// <summary>
/// 加密字符串
/// </summary>
/// <param name="dataStr">待加密字符串</param>
/// <param name="keyNmu">密钥大素数</param>
/// <param name="nNum">大整数N</param>
/// <returns>加密结果</returns>
private byte[] EncryptString(string dataStr, BigInteger keyNum, BigInteger nNum)
{
byte[] bytes = System.Text.Encoding.UTF8.GetBytes(dataStr);
int len = bytes.Length;
int len1 = 0;
int blockLen = 0;
if ((len % 128) == 0)
len1 = len / 128;
else
len1 = len / 128 + 1;
List<byte> tempbytes = new List<byte>();
for (int i = 0; i < len1; i++)
{
if (len >= 128)
{
blockLen = 128;
}
else
{
blockLen = len;
}
byte[] oText = new byte[blockLen];
Array.Copy(bytes, i * 128, oText, 0, blockLen);
string res = Encoding.UTF8.GetString(oText);
BigInteger biText = new BigInteger(oText);
BigInteger biEnText = biText.modPow(keyNum, nNum);
//补位
byte[] testbyte = null;
string resultStr = biEnText.ToHexString();
if (resultStr.Length < 256)
{
while (resultStr.Length != 256)
{
resultStr = "0" + resultStr;
}
}
byte[] returnBytes = new byte[128];
for (int j = 0; j < returnBytes.Length; j++)
returnBytes[j] = Convert.ToByte(resultStr.Substring(j * 2, 2), 16);
tempbytes.AddRange(returnBytes);
len -= blockLen;
}
return tempbytes.ToArray();
}
/// <summary>
/// 解密字符数组
/// </summary>
/// <param name="dataBytes">待解密字符数组</param>
/// <param name="KeyNum">密钥大素数</param>
/// <param name="nNum">大整数N</param>
/// <returns>解密结果</returns>
private string DecryptBytes(byte[] dataBytes, BigInteger KeyNum, BigInteger nNum)
{
int len = dataBytes.Length;
int len1 = 0;
int blockLen = 0;
if (len % 128 == 0)
{
len1 = len / 128;
}
else
{
len1 = len / 128 + 1;
}
List<byte> tempbytes = new List<byte>();
for (int i = 0; i < len1; i++)
{
if (len >= 128)
{
blockLen = 128;
}
else
{
blockLen = len;
}
byte[] oText = new byte[blockLen];
Array.Copy(dataBytes, i * 128, oText, 0, blockLen);
BigInteger biText = new BigInteger(oText);
BigInteger biEnText = biText.modPow(KeyNum, nNum);
byte[] testbyte = biEnText.getBytes();
string str = Encoding.UTF8.GetString(testbyte);
tempbytes.AddRange(testbyte);
len -= blockLen;
}
return System.Text.Encoding.UTF8.GetString(tempbytes.ToArray());
}
/// <summary>
/// 通过私钥解密
/// </summary>
/// <param name="dataBytes">待解密字符数组</param>
/// <returns>解密结果</returns>
public string DecryptByPrivateKey(byte[] dataBytes)
{
//取得私钥参数
RSAParameters rsaparameters = rsacryptoServiceProvider_0.ExportParameters(false);
byte[] keyN = rsaparameters.Modulus;
byte[] keyD = rsaparameters.Exponent;
//大整数N
BigInteger biN = new BigInteger(keyN);
//私钥大素数
BigInteger biD = new BigInteger(keyD);
//解密
return DecryptBytes(dataBytes, biD, biN);
}
}
RSA算法