using System;
using System.Security.Cryptography;
using System.Text;
using utility;
using System.Numerics;
class RSACSPSample
{
static void Main()
{
try
{
string Encdata = "967118D3B0C04E5C9B63AF832E9FC25373F6A9F636ABD01E871331320D071F3D67752645FCA52156064DD3DCA61BCD6310F29122E479E58D317FCE2841B3D1B38BB841599044C237567AB4EE5832D6BFF9FBBA691344DD15A759A0F1AF6DF880CE5F26354DE368E5DCE3A314FCBEDACB361E31A3AAE5ADD98672C65A707B1F0B5173D4C1CD6A22FBA1403AF63C7EE528";
string Pk = "A3767ABD1B6AA69D7F3FBF28C092DE9ED1E658BA5F0909AF7A1CCD907373B7210FDEB16287BA8E78E1529F443976FD27F991EC67D95E5F4E96B127CAB2396A94D6E45CDA44CA4C4867570D6B07542F8D4BF9FF97975DB9891515E66F525D2B3CBEB6D662BFB6C3F338E93B02142BFC44173A3764C56AADD202075B26DC2F9F7D7AE74BD7D00FD05EE430032663D27A57";
Console.WriteLine("data:"+Encdata+"\n pk:"+Pk+"\n");
Console.WriteLine("CA:" + RSARecover(Encdata, "03", Pk) + "\n");
Console.WriteLine("HASH="+SHA1("02621081FF122100030801019001AE1940C175DF1CF3904C474EC6847CFE77DAC9BEC5B0245E3172950AE573A49F654E237F8B0E44C9B1B1F39C012CEEAC10077033A978773FE0DBA05B72FEE8F6949398887975DA8A59BDF763E4447209A3B3C39B8231B99DFBBA1FEEF83F8E164322A22405840C27D35916EC2FD148B8E9D70F16ACA237B448F96691922C1FF1689E9512F46392CA9FE2DE46F173FA1D03") + "\n");
string PrivateKey, PubKEY;
int length = 1408;
if (GEN_Key(length, out PrivateKey, out PubKEY) == true)
{
Console.WriteLine("PrivateKey:" + PrivateKey + "\n PubKEY:" + PubKEY + "\n");
}
RSACryptoServiceProvider RSA = new RSACryptoServiceProvider(length);
RSAParameters RSAKeyInfo;
RSA.ImportCspBlob(strToHexByte(PrivateKey));
RSAKeyInfo = RSA.ExportParameters(true);
Console.WriteLine("D:" + byteToHexStr(RSAKeyInfo.D) + "\n ");
Console.WriteLine("DP:" + byteToHexStr(RSAKeyInfo.DP) + "\n ");
Console.WriteLine("DQ:" + byteToHexStr(RSAKeyInfo.DQ) + "\n ");
Console.WriteLine("E:" + byteToHexStr(RSAKeyInfo.Exponent) + "\n ");
Console.WriteLine("Dinv:" + byteToHexStr(RSAKeyInfo.InverseQ) + "\n ");
Console.WriteLine("N:" + byteToHexStr(RSAKeyInfo.Modulus) + "\n ");
Console.WriteLine("P:" + byteToHexStr(RSAKeyInfo.P) + "\n ");
Console.WriteLine("Q:" + byteToHexStr(RSAKeyInfo.Q) + "\n ");
}
catch (ArgumentNullException)
{
//Catch this exception in case the encryption did
//not succeed.
Console.WriteLine("Encryption failed.");
}
}
static private string RSARecover(string message, string E, string N)
{
BigInteger ORGdata, PKint, CAint, Eint;
ORGdata = System.Numerics.BigInteger.Parse("0" + message, System.Globalization.NumberStyles.HexNumber);
PKint = System.Numerics.BigInteger.Parse("0" + N, System.Globalization.NumberStyles.HexNumber);
Eint = System.Numerics.BigInteger.Parse("0" + E, System.Globalization.NumberStyles.HexNumber);
CAint= System.Numerics.BigInteger.ModPow(ORGdata, Eint, PKint);
return CAint.ToString("X");
}
static private string SHA1(string input)
{
byte[] result;
SHA1 sha = new SHA1CryptoServiceProvider();
result = sha.ComputeHash(strToHexByte(input));
return byteToHexStr(result);
}
/// <summary>
///
/// </summary>
/// <param name="priKey">私钥</param>
/// <param name="pubKey">公钥</param>
/// <returns>ERROR -1:key生成出错
/// NOERROR 0:key生成成功
/// </returns>
static public bool GEN_Key(int length, out string priKey, out string pubKey)
{
priKey = null;
pubKey = null;
//OAEP padding is only available on Microsoft Windows XP or later
try
{
RSACryptoServiceProvider RSA = new RSACryptoServiceProvider(length);
pubKey = byteToHexStr(RSA.ExportCspBlob(false));
priKey = byteToHexStr(RSA.ExportCspBlob(true));
RSA.Clear();
}
catch (Exception e)
{
Console.WriteLine(e.Message);
return false;
}
return true;
}
public static byte[] strToHexByte(string hexString)
{
hexString = hexString.Replace(" ", "");
if ((hexString.Length % 2) != 0)
hexString += " ";
byte[] returnBytes = new byte[hexString.Length / 2];
for (int i = 0; i < returnBytes.Length; i++)
returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16);
return returnBytes;
}
/// <summary>
/// 字节数组转16进制字符串
/// </summary>
/// <param name="bytes"></param>
/// <returns></returns>
public static string byteToHexStr(byte[] bytes)
{
string returnStr = "";
if (bytes != null)
{
for (int i = 0; i < bytes.Length; i++)
{
returnStr += bytes[i].ToString("X2");
}
}
return returnStr;
}
}