using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
namespace Gd.Security
{
/// <summary>
/// Rsa 对称加密类
/// </summary>
// Token: 0x02000017 RID: 23
public sealed class GdRsa
{
/// <summary>
/// 针对Silverlight RSA解密
/// </summary>
/// <param name="encryptedStr">加密字符串</param>
/// <param name="privateXmlKey">密钥</param>
/// <param name="errMsg">错误消息</param>
/// <param name="dwKeySize">要使用的密钥的大小(以位为单位)</param>
/// <returns></returns>
// Token: 0x06000090 RID: 144 RVA: 0x000084C4 File Offset: 0x000066C4
public static string DecryptFormXmlKey(string encryptedStr, string privateXmlKey, ref string errMsg, int dwKeySize = 1024)
{
string result = string.Empty;
using (RSACryptoServiceProvider rsacryptoServiceProvider = new RSACryptoServiceProvider(dwKeySize))
{
try
{
rsacryptoServiceProvider.FromXmlString(privateXmlKey);
byte[] rgb = Convert.FromBase64String(encryptedStr);
byte[] bytes = rsacryptoServiceProvider.Decrypt(rgb, true);
result = Encoding.UTF8.GetString(bytes);
}
catch (Exception ex)
{
errMsg = ex.ToString();
}
finally
{
rsacryptoServiceProvider.PersistKeyInCsp = false;
}
}
return result;
}
/// <summary>
/// 针对Silverlight RSA加密
/// </summary>
/// <param name="encryStr">字符串</param>
/// <param name="publicXmlKey">公钥</param>
/// <param name="errMsg">错误消息</param>
/// <param name="dwKeySize">要使用的密钥的大小(以位为单位)</param>
/// <returns></returns>
// Token: 0x06000091 RID: 145 RVA: 0x0000856C File Offset: 0x0000676C
public static string EncryptFormXmlKey(string encryStr, string publicXmlKey, ref string errMsg, int dwKeySize = 1024)
{
string result = string.Empty;
using (RSACryptoServiceProvider rsacryptoServiceProvider = new RSACryptoServiceProvider(dwKeySize))
{
try
{
rsacryptoServiceProvider.FromXmlString(publicXmlKey);
byte[] inArray = rsacryptoServiceProvider.Encrypt(Encoding.UTF8.GetBytes(encryStr), true);
result = Convert.ToBase64String(inArray);
}
catch (Exception ex)
{
errMsg = ex.ToString();
}
finally
{
rsacryptoServiceProvider.PersistKeyInCsp = false;
}
}
return result;
}
/// <summary>
/// 加密字符串
/// </summary>
/// <param name="resData">字符串</param>
/// <param name="publicKey">密钥</param>
/// <param name="errMsg">错误消息</param>
/// <returns></returns>
// Token: 0x06000092 RID: 146 RVA: 0x00008610 File Offset: 0x00006810
public static string EncryptData(string resData, string publicKey, ref string errMsg)
{
string result = null;
try
{
byte[] bytes = Encoding.UTF8.GetBytes(resData);
result = GdRsa.Encrypt(bytes, publicKey, "UTF-8");
}
catch (Exception ex)
{
errMsg = ex.ToString();
}
return result;
}
/// <summary>
/// 解密字符串
/// </summary>
/// <param name="resData">加密数据</param>
/// <param name="privateKey">密钥</param>
/// <param name="errMsg">错误消息</param>
/// <returns></returns>
// Token: 0x06000093 RID: 147 RVA: 0x00008660 File Offset: 0x00006860
public static string DecryptData(string resData, string privateKey, ref string errMsg)
{
string text = null;
try
{
byte[] array = Convert.FromBase64String(resData);
for (int i = 0; i < array.Length / 128; i++)
{
byte[] array2 = new byte[128];
for (int j = 0; j < 128; j++)
{
array2[j] = array[j + 128 * i];
}
text += GdRsa.Decrypt(array2, privateKey, "UTF-8", ref errMsg);
if (!string.IsNullOrEmpty(errMsg))
{
break;
}
}
}
catch (Exception ex)
{
errMsg = string.Concat(new string[]
{
"RSA解密出错,详情:",
ex.ToString(),
"解密字符串:",
resData,
",解密私钥:",
privateKey
});
}
return text;
}
/// <summary>
/// 签名,失败抛出异常
/// </summary>
/// <param name="content">待签名字符串</param>
/// <param name="privateKey">私钥</param>
/// <param name="input_charset">编码格式</param>
/// <param name="errMsg">错误信息</param>
/// <returns>签名后字符串</returns>
// Token: 0x06000094 RID: 148 RVA: 0x00008754 File Offset: 0x00006954
public static string Sign(string content, string privateKey, ref string errMsg, string input_charset = "UTF-8")
{
try
{
byte[] bytes = Encoding.GetEncoding(input_charset).GetBytes(content);
RSACryptoServiceProvider rsacryptoServiceProvider = GdRsa.DecodePemPrivateKey(privateKey, ref errMsg);
if (string.IsNullOrEmpty(errMsg))
{
SHA1 halg = new SHA1CryptoServiceProvider();
byte[] inArray = rsacryptoServiceProvider.SignData(bytes, halg);
string asciiString = Convert.ToBase64String(inArray);
return GdRsa.StringToHex(asciiString);
}
}
catch (Exception ex)
{
errMsg = "DecodePrivateKeyInfo:" + ex.ToString();
return string.Empty;
}
return string.Empty;
}
/// <summary>
/// 验签,失败抛出异常
/// </summary>
/// <param name="content">待验签字符串</param>
/// <param name="signedString">签名</param>
/// <param name="publicKey">公钥</param>
/// <param name="input_charset">编码格式</param>
/// <param name="errMsg">错误信息</param>
/// <returns>true(通过),false(不通过)</returns>
// Token: 0x06000095 RID: 149 RVA: 0x000087EC File Offset: 0x000069EC
public static bool Verify(string content, string signedString, string publicKey, ref string errMsg, string input_charset = "UTF-8")
{
bool result = false;
try
{
signedString = GdRsa.HexToString(signedString);
byte[] signature = Convert.FromBase64String(signedString);
byte[] bytes = Encoding.GetEncoding(input_charset).GetBytes(content);
RSAParameters parameters = GdRsa.ConvertFromPublicKey(publicKey);
RSACryptoServiceProvider rsacryptoServiceProvider = new RSACryptoServiceProvider();
rsacryptoServiceProvider.ImportParameters(parameters);
SHA1 halg = new SHA1CryptoServiceProvider();
result = rsacryptoServiceProvider.VerifyData(bytes, halg, signature);
return result;
}
catch (Exception ex)
{
errMsg = "Verify:" + ex.ToString();
}
return result;
}
// Token: 0x06000096 RID: 150 RVA: 0x0000887C File Offset: 0x00006A7C
private static string Encrypt(byte[] data, string publicKey, string input_charset)
{
RSACryptoServiceProvider rsa = GdRsa.DecodePemPublicKey(publicKey);
return GdRsa.Encrypt2(rsa, data);
}
// Token: 0x06000097 RID: 151 RVA: 0x0000889C File Offset: 0x00006A9C
private static string Decrypt(byte[] data, string privateKey, string input_charset, ref string errMsg)
{
string result = "";
RSACryptoServiceProvider rsa = GdRsa.DecodePemPrivateKey(privateKey, ref errMsg);
if (string.IsNullOrEmpty(errMsg))
{
result = GdRsa.Decrypt2(rsa, data);
}
return result;
}
// Token: 0x06000098 RID: 152 RVA: 0x000088D8 File Offset: 0x00006AD8
private static string Encrypt2(RSACryptoServiceProvider rsa, byte[] PlaintextData)
{
int num = rsa.KeySize / 8 - 11;
string result;
if (PlaintextData.Length <= num)
{
result = Convert.ToBase64String(rsa.Encrypt(PlaintextData, false));
}
else
{
string text;
using (MemoryStream memoryStream = new MemoryStream(PlaintextData))
{
using (MemoryStream memoryStream2 = new MemoryStream())
{
byte[] array = new byte[num];
for (int i = memoryStream.Read(array, 0, num); i > 0; i = memoryStream.Read(array, 0, num))
{
byte[] array2 = new byte[i];
Array.Copy(array, 0, array2, 0, i);
byte[] array3 = rsa.Encrypt(array2, false);
memoryStream2.Write(array3, 0, array3.Length);
}
text = Convert.ToBase64String(memoryStream2.ToArray(), Base64FormattingOptions.None);
}
}
result = text;
}
return result;
}
// Token: 0x06000099 RID: 153 RVA: 0x000089E0 File Offset: 0x00006BE0
private static string Decrypt2(RSACryptoServiceProvider rsa, byte[] CiphertextData)
{
int num = rsa.KeySize / 8;
string result;
if (CiphertextData.Length <= num)
{
result = Encoding.UTF8.GetString(rsa.Decrypt(CiphertextData, false));
}
else
{
string @string;
using (MemoryStream memoryStream = new MemoryStream(CiphertextData))
{
using (MemoryStream memoryStream2 = new MemoryStream())
{
byte[] array = new byte[num];
for (int i = memoryStream.Read(array, 0, num); i > 0; i = memoryStream.Read(array, 0, num))
{
byte[] array2 = new byte[i];
Array.Copy(array, 0, array2, 0, i);
byte[] array3 = rsa.Decrypt(array2, false);
memoryStream2.Write(array3, 0, array3.Length);
}
@string = Encoding.UTF8.GetString(memoryStream2.ToArray());
}
}
result = @string;
}
return result;
}
// Token: 0x0600009A RID: 154 RVA: 0x00008AEC File Offset: 0x00006CEC
private static RSACryptoServiceProvider DecodePemPublicKey(string pemstr)
{
byte[] array = Convert.FromBase64String(pemstr);
RSACryptoServiceProvider result;
if (array != null)
{
result = GdRsa.DecodeRSAPublicKey(array);
}
else
{
result = null;
}
return result;
}
// Token: 0x0600009B RID: 155 RVA: 0x00008B18 File Offset: 0x00006D18
private static RSACryptoServiceProvider DecodePemPrivateKey(string pemstr, ref string errMsg)
{
byte[] array = Convert.FromBase64String(pemstr);
RSACryptoServiceProvider result;
if (array != null)
{
result = GdRsa.DecodePrivateKeyInfo(array, ref errMsg);
}
else
{
result = null;
}
return result;
}
// Token: 0x0600009C RID: 156 RVA: 0x00008B58 File Offset: 0x00006D58
private static RSACryptoServiceProvider DecodePrivateKeyInfo(byte[] pkcs8, ref string errMsg)
{
BinaryReader binaryReader = null;
RSACryptoServiceProvider result;
try
{
byte[] b = new byte[]
{
48,
13,
6,
9,
42,
134,
72,
134,
247,
13,
1,
1,
1,
5,
0
};
byte[] a = new byte[15];
MemoryStream memoryStream = new MemoryStream(pkcs8);
int num = (int)memoryStream.Length;
binaryReader = new BinaryReader(memoryStream);
ushort num2 = binaryReader.ReadUInt16();
if (num2 == 33072)
{
binaryReader.ReadByte();
}
else
{
if (num2 != 33328)
{
result = null;
return result;
}
binaryReader.ReadInt16();
}
byte b2 = binaryReader.ReadByte();
if (b2 != 2)
{
result = null;
}
else
{
num2 = binaryReader.ReadUInt16();
if (num2 != 1)
{
result = null;
}
else
{
a = binaryReader.ReadBytes(15);
if (!GdRsa.CompareBytearrays(a, b))
{
result = null;
}
else
{
b2 = binaryReader.ReadByte();
if (b2 != 4)
{
result = null;
}
else
{
b2 = binaryReader.ReadByte();
if (b2 == 129)
{
binaryReader.ReadByte();
}
else if (b2 == 130)
{
binaryReader.ReadUInt16();
}
byte[] privkey = binaryReader.ReadBytes((int)((long)num - memoryStream.Position));
RSACryptoServiceProvider rsacryptoServiceProvider = GdRsa.DecodeRSAPrivateKey(privkey);
result = rsacryptoServiceProvider;
}
}
}
}
}
catch (Exception ex)
{
errMsg = "DecodePrivateKeyInfo:" + ex.ToString();
result = null;
}
finally
{
if (binaryReader != null)
{
binaryReader.Close();
}
}
return result;
}
// Token: 0x0600009D RID: 157 RVA: 0x00008D34 File Offset: 0x00006F34
private static bool CompareBytearrays(byte[] a, byte[] b)
{
bool result;
if (a.Length != b.Length)
{
result = false;
}
else
{
int num = 0;
foreach (byte b2 in a)
{
if (b2 != b[num])
{
return false;
}
num++;
}
result = true;
}
return result;
}
// Token: 0x0600009E RID: 158 RVA: 0x00008DA0 File Offset: 0x00006FA0
private static RSACryptoServiceProvider DecodeRSAPublicKey(byte[] publickey)
{
byte[] b = new byte[]
{
48,
13,
6,
9,
42,
134,
72,
134,
247,
13,
1,
1,
1,
5,
0
};
byte[] a = new byte[15];
MemoryStream input = new MemoryStream(publickey);
BinaryReader binaryReader = new BinaryReader(input);
RSACryptoServiceProvider result;
try
{
ushort num = binaryReader.ReadUInt16();
if (num == 33072)
{
binaryReader.ReadByte();
}
else
{
if (num != 33328)
{
result = null;
return result;
}
binaryReader.ReadInt16();
}
a = binaryReader.ReadBytes(15);
if (!GdRsa.CompareBytearrays(a, b))
{
result = null;
}
else
{
num = binaryReader.ReadUInt16();
if (num == 33027)
{
binaryReader.ReadByte();
}
else
{
if (num != 33283)
{
result = null;
return result;
}
binaryReader.ReadInt16();
}
byte b2 = binaryReader.ReadByte();
if (b2 != 0)
{
result = null;
}
else
{
num = binaryReader.ReadUInt16();
if (num == 33072)
{
binaryReader.ReadByte();
}
else
{
if (num != 33328)
{
result = null;
return result;
}
binaryReader.ReadInt16();
}
num = binaryReader.ReadUInt16();
byte b3 = 0;
byte b4;
if (num == 33026)
{
b4 = binaryReader.ReadByte();
}
else
{
if (num != 33282)
{
result = null;
return result;
}
b3 = binaryReader.ReadByte();
b4 = binaryReader.ReadByte();
}
byte[] array = new byte[4];
array[0] = b4;
array[1] = b3;
byte[] value = array;
int num2 = BitConverter.ToInt32(value, 0);
byte b5 = binaryReader.ReadByte();
binaryReader.BaseStream.Seek(-1L, SeekOrigin.Current);
if (b5 == 0)
{
binaryReader.ReadByte();
num2--;
}
byte[] modulus = binaryReader.ReadBytes(num2);
if (binaryReader.ReadByte() != 2)
{
result = null;
}
else
{
int count = (int)binaryReader.ReadByte();
byte[] exponent = binaryReader.ReadBytes(count);
RSACryptoServiceProvider rsacryptoServiceProvider = new RSACryptoServiceProvider();
rsacryptoServiceProvider.ImportParameters(new RSAParameters
{
Modulus = modulus,
Exponent = exponent
});
result = rsacryptoServiceProvider;
}
}
}
}
catch (Exception)
{
result = null;
}
finally
{
binaryReader.Close();
}
return result;
}
// Token: 0x0600009F RID: 159 RVA: 0x00009060 File Offset: 0x00007260
private static RSACryptoServiceProvider DecodeRSAPrivateKey(byte[] privkey)
{
MemoryStream input = new MemoryStream(privkey);
BinaryReader binaryReader = new BinaryReader(input);
RSACryptoServiceProvider result;
try
{
ushort num = binaryReader.ReadUInt16();
if (num == 33072)
{
binaryReader.ReadByte();
}
else
{
if (num != 33328)
{
result = null;
return result;
}
binaryReader.ReadInt16();
}
num = binaryReader.ReadUInt16();
if (num != 258)
{
result = null;
}
else
{
byte b = binaryReader.ReadByte();
if (b != 0)
{
result = null;
}
else
{
int integerSize = GdRsa.GetIntegerSize(binaryReader);
byte[] modulus = binaryReader.ReadBytes(integerSize);
integerSize = GdRsa.GetIntegerSize(binaryReader);
byte[] exponent = binaryReader.ReadBytes(integerSize);
integerSize = GdRsa.GetIntegerSize(binaryReader);
byte[] d = binaryReader.ReadBytes(integerSize);
integerSize = GdRsa.GetIntegerSize(binaryReader);
byte[] p = binaryReader.ReadBytes(integerSize);
integerSize = GdRsa.GetIntegerSize(binaryReader);
byte[] q = binaryReader.ReadBytes(integerSize);
integerSize = GdRsa.GetIntegerSize(binaryReader);
byte[] dp = binaryReader.ReadBytes(integerSize);
integerSize = GdRsa.GetIntegerSize(binaryReader);
byte[] dq = binaryReader.ReadBytes(integerSize);
integerSize = GdRsa.GetIntegerSize(binaryReader);
byte[] inverseQ = binaryReader.ReadBytes(integerSize);
RSACryptoServiceProvider rsacryptoServiceProvider = new RSACryptoServiceProvider();
rsacryptoServiceProvider.ImportParameters(new RSAParameters
{
Modulus = modulus,
Exponent = exponent,
D = d,
P = p,
Q = q,
DP = dp,
DQ = dq,
InverseQ = inverseQ
});
result = rsacryptoServiceProvider;
}
}
}
catch (Exception)
{
result = null;
}
finally
{
binaryReader.Close();
}
return result;
}
// Token: 0x060000A0 RID: 160 RVA: 0x00009248 File Offset: 0x00007448
private static int GetIntegerSize(BinaryReader binr)
{
byte b = binr.ReadByte();
int result;
if (b != 2)
{
result = 0;
}
else
{
b = binr.ReadByte();
int num;
if (b == 129)
{
num = (int)binr.ReadByte();
}
else if (b == 130)
{
byte b2 = binr.ReadByte();
byte b3 = binr.ReadByte();
byte[] array = new byte[4];
array[0] = b3;
array[1] = b2;
byte[] value = array;
num = BitConverter.ToInt32(value, 0);
}
else
{
num = (int)b;
}
while (binr.ReadByte() == 0)
{
num--;
}
binr.BaseStream.Seek(-1L, SeekOrigin.Current);
result = num;
}
return result;
}
// Token: 0x060000A1 RID: 161 RVA: 0x00009308 File Offset: 0x00007508
private static string ConvertStringToHex(string asciiString)
{
string text = "";
foreach (char value in asciiString)
{
int num = Convert.ToInt32(value);
text += string.Format("{0:X2}", num);
}
return text.ToUpper();
}
// Token: 0x060000A2 RID: 162 RVA: 0x00009368 File Offset: 0x00007568
private static string ConvertHexToString(string HexValue)
{
string text = "";
while (HexValue.Length > 0)
{
text += Convert.ToChar(Convert.ToUInt32(HexValue.Substring(0, 2), 16)).ToString();
HexValue = HexValue.Substring(2, HexValue.Length - 2);
}
return text;
}
// Token: 0x060000A3 RID: 163 RVA: 0x000093C8 File Offset: 0x000075C8
private static RSAParameters ConvertFromPublicKey(string pemFileConent)
{
byte[] array = Convert.FromBase64String(pemFileConent);
if (array.Length < 162)
{
throw new ArgumentException("pem file content is incorrect.");
}
byte[] array2 = new byte[128];
byte[] array3 = new byte[3];
Array.Copy(array, 29, array2, 0, 128);
Array.Copy(array, 159, array3, 0, 3);
return new RSAParameters
{
Modulus = array2,
Exponent = array3
};
}
// Token: 0x060000A4 RID: 164 RVA: 0x0000944C File Offset: 0x0000764C
private static RSAParameters ConvertFromPrivateKey(string pemFileConent)
{
byte[] array = Convert.FromBase64String(pemFileConent);
if (array.Length < 609)
{
throw new ArgumentException("pem file content is incorrect.");
}
int num = 11;
byte[] array2 = new byte[128];
Array.Copy(array, num, array2, 0, 128);
num += 128;
num += 2;
byte[] array3 = new byte[3];
Array.Copy(array, num, array3, 0, 3);
num += 3;
num += 4;
byte[] array4 = new byte[128];
Array.Copy(array, num, array4, 0, 128);
num += 128;
num += ((array[num + 1] == 64) ? 2 : 3);
byte[] array5 = new byte[64];
Array.Copy(array, num, array5, 0, 64);
num += 64;
num += ((array[num + 1] == 64) ? 2 : 3);
byte[] array6 = new byte[64];
Array.Copy(array, num, array6, 0, 64);
num += 64;
num += ((array[num + 1] == 64) ? 2 : 3);
byte[] array7 = new byte[64];
Array.Copy(array, num, array7, 0, 64);
num += 64;
num += ((array[num + 1] == 64) ? 2 : 3);
byte[] array8 = new byte[64];
Array.Copy(array, num, array8, 0, 64);
num += 64;
num += ((array[num + 1] == 64) ? 2 : 3);
byte[] array9 = new byte[64];
Array.Copy(array, num, array9, 0, 64);
return new RSAParameters
{
Modulus = array2,
Exponent = array3,
D = array4,
P = array5,
Q = array6,
DP = array7,
DQ = array8,
InverseQ = array9
};
}
/// <summary>
/// 字符串转换验签字符
/// </summary>
/// <param name="asciiString">编码字符</param>
/// <returns></returns>
// Token: 0x060000A5 RID: 165 RVA: 0x00009614 File Offset: 0x00007814
private static string StringToHex(string asciiString)
{
string text = "";
foreach (char value in asciiString)
{
int num = Convert.ToInt32(value);
text += string.Format("{0:X2}", num);
}
return text.ToUpper();
}
/// <summary>
/// 转换验签字符为字符串
/// </summary>
/// <param name="HexValue">验签字符串</param>
/// <returns></returns>
// Token: 0x060000A6 RID: 166 RVA: 0x0000967C File Offset: 0x0000787C
private static string HexToString(string HexValue)
{
string text = "";
while (HexValue.Length > 0)
{
text += Convert.ToChar(Convert.ToUInt32(HexValue.Substring(0, 2), 16)).ToString();
HexValue = HexValue.Substring(2, HexValue.Length - 2);
}
return text;
}
}
}