ToUInt32LE() private static méthode

private static ToUInt32LE ( byte bytes, int offset ) : uint
bytes byte
offset int
Résultat uint
Exemple #1
0
        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 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 ex)
            {
                try
                {
                    rsa = new RSACryptoServiceProvider(new CspParameters
                    {
                        Flags = CspProviderFlags.UseMachineKeyStore
                    });
                    rsa.ImportParameters(parameters);
                }
                catch
                {
                    throw ex;
                }
            }
            return(rsa);
        }
Exemple #2
0
        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 = 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);
        }
Exemple #3
0
        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 result;

            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 parameters = default(DSAParameters);
                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.Y = new byte[num2];
                Buffer.BlockCopy(blob, num3, parameters.Y, 0, num2);
                Array.Reverse(parameters.Y);
                num3 += num2;
                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;
                DSA dsa = DSA.Create();
                dsa.ImportParameters(parameters);
                result = dsa;
            }
            catch (Exception inner)
            {
                throw new CryptographicException("Invalid blob.", inner);
            }
            return(result);
        }
Exemple #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);
        }
Exemple #5
0
        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 = null;

            try
            {
                dsa = DSA.Create();
                dsa.ImportParameters(parameters);
            }
            catch (CryptographicException ex)
            {
                try
                {
                    dsa = new DSACryptoServiceProvider(new CspParameters
                    {
                        Flags = CspProviderFlags.UseMachineKeyStore
                    });
                    dsa.ImportParameters(parameters);
                }
                catch
                {
                    throw ex;
                }
            }
            return(dsa);
        }
Exemple #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);
     }
 }
Exemple #7
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);
     }
 }
Exemple #8
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);
        }
Exemple #9
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);
        }
Exemple #10
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);
        }