You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
176 lines
6.3 KiB
176 lines
6.3 KiB
using System;
|
|
using System.Collections.Generic;
|
|
using System.Text;
|
|
|
|
using System.Security.Cryptography;
|
|
|
|
namespace Sog.Crypto
|
|
{
|
|
/// <summary>
|
|
/// mono(.NetFramework 2.0) crypto api 和 BouncyCastle兼容性处理
|
|
/// </summary>
|
|
public static class CryptoConvertUtils
|
|
{
|
|
/*
|
|
/// <summary>
|
|
/// 根据mod,exp生成publickey
|
|
/// </summary>
|
|
/// <param name="modulus"></param>
|
|
/// <param name="exponent"></param>
|
|
/// <returns></returns>
|
|
/// RSAParameters
|
|
public static RsaKeyParameters GetRsaPublicKey(byte[] modulus, byte[] exponent)
|
|
{
|
|
RSAParameters
|
|
return new RsaKeyParameters(
|
|
false,
|
|
new BigInteger(1, modulus),
|
|
new BigInteger(1, exponent));
|
|
}
|
|
|
|
/// <summary>
|
|
/// 公钥解密
|
|
/// </summary>
|
|
/// <param name="encryptedData"></param>
|
|
/// <param name="publicParameters"></param>
|
|
/// <returns></returns>
|
|
public static byte[] DecryptWithPublicKey(byte[] encryptedData, RsaKeyParameters publicParameters)
|
|
{
|
|
IAsymmetricBlockCipher eng = new Pkcs1Encoding(new RsaEngine());
|
|
eng.Init(false, publicParameters);
|
|
|
|
byte[] data = eng.ProcessBlock(encryptedData, 0, encryptedData.Length);
|
|
|
|
return data;
|
|
}
|
|
|
|
|
|
public static byte[] DecryptWithPublicKey(byte[] encryptedData, byte[] modulus, byte[] exponent)
|
|
{
|
|
RsaKeyParameters publicParameters = GetRsaPublicKey(modulus,exponent);
|
|
|
|
return DecryptWithPublicKey(encryptedData, publicParameters);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 公钥加密
|
|
/// </summary>
|
|
/// <param name="data"></param>
|
|
/// <param name="publicParameters">公钥参数</param>
|
|
/// <returns>加密后的数据</returns>
|
|
public static byte[] EncryptWithPublicKey(byte[] data, RsaKeyParameters publicParameters)
|
|
{
|
|
IAsymmetricBlockCipher eng = new Pkcs1Encoding(new RsaEngine());
|
|
eng.Init(true, publicParameters);
|
|
|
|
byte[] encryptedData = eng.ProcessBlock(data, 0, data.Length);
|
|
|
|
return encryptedData;
|
|
}
|
|
|
|
public static byte[] EncryptWithPublicKey(byte[] data, byte[] modulus, byte[] exponent)
|
|
{
|
|
RsaKeyParameters publicParameters = GetRsaPublicKey(modulus, exponent);
|
|
|
|
return EncryptWithPublicKey(data, publicParameters);
|
|
}
|
|
*/
|
|
|
|
/// <summary>
|
|
/// 对ExportCspBlob(false)方法到处的私钥进行解析,提取私钥参数
|
|
/// </summary>
|
|
/// <param name="cspblobPublicKey">RSA.ExportCspBlob(false)得到的包含私钥信息</param>
|
|
/// <returns>公钥模数</returns>
|
|
public static byte[] PublicKeyResolve(byte[] cspblobPublicKey)
|
|
{
|
|
byte[] modulus = new byte[128];
|
|
Array.Reverse(cspblobPublicKey, 0, cspblobPublicKey.Length);
|
|
Buffer.BlockCopy(cspblobPublicKey, 0, modulus, 0, 128);
|
|
return modulus;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 对ExportCspBlob(true)方法到处的私钥进行解析,提取私钥参数
|
|
/// </summary>
|
|
/// <param name="cspblobPrivateKey">RSA.ExportCspBlob(true)得到的包含私钥信息</param>
|
|
/// <returns>私钥参数</returns>
|
|
public static Dictionary<string, byte[]> PrivateKeyResolve(byte[] cspblobPrivateKey)
|
|
{
|
|
Dictionary<string, byte[]> privateKeyParameters = new Dictionary<string, byte[]>();
|
|
|
|
Array.Reverse(cspblobPrivateKey, 0, cspblobPrivateKey.Length);
|
|
int offset = 0;
|
|
byte[] part = new byte[128];
|
|
Buffer.BlockCopy(cspblobPrivateKey, offset, part, offset, part.Length);
|
|
privateKeyParameters.Add("D", part);
|
|
|
|
offset += part.Length;
|
|
part = new byte[64];
|
|
Buffer.BlockCopy(cspblobPrivateKey, offset, part, 0, part.Length);
|
|
privateKeyParameters.Add("INVERSEQ", part);
|
|
|
|
offset += part.Length;
|
|
part = new byte[64];
|
|
Buffer.BlockCopy(cspblobPrivateKey, offset, part, 0, part.Length);
|
|
privateKeyParameters.Add("DQ", part);
|
|
|
|
offset += part.Length;
|
|
part = new byte[64];
|
|
Buffer.BlockCopy(cspblobPrivateKey, offset, part, 0, part.Length);
|
|
privateKeyParameters.Add("DP", part);
|
|
|
|
offset += part.Length;
|
|
part = new byte[64];
|
|
Buffer.BlockCopy(cspblobPrivateKey, offset, part, 0, part.Length);
|
|
privateKeyParameters.Add("Q", part);
|
|
|
|
offset += part.Length;
|
|
part = new byte[64];
|
|
Buffer.BlockCopy(cspblobPrivateKey, offset, part, 0, part.Length);
|
|
privateKeyParameters.Add("P", part);
|
|
|
|
offset += part.Length;
|
|
part = new byte[128];
|
|
Buffer.BlockCopy(cspblobPrivateKey, offset, part, 0, part.Length);
|
|
privateKeyParameters.Add("MODULUS", part);
|
|
return privateKeyParameters;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 数组转成16进制字符串
|
|
/// </summary>
|
|
/// <param name="bytes"></param>
|
|
/// <returns></returns>
|
|
public static string ToHexString(byte[] bytes) // 0xae00cf => "AE00CF "
|
|
{
|
|
string hexString = string.Empty;
|
|
|
|
if (bytes != null)
|
|
{
|
|
StringBuilder strB = new StringBuilder();
|
|
|
|
for (int i = 0; i < bytes.Length; i++)
|
|
{
|
|
strB.Append(bytes[i].ToString("X2"));
|
|
}
|
|
hexString = strB.ToString();
|
|
}
|
|
return hexString;
|
|
}
|
|
|
|
public static byte[] EncryptWithPublicKeyWithChewKeongTANBigInteger(byte[] data, byte[] modulus, byte[] exponent)
|
|
{
|
|
ChewKeongTAN.BigInteger bi_n = new ChewKeongTAN.BigInteger(modulus);
|
|
ChewKeongTAN.BigInteger bi_e = new ChewKeongTAN.BigInteger(exponent);
|
|
|
|
ChewKeongTAN.BigInteger biText = new ChewKeongTAN.BigInteger(data);
|
|
ChewKeongTAN.BigInteger biEnText = biText.modPow(bi_e, bi_n);
|
|
|
|
|
|
byte[] outbytes = biEnText.getBytes();
|
|
|
|
//TraceLog.Trace("EncryptWithPublicKeyWithChewKeongTANBigInteger entext {0}", biEnText.ToString());
|
|
return outbytes;
|
|
}
|
|
}
|
|
}
|
|
|