Example #1
0
 public static RSA FromCapiKeyBlob(byte[] blob)
 {
     return(CryptoConvert.FromCapiKeyBlob(blob, 0));
 }
Example #2
0
        private static RSA FromCapiPrivateKeyBlob(byte[] blob, int offset)
        {
            RSAParameters parameters = 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);
                parameters.Exponent = CryptoConvert.Trim(array);
                int num2 = offset + 20;
                int num3 = num >> 3;
                parameters.Modulus = new byte[num3];
                Buffer.BlockCopy(blob, num2, parameters.Modulus, 0, num3);
                Array.Reverse(parameters.Modulus);
                num2 += num3;
                int num4 = num3 >> 1;
                parameters.P = new byte[num4];
                Buffer.BlockCopy(blob, num2, parameters.P, 0, num4);
                Array.Reverse(parameters.P);
                num2        += num4;
                parameters.Q = new byte[num4];
                Buffer.BlockCopy(blob, num2, parameters.Q, 0, num4);
                Array.Reverse(parameters.Q);
                num2         += num4;
                parameters.DP = new byte[num4];
                Buffer.BlockCopy(blob, num2, parameters.DP, 0, num4);
                Array.Reverse(parameters.DP);
                num2         += num4;
                parameters.DQ = new byte[num4];
                Buffer.BlockCopy(blob, num2, parameters.DQ, 0, num4);
                Array.Reverse(parameters.DQ);
                num2 += num4;
                parameters.InverseQ = new byte[num4];
                Buffer.BlockCopy(blob, num2, parameters.InverseQ, 0, num4);
                Array.Reverse(parameters.InverseQ);
                num2        += num4;
                parameters.D = new byte[num3];
                if (num2 + num3 + offset <= blob.Length)
                {
                    Buffer.BlockCopy(blob, num2, parameters.D, 0, num3);
                    Array.Reverse(parameters.D);
                }
            }
            catch (Exception inner)
            {
                throw new CryptographicException("Invalid blob.", inner);
            }
            RSA rSA = null;

            try
            {
                rSA = RSA.Create();
                rSA.ImportParameters(parameters);
            }
            catch (CryptographicException)
            {
                bool flag = false;
                try
                {
                    rSA = new RSACryptoServiceProvider(new CspParameters
                    {
                        Flags = CspProviderFlags.UseMachineKeyStore
                    });
                    rSA.ImportParameters(parameters);
                }
                catch
                {
                    flag = true;
                }
                if (flag)
                {
                    throw;
                }
            }
            return(rSA);
        }
Example #3
0
        private static RSA FromCapiPublicKeyBlob(byte[] blob, int offset)
        {
            RSA result;

            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 parameters = default(RSAParameters);
                parameters.Exponent    = new byte[3];
                parameters.Exponent[0] = blob[offset + 18];
                parameters.Exponent[1] = blob[offset + 17];
                parameters.Exponent[2] = blob[offset + 16];
                int srcOffset = offset + 20;
                int num2      = num >> 3;
                parameters.Modulus = new byte[num2];
                Buffer.BlockCopy(blob, srcOffset, parameters.Modulus, 0, num2);
                Array.Reverse(parameters.Modulus);
                RSA rSA = null;
                try
                {
                    rSA = RSA.Create();
                    rSA.ImportParameters(parameters);
                }
                catch (CryptographicException)
                {
                    rSA = new RSACryptoServiceProvider(new CspParameters
                    {
                        Flags = CspProviderFlags.UseMachineKeyStore
                    });
                    rSA.ImportParameters(parameters);
                }
                result = rSA;
            }
            catch (Exception inner)
            {
                throw new CryptographicException("Invalid blob.", inner);
            }
            return(result);
        }