public static DSA FromCapiPublicKeyBlobDSA(byte[] blob)
 {
     return(CryptoConvert.FromCapiPublicKeyBlobDSA(blob, 0));
 }
        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);
        }
        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 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 = RSA.Create();
                rsa.ImportParameters(parameters);
                result = rsa;
            }
            catch (Exception inner)
            {
                throw new CryptographicException("Invalid blob.", inner);
            }
            return(result);
        }
示例#4
0
        public static RSA FromCapiPrivateKeyBlob(byte[] blob, int offset)
        {
            if (blob == null)
            {
                throw new ArgumentNullException(nameof(blob));
            }
            if (offset >= blob.Length)
            {
                throw new ArgumentException("blob is too small.");
            }
            RSAParameters parameters = new RSAParameters();

            try
            {
                if (blob[offset] != (byte)7 || blob[offset + 1] != (byte)2 || (blob[offset + 2] != (byte)0 || blob[offset + 3] != (byte)0) || CryptoConvert.ToUInt32LE(blob, offset + 8) != 843141970U)
                {
                    throw new CryptographicException("Invalid blob header");
                }
                int    int32Le = CryptoConvert.ToInt32LE(blob, offset + 12);
                byte[] array   = new byte[4];
                Buffer.BlockCopy((Array)blob, offset + 16, (Array)array, 0, 4);
                Array.Reverse((Array)array);
                parameters.Exponent = CryptoConvert.Trim(array);
                int srcOffset1 = offset + 20;
                int count1     = int32Le >> 3;
                parameters.Modulus = new byte[count1];
                Buffer.BlockCopy((Array)blob, srcOffset1, (Array)parameters.Modulus, 0, count1);
                Array.Reverse((Array)parameters.Modulus);
                int srcOffset2 = srcOffset1 + count1;
                int count2     = count1 >> 1;
                parameters.P = new byte[count2];
                Buffer.BlockCopy((Array)blob, srcOffset2, (Array)parameters.P, 0, count2);
                Array.Reverse((Array)parameters.P);
                int srcOffset3 = srcOffset2 + count2;
                parameters.Q = new byte[count2];
                Buffer.BlockCopy((Array)blob, srcOffset3, (Array)parameters.Q, 0, count2);
                Array.Reverse((Array)parameters.Q);
                int srcOffset4 = srcOffset3 + count2;
                parameters.DP = new byte[count2];
                Buffer.BlockCopy((Array)blob, srcOffset4, (Array)parameters.DP, 0, count2);
                Array.Reverse((Array)parameters.DP);
                int srcOffset5 = srcOffset4 + count2;
                parameters.DQ = new byte[count2];
                Buffer.BlockCopy((Array)blob, srcOffset5, (Array)parameters.DQ, 0, count2);
                Array.Reverse((Array)parameters.DQ);
                int srcOffset6 = srcOffset5 + count2;
                parameters.InverseQ = new byte[count2];
                Buffer.BlockCopy((Array)blob, srcOffset6, (Array)parameters.InverseQ, 0, count2);
                Array.Reverse((Array)parameters.InverseQ);
                int srcOffset7 = srcOffset6 + count2;
                parameters.D = new byte[count1];
                if (srcOffset7 + count1 + offset <= blob.Length)
                {
                    Buffer.BlockCopy((Array)blob, srcOffset7, (Array)parameters.D, 0, count1);
                    Array.Reverse((Array)parameters.D);
                }
            }
            catch (Exception ex)
            {
                throw new CryptographicException("Invalid blob.", ex);
            }
            RSA rsa;

            try
            {
                rsa = RSA.Create();
                rsa.ImportParameters(parameters);
            }
            catch (CryptographicException ex)
            {
                try
                {
                    rsa = (RSA) new RSACryptoServiceProvider(new CspParameters()
                    {
                        Flags = CspProviderFlags.UseMachineKeyStore
                    });
                    rsa.ImportParameters(parameters);
                }
                catch
                {
                    throw ex;
                }
            }
            return(rsa);
        }
        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 parameters = 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;
                parameters.P = new byte[num2];
                Buffer.BlockCopy(blob, num3, parameters.P, 0, num2);
                Array.Reverse(parameters.P);
                num3        += num2;
                parameters.Q = new byte[20];
                Buffer.BlockCopy(blob, num3, parameters.Q, 0, 20);
                Array.Reverse(parameters.Q);
                num3        += 20;
                parameters.G = new byte[num2];
                Buffer.BlockCopy(blob, num3, parameters.G, 0, num2);
                Array.Reverse(parameters.G);
                num3        += num2;
                parameters.X = new byte[20];
                Buffer.BlockCopy(blob, num3, parameters.X, 0, 20);
                Array.Reverse(parameters.X);
                num3 += 20;
                parameters.Counter = CryptoConvert.ToInt32LE(blob, num3);
                num3           += 4;
                parameters.Seed = new byte[20];
                Buffer.BlockCopy(blob, num3, parameters.Seed, 0, 20);
                Array.Reverse(parameters.Seed);
                num3 += 20;
            }
            catch (Exception inner)
            {
                throw new CryptographicException("Invalid blob.", inner);
            }
            DSA dsa = DSA.Create();

            dsa.ImportParameters(parameters);
            return(dsa);
        }
示例#6
0
 public static DSA FromCapiPublicKeyBlobDSA(byte[] blob, int offset)
 {
     if (blob == null)
     {
         throw new ArgumentNullException(nameof(blob));
     }
     if (offset >= blob.Length)
     {
         throw new ArgumentException("blob is too small.");
     }
     try
     {
         if (blob[offset] != (byte)6 || blob[offset + 1] != (byte)2 || (blob[offset + 2] != (byte)0 || blob[offset + 3] != (byte)0) || CryptoConvert.ToUInt32LE(blob, offset + 8) != 827544388U)
         {
             throw new CryptographicException("Invalid blob header");
         }
         int           int32Le    = CryptoConvert.ToInt32LE(blob, offset + 12);
         DSAParameters parameters = new DSAParameters();
         int           count      = int32Le >> 3;
         int           srcOffset1 = offset + 16;
         parameters.P = new byte[count];
         Buffer.BlockCopy((Array)blob, srcOffset1, (Array)parameters.P, 0, count);
         Array.Reverse((Array)parameters.P);
         int srcOffset2 = srcOffset1 + count;
         parameters.Q = new byte[20];
         Buffer.BlockCopy((Array)blob, srcOffset2, (Array)parameters.Q, 0, 20);
         Array.Reverse((Array)parameters.Q);
         int srcOffset3 = srcOffset2 + 20;
         parameters.G = new byte[count];
         Buffer.BlockCopy((Array)blob, srcOffset3, (Array)parameters.G, 0, count);
         Array.Reverse((Array)parameters.G);
         int srcOffset4 = srcOffset3 + count;
         parameters.Y = new byte[count];
         Buffer.BlockCopy((Array)blob, srcOffset4, (Array)parameters.Y, 0, count);
         Array.Reverse((Array)parameters.Y);
         int offset1 = srcOffset4 + count;
         parameters.Counter = CryptoConvert.ToInt32LE(blob, offset1);
         int srcOffset5 = offset1 + 4;
         parameters.Seed = new byte[20];
         Buffer.BlockCopy((Array)blob, srcOffset5, (Array)parameters.Seed, 0, 20);
         Array.Reverse((Array)parameters.Seed);
         int num = srcOffset5 + 20;
         DSA dsa = DSA.Create();
         dsa.ImportParameters(parameters);
         return(dsa);
     }
     catch (Exception ex)
     {
         throw new CryptographicException("Invalid blob.", ex);
     }
 }
示例#7
0
 public static RSA FromCapiKeyBlob(byte[] blob)
 {
     return(CryptoConvert.FromCapiKeyBlob(blob, 0));
 }
示例#8
0
 public static RSA FromCapiPublicKeyBlob(byte[] blob, int offset)
 {
     if (blob == null)
     {
         throw new ArgumentNullException(nameof(blob));
     }
     if (offset >= blob.Length)
     {
         throw new ArgumentException("blob is too small.");
     }
     try
     {
         if (blob[offset] != (byte)6 || blob[offset + 1] != (byte)2 || (blob[offset + 2] != (byte)0 || blob[offset + 3] != (byte)0) || CryptoConvert.ToUInt32LE(blob, offset + 8) != 826364754U)
         {
             throw new CryptographicException("Invalid blob header");
         }
         int           int32Le    = CryptoConvert.ToInt32LE(blob, offset + 12);
         RSAParameters parameters = new 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 count     = int32Le >> 3;
         parameters.Modulus = new byte[count];
         Buffer.BlockCopy((Array)blob, srcOffset, (Array)parameters.Modulus, 0, count);
         Array.Reverse((Array)parameters.Modulus);
         RSA rsa;
         try
         {
             rsa = RSA.Create();
             rsa.ImportParameters(parameters);
         }
         catch (CryptographicException)
         {
             rsa = (RSA) new RSACryptoServiceProvider(new CspParameters()
             {
                 Flags = CspProviderFlags.UseMachineKeyStore
             });
             rsa.ImportParameters(parameters);
         }
         return(rsa);
     }
     catch (Exception ex)
     {
         throw new CryptographicException("Invalid blob.", ex);
     }
 }
示例#9
0
        public static byte[] ToCapiPrivateKeyBlob(RSA rsa)
        {
            RSAParameters rsaParameters = rsa.ExportParameters(true);
            int           length1       = rsaParameters.Modulus.Length;

            byte[] numArray = new byte[20 + (length1 << 2) + (length1 >> 1)];
            numArray[0]  = (byte)7;
            numArray[1]  = (byte)2;
            numArray[5]  = (byte)36;
            numArray[8]  = (byte)82;
            numArray[9]  = (byte)83;
            numArray[10] = (byte)65;
            numArray[11] = (byte)50;
            byte[] bytesLe = CryptoConvert.GetBytesLE(length1 << 3);
            numArray[12] = bytesLe[0];
            numArray[13] = bytesLe[1];
            numArray[14] = bytesLe[2];
            numArray[15] = bytesLe[3];
            int num     = 16;
            int length2 = rsaParameters.Exponent.Length;

            while (length2 > 0)
            {
                numArray[num++] = rsaParameters.Exponent[--length2];
            }
            int dstOffset1 = 20;

            byte[] modulus = rsaParameters.Modulus;
            int    length3 = modulus.Length;

            Array.Reverse((Array)modulus, 0, length3);
            Buffer.BlockCopy((Array)modulus, 0, (Array)numArray, dstOffset1, length3);
            int dstOffset2 = dstOffset1 + length3;

            byte[] p       = rsaParameters.P;
            int    length4 = p.Length;

            Array.Reverse((Array)p, 0, length4);
            Buffer.BlockCopy((Array)p, 0, (Array)numArray, dstOffset2, length4);
            int dstOffset3 = dstOffset2 + length4;

            byte[] q       = rsaParameters.Q;
            int    length5 = q.Length;

            Array.Reverse((Array)q, 0, length5);
            Buffer.BlockCopy((Array)q, 0, (Array)numArray, dstOffset3, length5);
            int dstOffset4 = dstOffset3 + length5;

            byte[] dp      = rsaParameters.DP;
            int    length6 = dp.Length;

            Array.Reverse((Array)dp, 0, length6);
            Buffer.BlockCopy((Array)dp, 0, (Array)numArray, dstOffset4, length6);
            int dstOffset5 = dstOffset4 + length6;

            byte[] dq      = rsaParameters.DQ;
            int    length7 = dq.Length;

            Array.Reverse((Array)dq, 0, length7);
            Buffer.BlockCopy((Array)dq, 0, (Array)numArray, dstOffset5, length7);
            int dstOffset6 = dstOffset5 + length7;

            byte[] inverseQ = rsaParameters.InverseQ;
            int    length8  = inverseQ.Length;

            Array.Reverse((Array)inverseQ, 0, length8);
            Buffer.BlockCopy((Array)inverseQ, 0, (Array)numArray, dstOffset6, length8);
            int dstOffset7 = dstOffset6 + length8;

            byte[] d       = rsaParameters.D;
            int    length9 = d.Length;

            Array.Reverse((Array)d, 0, length9);
            Buffer.BlockCopy((Array)d, 0, (Array)numArray, dstOffset7, length9);
            return(numArray);
        }
示例#10
0
        public static DSA FromCapiPrivateKeyBlobDSA(byte[] blob, int offset)
        {
            if (blob == null)
            {
                throw new ArgumentNullException(nameof(blob));
            }
            if (offset >= blob.Length)
            {
                throw new ArgumentException("blob is too small.");
            }
            DSAParameters parameters = new DSAParameters();

            try
            {
                if (blob[offset] != (byte)7 || blob[offset + 1] != (byte)2 || (blob[offset + 2] != (byte)0 || blob[offset + 3] != (byte)0) || CryptoConvert.ToUInt32LE(blob, offset + 8) != 844321604U)
                {
                    throw new CryptographicException("Invalid blob header");
                }
                int count      = CryptoConvert.ToInt32LE(blob, offset + 12) >> 3;
                int srcOffset1 = offset + 16;
                parameters.P = new byte[count];
                Buffer.BlockCopy((Array)blob, srcOffset1, (Array)parameters.P, 0, count);
                Array.Reverse((Array)parameters.P);
                int srcOffset2 = srcOffset1 + count;
                parameters.Q = new byte[20];
                Buffer.BlockCopy((Array)blob, srcOffset2, (Array)parameters.Q, 0, 20);
                Array.Reverse((Array)parameters.Q);
                int srcOffset3 = srcOffset2 + 20;
                parameters.G = new byte[count];
                Buffer.BlockCopy((Array)blob, srcOffset3, (Array)parameters.G, 0, count);
                Array.Reverse((Array)parameters.G);
                int srcOffset4 = srcOffset3 + count;
                parameters.X = new byte[20];
                Buffer.BlockCopy((Array)blob, srcOffset4, (Array)parameters.X, 0, 20);
                Array.Reverse((Array)parameters.X);
                int offset1 = srcOffset4 + 20;
                parameters.Counter = CryptoConvert.ToInt32LE(blob, offset1);
                int srcOffset5 = offset1 + 4;
                parameters.Seed = new byte[20];
                Buffer.BlockCopy((Array)blob, srcOffset5, (Array)parameters.Seed, 0, 20);
                Array.Reverse((Array)parameters.Seed);
                int num = srcOffset5 + 20;
            }
            catch (Exception ex)
            {
                throw new CryptographicException("Invalid blob.", ex);
            }
            DSA dsa;

            try
            {
                dsa = DSA.Create();
                dsa.ImportParameters(parameters);
            }
            catch (CryptographicException ex)
            {
                try
                {
                    dsa = (DSA) new DSACryptoServiceProvider(new CspParameters()
                    {
                        Flags = CspProviderFlags.UseMachineKeyStore
                    });
                    dsa.ImportParameters(parameters);
                }
                catch
                {
                    throw ex;
                }
            }
            return(dsa);
        }
示例#11
0
        private static RSA FromCapiPrivateKeyBlob(byte[] blob, int offset)
        {
            RSAParameters rSAParameter = new RSAParameters();

            try
            {
                if (blob[offset] != 7 || blob[offset + 1] != 2 || blob[offset + 2] != 0 || blob[offset + 3] != 0 || CryptoConvert.ToUInt32LE(blob, offset + 8) != 843141970)
                {
                    throw new CryptographicException("Invalid blob header");
                }
                int    int32LE  = CryptoConvert.ToInt32LE(blob, offset + 12);
                byte[] numArray = new byte[4];
                Buffer.BlockCopy(blob, offset + 16, numArray, 0, 4);
                Array.Reverse(numArray);
                rSAParameter.Exponent = CryptoConvert.Trim(numArray);
                int num  = offset + 20;
                int num1 = int32LE >> 3;
                rSAParameter.Modulus = new byte[num1];
                Buffer.BlockCopy(blob, num, rSAParameter.Modulus, 0, num1);
                Array.Reverse(rSAParameter.Modulus);
                num += num1;
                int num2 = num1 >> 1;
                rSAParameter.P = new byte[num2];
                Buffer.BlockCopy(blob, num, rSAParameter.P, 0, num2);
                Array.Reverse(rSAParameter.P);
                num           += num2;
                rSAParameter.Q = new byte[num2];
                Buffer.BlockCopy(blob, num, rSAParameter.Q, 0, num2);
                Array.Reverse(rSAParameter.Q);
                num            += num2;
                rSAParameter.DP = new byte[num2];
                Buffer.BlockCopy(blob, num, rSAParameter.DP, 0, num2);
                Array.Reverse(rSAParameter.DP);
                num            += num2;
                rSAParameter.DQ = new byte[num2];
                Buffer.BlockCopy(blob, num, rSAParameter.DQ, 0, num2);
                Array.Reverse(rSAParameter.DQ);
                num += num2;
                rSAParameter.InverseQ = new byte[num2];
                Buffer.BlockCopy(blob, num, rSAParameter.InverseQ, 0, num2);
                Array.Reverse(rSAParameter.InverseQ);
                num           += num2;
                rSAParameter.D = new byte[num1];
                if (num + num1 + offset <= (int)blob.Length)
                {
                    Buffer.BlockCopy(blob, num, rSAParameter.D, 0, num1);
                    Array.Reverse(rSAParameter.D);
                }
            }
            catch (Exception exception)
            {
                throw new CryptographicException("Invalid blob.", exception);
            }
            RSA rSACryptoServiceProvider = null;

            try
            {
                rSACryptoServiceProvider = RSA.Create();
                rSACryptoServiceProvider.ImportParameters(rSAParameter);
            }
            catch (CryptographicException cryptographicException)
            {
                bool flag = false;
                try
                {
                    rSACryptoServiceProvider = new RSACryptoServiceProvider(new CspParameters()
                    {
                        Flags = CspProviderFlags.UseMachineKeyStore
                    });
                    rSACryptoServiceProvider.ImportParameters(rSAParameter);
                }
                catch
                {
                    flag = true;
                }
                if (flag)
                {
                    throw;
                }
            }
            return(rSACryptoServiceProvider);
        }
示例#12
0
        private static RSA FromCapiPublicKeyBlob(byte[] blob, int offset)
        {
            RSA rSA;

            try
            {
                if (blob[offset] != 6 || blob[offset + 1] != 2 || blob[offset + 2] != 0 || blob[offset + 3] != 0 || CryptoConvert.ToUInt32LE(blob, offset + 8) != 826364754)
                {
                    throw new CryptographicException("Invalid blob header");
                }
                int           int32LE      = CryptoConvert.ToInt32LE(blob, offset + 12);
                RSAParameters rSAParameter = new RSAParameters()
                {
                    Exponent = new byte[] { blob[offset + 18], blob[offset + 17], blob[offset + 16] }
                };
                int num  = offset + 20;
                int num1 = int32LE >> 3;
                rSAParameter.Modulus = new byte[num1];
                Buffer.BlockCopy(blob, num, rSAParameter.Modulus, 0, num1);
                Array.Reverse(rSAParameter.Modulus);
                RSA rSACryptoServiceProvider = null;
                try
                {
                    rSACryptoServiceProvider = RSA.Create();
                    rSACryptoServiceProvider.ImportParameters(rSAParameter);
                }
                catch (CryptographicException cryptographicException)
                {
                    rSACryptoServiceProvider = new RSACryptoServiceProvider(new CspParameters()
                    {
                        Flags = CspProviderFlags.UseMachineKeyStore
                    });
                    rSACryptoServiceProvider.ImportParameters(rSAParameter);
                }
                rSA = rSACryptoServiceProvider;
            }
            catch (Exception exception)
            {
                throw new CryptographicException("Invalid blob.", exception);
            }
            return(rSA);
        }