Files
Dec2017-Script-Dump/mscorlib/Mono/Security/Cryptography/CryptoConvert.cs
T
2026-06-04 11:42:34 +02:00

643 lines
19 KiB
C#

using System;
using System.Globalization;
using System.Security.Cryptography;
using System.Text;
namespace Mono.Security.Cryptography
{
// Token: 0x020000A1 RID: 161
internal sealed class CryptoConvert
{
// Token: 0x060008CC RID: 2252 RVA: 0x00021F80 File Offset: 0x00020180
private CryptoConvert()
{
}
// Token: 0x060008CD RID: 2253 RVA: 0x00021F88 File Offset: 0x00020188
private static int ToInt32LE(byte[] bytes, int offset)
{
return ((int)bytes[offset + 3] << 24) | ((int)bytes[offset + 2] << 16) | ((int)bytes[offset + 1] << 8) | (int)bytes[offset];
}
// Token: 0x060008CE RID: 2254 RVA: 0x00021FA8 File Offset: 0x000201A8
private static uint ToUInt32LE(byte[] bytes, int offset)
{
return (uint)(((int)bytes[offset + 3] << 24) | ((int)bytes[offset + 2] << 16) | ((int)bytes[offset + 1] << 8) | (int)bytes[offset]);
}
// Token: 0x060008CF RID: 2255 RVA: 0x00021FC8 File Offset: 0x000201C8
private static byte[] GetBytesLE(int val)
{
return new byte[]
{
(byte)(val & 255),
(byte)((val >> 8) & 255),
(byte)((val >> 16) & 255),
(byte)((val >> 24) & 255)
};
}
// Token: 0x060008D0 RID: 2256 RVA: 0x00022010 File Offset: 0x00020210
private static byte[] Trim(byte[] array)
{
for (int i = 0; i < array.Length; i++)
{
if (array[i] != 0)
{
byte[] array2 = new byte[array.Length - i];
Buffer.BlockCopy(array, i, array2, 0, array2.Length);
return array2;
}
}
return null;
}
// Token: 0x060008D1 RID: 2257 RVA: 0x00022054 File Offset: 0x00020254
public static RSA FromCapiPrivateKeyBlob(byte[] blob)
{
return CryptoConvert.FromCapiPrivateKeyBlob(blob, 0);
}
// Token: 0x060008D2 RID: 2258 RVA: 0x00022060 File Offset: 0x00020260
public static RSA FromCapiPrivateKeyBlob(byte[] blob, int offset)
{
if (blob == null)
{
throw new ArgumentNullException("blob");
}
if (offset >= blob.Length)
{
throw new ArgumentException("blob is too small.");
}
RSAParameters rsaparameters = default(RSAParameters);
try
{
if (blob[offset] != 7 || blob[offset + 1] != 2 || blob[offset + 2] != 0 || blob[offset + 3] != 0 || CryptoConvert.ToUInt32LE(blob, offset + 8) != 843141970U)
{
throw new CryptographicException("Invalid blob header");
}
int num = CryptoConvert.ToInt32LE(blob, offset + 12);
byte[] array = new byte[4];
Buffer.BlockCopy(blob, offset + 16, array, 0, 4);
Array.Reverse(array);
rsaparameters.Exponent = CryptoConvert.Trim(array);
int num2 = offset + 20;
int num3 = num >> 3;
rsaparameters.Modulus = new byte[num3];
Buffer.BlockCopy(blob, num2, rsaparameters.Modulus, 0, num3);
Array.Reverse(rsaparameters.Modulus);
num2 += num3;
int num4 = num3 >> 1;
rsaparameters.P = new byte[num4];
Buffer.BlockCopy(blob, num2, rsaparameters.P, 0, num4);
Array.Reverse(rsaparameters.P);
num2 += num4;
rsaparameters.Q = new byte[num4];
Buffer.BlockCopy(blob, num2, rsaparameters.Q, 0, num4);
Array.Reverse(rsaparameters.Q);
num2 += num4;
rsaparameters.DP = new byte[num4];
Buffer.BlockCopy(blob, num2, rsaparameters.DP, 0, num4);
Array.Reverse(rsaparameters.DP);
num2 += num4;
rsaparameters.DQ = new byte[num4];
Buffer.BlockCopy(blob, num2, rsaparameters.DQ, 0, num4);
Array.Reverse(rsaparameters.DQ);
num2 += num4;
rsaparameters.InverseQ = new byte[num4];
Buffer.BlockCopy(blob, num2, rsaparameters.InverseQ, 0, num4);
Array.Reverse(rsaparameters.InverseQ);
num2 += num4;
rsaparameters.D = new byte[num3];
if (num2 + num3 + offset <= blob.Length)
{
Buffer.BlockCopy(blob, num2, rsaparameters.D, 0, num3);
Array.Reverse(rsaparameters.D);
}
}
catch (Exception ex)
{
throw new CryptographicException("Invalid blob.", ex);
}
RSA rsa = RSA.Create();
rsa.ImportParameters(rsaparameters);
return rsa;
}
// Token: 0x060008D3 RID: 2259 RVA: 0x000222C4 File Offset: 0x000204C4
public static DSA FromCapiPrivateKeyBlobDSA(byte[] blob)
{
return CryptoConvert.FromCapiPrivateKeyBlobDSA(blob, 0);
}
// Token: 0x060008D4 RID: 2260 RVA: 0x000222D0 File Offset: 0x000204D0
public static DSA FromCapiPrivateKeyBlobDSA(byte[] blob, int offset)
{
if (blob == null)
{
throw new ArgumentNullException("blob");
}
if (offset >= blob.Length)
{
throw new ArgumentException("blob is too small.");
}
DSAParameters dsaparameters = default(DSAParameters);
try
{
if (blob[offset] != 7 || blob[offset + 1] != 2 || blob[offset + 2] != 0 || blob[offset + 3] != 0 || CryptoConvert.ToUInt32LE(blob, offset + 8) != 844321604U)
{
throw new CryptographicException("Invalid blob header");
}
int num = CryptoConvert.ToInt32LE(blob, offset + 12);
int num2 = num >> 3;
int num3 = offset + 16;
dsaparameters.P = new byte[num2];
Buffer.BlockCopy(blob, num3, dsaparameters.P, 0, num2);
Array.Reverse(dsaparameters.P);
num3 += num2;
dsaparameters.Q = new byte[20];
Buffer.BlockCopy(blob, num3, dsaparameters.Q, 0, 20);
Array.Reverse(dsaparameters.Q);
num3 += 20;
dsaparameters.G = new byte[num2];
Buffer.BlockCopy(blob, num3, dsaparameters.G, 0, num2);
Array.Reverse(dsaparameters.G);
num3 += num2;
dsaparameters.X = new byte[20];
Buffer.BlockCopy(blob, num3, dsaparameters.X, 0, 20);
Array.Reverse(dsaparameters.X);
num3 += 20;
dsaparameters.Counter = CryptoConvert.ToInt32LE(blob, num3);
num3 += 4;
dsaparameters.Seed = new byte[20];
Buffer.BlockCopy(blob, num3, dsaparameters.Seed, 0, 20);
Array.Reverse(dsaparameters.Seed);
num3 += 20;
}
catch (Exception ex)
{
throw new CryptographicException("Invalid blob.", ex);
}
DSA dsa = DSA.Create();
dsa.ImportParameters(dsaparameters);
return dsa;
}
// Token: 0x060008D5 RID: 2261 RVA: 0x000224A8 File Offset: 0x000206A8
public static byte[] ToCapiPrivateKeyBlob(RSA rsa)
{
RSAParameters rsaparameters = rsa.ExportParameters(true);
int num = rsaparameters.Modulus.Length;
byte[] array = new byte[20 + (num << 2) + (num >> 1)];
array[0] = 7;
array[1] = 2;
array[5] = 36;
array[8] = 82;
array[9] = 83;
array[10] = 65;
array[11] = 50;
byte[] bytesLE = CryptoConvert.GetBytesLE(num << 3);
array[12] = bytesLE[0];
array[13] = bytesLE[1];
array[14] = bytesLE[2];
array[15] = bytesLE[3];
int num2 = 16;
int i = rsaparameters.Exponent.Length;
while (i > 0)
{
array[num2++] = rsaparameters.Exponent[--i];
}
num2 = 20;
byte[] array2 = rsaparameters.Modulus;
int num3 = array2.Length;
Array.Reverse(array2, 0, num3);
Buffer.BlockCopy(array2, 0, array, num2, num3);
num2 += num3;
array2 = rsaparameters.P;
num3 = array2.Length;
Array.Reverse(array2, 0, num3);
Buffer.BlockCopy(array2, 0, array, num2, num3);
num2 += num3;
array2 = rsaparameters.Q;
num3 = array2.Length;
Array.Reverse(array2, 0, num3);
Buffer.BlockCopy(array2, 0, array, num2, num3);
num2 += num3;
array2 = rsaparameters.DP;
num3 = array2.Length;
Array.Reverse(array2, 0, num3);
Buffer.BlockCopy(array2, 0, array, num2, num3);
num2 += num3;
array2 = rsaparameters.DQ;
num3 = array2.Length;
Array.Reverse(array2, 0, num3);
Buffer.BlockCopy(array2, 0, array, num2, num3);
num2 += num3;
array2 = rsaparameters.InverseQ;
num3 = array2.Length;
Array.Reverse(array2, 0, num3);
Buffer.BlockCopy(array2, 0, array, num2, num3);
num2 += num3;
array2 = rsaparameters.D;
num3 = array2.Length;
Array.Reverse(array2, 0, num3);
Buffer.BlockCopy(array2, 0, array, num2, num3);
return array;
}
// Token: 0x060008D6 RID: 2262 RVA: 0x00022690 File Offset: 0x00020890
public static byte[] ToCapiPrivateKeyBlob(DSA dsa)
{
DSAParameters dsaparameters = dsa.ExportParameters(true);
int num = dsaparameters.P.Length;
byte[] array = new byte[16 + num + 20 + num + 20 + 4 + 20];
array[0] = 7;
array[1] = 2;
array[5] = 34;
array[8] = 68;
array[9] = 83;
array[10] = 83;
array[11] = 50;
byte[] bytesLE = CryptoConvert.GetBytesLE(num << 3);
array[12] = bytesLE[0];
array[13] = bytesLE[1];
array[14] = bytesLE[2];
array[15] = bytesLE[3];
int num2 = 16;
byte[] array2 = dsaparameters.P;
Array.Reverse(array2);
Buffer.BlockCopy(array2, 0, array, num2, num);
num2 += num;
array2 = dsaparameters.Q;
Array.Reverse(array2);
Buffer.BlockCopy(array2, 0, array, num2, 20);
num2 += 20;
array2 = dsaparameters.G;
Array.Reverse(array2);
Buffer.BlockCopy(array2, 0, array, num2, num);
num2 += num;
array2 = dsaparameters.X;
Array.Reverse(array2);
Buffer.BlockCopy(array2, 0, array, num2, 20);
num2 += 20;
Buffer.BlockCopy(CryptoConvert.GetBytesLE(dsaparameters.Counter), 0, array, num2, 4);
num2 += 4;
array2 = dsaparameters.Seed;
Array.Reverse(array2);
Buffer.BlockCopy(array2, 0, array, num2, 20);
return array;
}
// Token: 0x060008D7 RID: 2263 RVA: 0x000227DC File Offset: 0x000209DC
public static RSA FromCapiPublicKeyBlob(byte[] blob)
{
return CryptoConvert.FromCapiPublicKeyBlob(blob, 0);
}
// Token: 0x060008D8 RID: 2264 RVA: 0x000227E8 File Offset: 0x000209E8
public static RSA FromCapiPublicKeyBlob(byte[] blob, int offset)
{
if (blob == null)
{
throw new ArgumentNullException("blob");
}
if (offset >= blob.Length)
{
throw new ArgumentException("blob is too small.");
}
RSA rsa2;
try
{
if (blob[offset] != 6 || blob[offset + 1] != 2 || blob[offset + 2] != 0 || blob[offset + 3] != 0 || CryptoConvert.ToUInt32LE(blob, offset + 8) != 826364754U)
{
throw new CryptographicException("Invalid blob header");
}
int num = CryptoConvert.ToInt32LE(blob, offset + 12);
RSAParameters rsaparameters = default(RSAParameters);
rsaparameters.Exponent = new byte[3];
rsaparameters.Exponent[0] = blob[offset + 18];
rsaparameters.Exponent[1] = blob[offset + 17];
rsaparameters.Exponent[2] = blob[offset + 16];
int num2 = offset + 20;
int num3 = num >> 3;
rsaparameters.Modulus = new byte[num3];
Buffer.BlockCopy(blob, num2, rsaparameters.Modulus, 0, num3);
Array.Reverse(rsaparameters.Modulus);
RSA rsa = RSA.Create();
rsa.ImportParameters(rsaparameters);
rsa2 = rsa;
}
catch (Exception ex)
{
throw new CryptographicException("Invalid blob.", ex);
}
return rsa2;
}
// Token: 0x060008D9 RID: 2265 RVA: 0x00022930 File Offset: 0x00020B30
public static DSA FromCapiPublicKeyBlobDSA(byte[] blob)
{
return CryptoConvert.FromCapiPublicKeyBlobDSA(blob, 0);
}
// Token: 0x060008DA RID: 2266 RVA: 0x0002293C File Offset: 0x00020B3C
public static DSA FromCapiPublicKeyBlobDSA(byte[] blob, int offset)
{
if (blob == null)
{
throw new ArgumentNullException("blob");
}
if (offset >= blob.Length)
{
throw new ArgumentException("blob is too small.");
}
DSA dsa2;
try
{
if (blob[offset] != 6 || blob[offset + 1] != 2 || blob[offset + 2] != 0 || blob[offset + 3] != 0 || CryptoConvert.ToUInt32LE(blob, offset + 8) != 827544388U)
{
throw new CryptographicException("Invalid blob header");
}
int num = CryptoConvert.ToInt32LE(blob, offset + 12);
DSAParameters dsaparameters = default(DSAParameters);
int num2 = num >> 3;
int num3 = offset + 16;
dsaparameters.P = new byte[num2];
Buffer.BlockCopy(blob, num3, dsaparameters.P, 0, num2);
Array.Reverse(dsaparameters.P);
num3 += num2;
dsaparameters.Q = new byte[20];
Buffer.BlockCopy(blob, num3, dsaparameters.Q, 0, 20);
Array.Reverse(dsaparameters.Q);
num3 += 20;
dsaparameters.G = new byte[num2];
Buffer.BlockCopy(blob, num3, dsaparameters.G, 0, num2);
Array.Reverse(dsaparameters.G);
num3 += num2;
dsaparameters.Y = new byte[num2];
Buffer.BlockCopy(blob, num3, dsaparameters.Y, 0, num2);
Array.Reverse(dsaparameters.Y);
num3 += num2;
dsaparameters.Counter = CryptoConvert.ToInt32LE(blob, num3);
num3 += 4;
dsaparameters.Seed = new byte[20];
Buffer.BlockCopy(blob, num3, dsaparameters.Seed, 0, 20);
Array.Reverse(dsaparameters.Seed);
num3 += 20;
DSA dsa = DSA.Create();
dsa.ImportParameters(dsaparameters);
dsa2 = dsa;
}
catch (Exception ex)
{
throw new CryptographicException("Invalid blob.", ex);
}
return dsa2;
}
// Token: 0x060008DB RID: 2267 RVA: 0x00022B18 File Offset: 0x00020D18
public static byte[] ToCapiPublicKeyBlob(RSA rsa)
{
RSAParameters rsaparameters = rsa.ExportParameters(false);
int num = rsaparameters.Modulus.Length;
byte[] array = new byte[20 + num];
array[0] = 6;
array[1] = 2;
array[5] = 36;
array[8] = 82;
array[9] = 83;
array[10] = 65;
array[11] = 49;
byte[] bytesLE = CryptoConvert.GetBytesLE(num << 3);
array[12] = bytesLE[0];
array[13] = bytesLE[1];
array[14] = bytesLE[2];
array[15] = bytesLE[3];
int num2 = 16;
int i = rsaparameters.Exponent.Length;
while (i > 0)
{
array[num2++] = rsaparameters.Exponent[--i];
}
num2 = 20;
byte[] modulus = rsaparameters.Modulus;
int num3 = modulus.Length;
Array.Reverse(modulus, 0, num3);
Buffer.BlockCopy(modulus, 0, array, num2, num3);
num2 += num3;
return array;
}
// Token: 0x060008DC RID: 2268 RVA: 0x00022BF0 File Offset: 0x00020DF0
public static byte[] ToCapiPublicKeyBlob(DSA dsa)
{
DSAParameters dsaparameters = dsa.ExportParameters(false);
int num = dsaparameters.P.Length;
byte[] array = new byte[16 + num + 20 + num + num + 4 + 20];
array[0] = 6;
array[1] = 2;
array[5] = 34;
array[8] = 68;
array[9] = 83;
array[10] = 83;
array[11] = 49;
byte[] bytesLE = CryptoConvert.GetBytesLE(num << 3);
array[12] = bytesLE[0];
array[13] = bytesLE[1];
array[14] = bytesLE[2];
array[15] = bytesLE[3];
int num2 = 16;
byte[] array2 = dsaparameters.P;
Array.Reverse(array2);
Buffer.BlockCopy(array2, 0, array, num2, num);
num2 += num;
array2 = dsaparameters.Q;
Array.Reverse(array2);
Buffer.BlockCopy(array2, 0, array, num2, 20);
num2 += 20;
array2 = dsaparameters.G;
Array.Reverse(array2);
Buffer.BlockCopy(array2, 0, array, num2, num);
num2 += num;
array2 = dsaparameters.Y;
Array.Reverse(array2);
Buffer.BlockCopy(array2, 0, array, num2, num);
num2 += num;
Buffer.BlockCopy(CryptoConvert.GetBytesLE(dsaparameters.Counter), 0, array, num2, 4);
num2 += 4;
array2 = dsaparameters.Seed;
Array.Reverse(array2);
Buffer.BlockCopy(array2, 0, array, num2, 20);
return array;
}
// Token: 0x060008DD RID: 2269 RVA: 0x00022D38 File Offset: 0x00020F38
public static RSA FromCapiKeyBlob(byte[] blob)
{
return CryptoConvert.FromCapiKeyBlob(blob, 0);
}
// Token: 0x060008DE RID: 2270 RVA: 0x00022D44 File Offset: 0x00020F44
public static RSA FromCapiKeyBlob(byte[] blob, int offset)
{
if (blob == null)
{
throw new ArgumentNullException("blob");
}
if (offset >= blob.Length)
{
throw new ArgumentException("blob is too small.");
}
byte b = blob[offset];
if (b == 6)
{
return CryptoConvert.FromCapiPublicKeyBlob(blob, offset);
}
if (b != 7)
{
if (b == 0)
{
if (blob[offset + 12] == 6)
{
return CryptoConvert.FromCapiPublicKeyBlob(blob, offset + 12);
}
}
throw new CryptographicException("Unknown blob format.");
}
return CryptoConvert.FromCapiPrivateKeyBlob(blob, offset);
}
// Token: 0x060008DF RID: 2271 RVA: 0x00022DCC File Offset: 0x00020FCC
public static DSA FromCapiKeyBlobDSA(byte[] blob)
{
return CryptoConvert.FromCapiKeyBlobDSA(blob, 0);
}
// Token: 0x060008E0 RID: 2272 RVA: 0x00022DD8 File Offset: 0x00020FD8
public static DSA FromCapiKeyBlobDSA(byte[] blob, int offset)
{
if (blob == null)
{
throw new ArgumentNullException("blob");
}
if (offset >= blob.Length)
{
throw new ArgumentException("blob is too small.");
}
byte b = blob[offset];
if (b == 6)
{
return CryptoConvert.FromCapiPublicKeyBlobDSA(blob, offset);
}
if (b != 7)
{
throw new CryptographicException("Unknown blob format.");
}
return CryptoConvert.FromCapiPrivateKeyBlobDSA(blob, offset);
}
// Token: 0x060008E1 RID: 2273 RVA: 0x00022E3C File Offset: 0x0002103C
public static byte[] ToCapiKeyBlob(AsymmetricAlgorithm keypair, bool includePrivateKey)
{
if (keypair == null)
{
throw new ArgumentNullException("keypair");
}
if (keypair is RSA)
{
return CryptoConvert.ToCapiKeyBlob((RSA)keypair, includePrivateKey);
}
if (keypair is DSA)
{
return CryptoConvert.ToCapiKeyBlob((DSA)keypair, includePrivateKey);
}
return null;
}
// Token: 0x060008E2 RID: 2274 RVA: 0x00022E8C File Offset: 0x0002108C
public static byte[] ToCapiKeyBlob(RSA rsa, bool includePrivateKey)
{
if (rsa == null)
{
throw new ArgumentNullException("rsa");
}
if (includePrivateKey)
{
return CryptoConvert.ToCapiPrivateKeyBlob(rsa);
}
return CryptoConvert.ToCapiPublicKeyBlob(rsa);
}
// Token: 0x060008E3 RID: 2275 RVA: 0x00022EC0 File Offset: 0x000210C0
public static byte[] ToCapiKeyBlob(DSA dsa, bool includePrivateKey)
{
if (dsa == null)
{
throw new ArgumentNullException("dsa");
}
if (includePrivateKey)
{
return CryptoConvert.ToCapiPrivateKeyBlob(dsa);
}
return CryptoConvert.ToCapiPublicKeyBlob(dsa);
}
// Token: 0x060008E4 RID: 2276 RVA: 0x00022EF4 File Offset: 0x000210F4
public static string ToHex(byte[] input)
{
if (input == null)
{
return null;
}
StringBuilder stringBuilder = new StringBuilder(input.Length * 2);
foreach (byte b in input)
{
stringBuilder.Append(b.ToString("X2", CultureInfo.InvariantCulture));
}
return stringBuilder.ToString();
}
// Token: 0x060008E5 RID: 2277 RVA: 0x00022F4C File Offset: 0x0002114C
private static byte FromHexChar(char c)
{
if (c >= 'a' && c <= 'f')
{
return (byte)(c - 'a' + '\n');
}
if (c >= 'A' && c <= 'F')
{
return (byte)(c - 'A' + '\n');
}
if (c >= '0' && c <= '9')
{
return (byte)(c - '0');
}
throw new ArgumentException("invalid hex char");
}
// Token: 0x060008E6 RID: 2278 RVA: 0x00022FAC File Offset: 0x000211AC
public static byte[] FromHex(string hex)
{
if (hex == null)
{
return null;
}
if ((hex.Length & 1) == 1)
{
throw new ArgumentException("Length must be a multiple of 2");
}
byte[] array = new byte[hex.Length >> 1];
int i = 0;
int num = 0;
while (i < array.Length)
{
array[i] = (byte)(CryptoConvert.FromHexChar(hex[num++]) << 4);
byte[] array2 = array;
int num2 = i++;
array2[num2] += CryptoConvert.FromHexChar(hex[num++]);
}
return array;
}
}
}