public static Asn1Encodable GenerateAlgorithmParameters(string algorithm, byte[] salt, int iterationCount)
 {
     if (PbeUtilities.IsPkcs12(algorithm))
     {
         return(new Pkcs12PbeParams(salt, iterationCount));
     }
     if (PbeUtilities.IsPkcs5Scheme2(algorithm))
     {
         return(new Pbkdf2Params(salt, iterationCount));
     }
     return(new PbeParameter(salt, iterationCount));
 }
        public static object CreateEngine(AlgorithmIdentifier algID)
        {
            string id = algID.ObjectID.Id;

            if (PbeUtilities.IsPkcs5Scheme2(id))
            {
                PbeS2Parameters     instance         = PbeS2Parameters.GetInstance(algID.Parameters.ToAsn1Object());
                AlgorithmIdentifier encryptionScheme = instance.EncryptionScheme;
                return(CipherUtilities.GetCipher(encryptionScheme.ObjectID));
            }
            return(PbeUtilities.CreateEngine(id));
        }
        private static ICipherParameters FixDesParity(string mechanism, ICipherParameters parameters)
        {
            if (!mechanism.EndsWith("DES-CBC") & !mechanism.EndsWith("DESEDE-CBC"))
            {
                return(parameters);
            }
            if (parameters is ParametersWithIV)
            {
                ParametersWithIV parametersWithIV = (ParametersWithIV)parameters;
                return(new ParametersWithIV(PbeUtilities.FixDesParity(mechanism, parametersWithIV.Parameters), parametersWithIV.GetIV()));
            }
            KeyParameter keyParameter = (KeyParameter)parameters;

            byte[] key = keyParameter.GetKey();
            DesParameters.SetOddParity(key);
            return(new KeyParameter(key));
        }
 public static object CreateEngine(DerObjectIdentifier algorithmOid)
 {
     return(PbeUtilities.CreateEngine(algorithmOid.Id));
 }
        public static ICipherParameters GenerateCipherParameters(string algorithm, char[] password, bool wrongPkcs12Zero, Asn1Encodable pbeParameters)
        {
            string text = (string)PbeUtilities.algorithms[Platform.ToUpperInvariant(algorithm)];

            byte[] array          = null;
            byte[] salt           = null;
            int    iterationCount = 0;

            if (PbeUtilities.IsPkcs12(text))
            {
                Pkcs12PbeParams instance = Pkcs12PbeParams.GetInstance(pbeParameters);
                salt           = instance.GetIV();
                iterationCount = instance.Iterations.IntValue;
                array          = PbeParametersGenerator.Pkcs12PasswordToBytes(password, wrongPkcs12Zero);
            }
            else if (!PbeUtilities.IsPkcs5Scheme2(text))
            {
                PbeParameter instance2 = PbeParameter.GetInstance(pbeParameters);
                salt           = instance2.GetSalt();
                iterationCount = instance2.IterationCount.IntValue;
                array          = PbeParametersGenerator.Pkcs5PasswordToBytes(password);
            }
            ICipherParameters parameters = null;

            if (PbeUtilities.IsPkcs5Scheme2(text))
            {
                PbeS2Parameters     instance3        = PbeS2Parameters.GetInstance(pbeParameters.ToAsn1Object());
                AlgorithmIdentifier encryptionScheme = instance3.EncryptionScheme;
                DerObjectIdentifier objectID         = encryptionScheme.ObjectID;
                Asn1Object          obj       = encryptionScheme.Parameters.ToAsn1Object();
                Pbkdf2Params        instance4 = Pbkdf2Params.GetInstance(instance3.KeyDerivationFunc.Parameters.ToAsn1Object());
                byte[] array2;
                if (objectID.Equals(PkcsObjectIdentifiers.RC2Cbc))
                {
                    RC2CbcParameter instance5 = RC2CbcParameter.GetInstance(obj);
                    array2 = instance5.GetIV();
                }
                else
                {
                    array2 = Asn1OctetString.GetInstance(obj).GetOctets();
                }
                salt           = instance4.GetSalt();
                iterationCount = instance4.IterationCount.IntValue;
                array          = PbeParametersGenerator.Pkcs5PasswordToBytes(password);
                int keySize = (instance4.KeyLength != null) ? (instance4.KeyLength.IntValue * 8) : GeneratorUtilities.GetDefaultKeySize(objectID);
                PbeParametersGenerator pbeParametersGenerator = PbeUtilities.MakePbeGenerator((string)PbeUtilities.algorithmType[text], null, array, salt, iterationCount);
                parameters = pbeParametersGenerator.GenerateDerivedParameters(objectID.Id, keySize);
                if (array2 != null && !Arrays.AreEqual(array2, new byte[array2.Length]))
                {
                    parameters = new ParametersWithIV(parameters, array2);
                }
            }
            else if (text.StartsWith("PBEwithSHA-1"))
            {
                PbeParametersGenerator pbeParametersGenerator2 = PbeUtilities.MakePbeGenerator((string)PbeUtilities.algorithmType[text], new Sha1Digest(), array, salt, iterationCount);
                if (text.Equals("PBEwithSHA-1and128bitAES-CBC-BC"))
                {
                    parameters = pbeParametersGenerator2.GenerateDerivedParameters("AES", 128, 128);
                }
                else if (text.Equals("PBEwithSHA-1and192bitAES-CBC-BC"))
                {
                    parameters = pbeParametersGenerator2.GenerateDerivedParameters("AES", 192, 128);
                }
                else if (text.Equals("PBEwithSHA-1and256bitAES-CBC-BC"))
                {
                    parameters = pbeParametersGenerator2.GenerateDerivedParameters("AES", 256, 128);
                }
                else if (text.Equals("PBEwithSHA-1and128bitRC4"))
                {
                    parameters = pbeParametersGenerator2.GenerateDerivedParameters("RC4", 128);
                }
                else if (text.Equals("PBEwithSHA-1and40bitRC4"))
                {
                    parameters = pbeParametersGenerator2.GenerateDerivedParameters("RC4", 40);
                }
                else if (text.Equals("PBEwithSHA-1and3-keyDESEDE-CBC"))
                {
                    parameters = pbeParametersGenerator2.GenerateDerivedParameters("DESEDE", 192, 64);
                }
                else if (text.Equals("PBEwithSHA-1and2-keyDESEDE-CBC"))
                {
                    parameters = pbeParametersGenerator2.GenerateDerivedParameters("DESEDE", 128, 64);
                }
                else if (text.Equals("PBEwithSHA-1and128bitRC2-CBC"))
                {
                    parameters = pbeParametersGenerator2.GenerateDerivedParameters("RC2", 128, 64);
                }
                else if (text.Equals("PBEwithSHA-1and40bitRC2-CBC"))
                {
                    parameters = pbeParametersGenerator2.GenerateDerivedParameters("RC2", 40, 64);
                }
                else if (text.Equals("PBEwithSHA-1andDES-CBC"))
                {
                    parameters = pbeParametersGenerator2.GenerateDerivedParameters("DES", 64, 64);
                }
                else if (text.Equals("PBEwithSHA-1andRC2-CBC"))
                {
                    parameters = pbeParametersGenerator2.GenerateDerivedParameters("RC2", 64, 64);
                }
            }
            else if (text.StartsWith("PBEwithSHA-256"))
            {
                PbeParametersGenerator pbeParametersGenerator3 = PbeUtilities.MakePbeGenerator((string)PbeUtilities.algorithmType[text], new Sha256Digest(), array, salt, iterationCount);
                if (text.Equals("PBEwithSHA-256and128bitAES-CBC-BC"))
                {
                    parameters = pbeParametersGenerator3.GenerateDerivedParameters("AES", 128, 128);
                }
                else if (text.Equals("PBEwithSHA-256and192bitAES-CBC-BC"))
                {
                    parameters = pbeParametersGenerator3.GenerateDerivedParameters("AES", 192, 128);
                }
                else if (text.Equals("PBEwithSHA-256and256bitAES-CBC-BC"))
                {
                    parameters = pbeParametersGenerator3.GenerateDerivedParameters("AES", 256, 128);
                }
            }
            else if (text.StartsWith("PBEwithMD5"))
            {
                PbeParametersGenerator pbeParametersGenerator4 = PbeUtilities.MakePbeGenerator((string)PbeUtilities.algorithmType[text], new MD5Digest(), array, salt, iterationCount);
                if (text.Equals("PBEwithMD5andDES-CBC"))
                {
                    parameters = pbeParametersGenerator4.GenerateDerivedParameters("DES", 64, 64);
                }
                else if (text.Equals("PBEwithMD5andRC2-CBC"))
                {
                    parameters = pbeParametersGenerator4.GenerateDerivedParameters("RC2", 64, 64);
                }
                else if (text.Equals("PBEwithMD5and128bitAES-CBC-OpenSSL"))
                {
                    parameters = pbeParametersGenerator4.GenerateDerivedParameters("AES", 128, 128);
                }
                else if (text.Equals("PBEwithMD5and192bitAES-CBC-OpenSSL"))
                {
                    parameters = pbeParametersGenerator4.GenerateDerivedParameters("AES", 192, 128);
                }
                else if (text.Equals("PBEwithMD5and256bitAES-CBC-OpenSSL"))
                {
                    parameters = pbeParametersGenerator4.GenerateDerivedParameters("AES", 256, 128);
                }
            }
            else if (text.StartsWith("PBEwithMD2"))
            {
                PbeParametersGenerator pbeParametersGenerator5 = PbeUtilities.MakePbeGenerator((string)PbeUtilities.algorithmType[text], new MD2Digest(), array, salt, iterationCount);
                if (text.Equals("PBEwithMD2andDES-CBC"))
                {
                    parameters = pbeParametersGenerator5.GenerateDerivedParameters("DES", 64, 64);
                }
                else if (text.Equals("PBEwithMD2andRC2-CBC"))
                {
                    parameters = pbeParametersGenerator5.GenerateDerivedParameters("RC2", 64, 64);
                }
            }
            else if (text.StartsWith("PBEwithHmac"))
            {
                string  algorithm2 = text.Substring("PBEwithHmac".Length);
                IDigest digest     = DigestUtilities.GetDigest(algorithm2);
                PbeParametersGenerator pbeParametersGenerator6 = PbeUtilities.MakePbeGenerator((string)PbeUtilities.algorithmType[text], digest, array, salt, iterationCount);
                int keySize2 = digest.GetDigestSize() * 8;
                parameters = pbeParametersGenerator6.GenerateDerivedMacParameters(keySize2);
            }
            Array.Clear(array, 0, array.Length);
            return(PbeUtilities.FixDesParity(text, parameters));
        }
 public static ICipherParameters GenerateCipherParameters(string algorithm, char[] password, Asn1Encodable pbeParameters)
 {
     return(PbeUtilities.GenerateCipherParameters(algorithm, password, false, pbeParameters));
 }
 public static ICipherParameters GenerateCipherParameters(AlgorithmIdentifier algID, char[] password, bool wrongPkcs12Zero)
 {
     return(PbeUtilities.GenerateCipherParameters(algID.ObjectID.Id, password, wrongPkcs12Zero, algID.Parameters));
 }
 public static ICipherParameters GenerateCipherParameters(AlgorithmIdentifier algID, char[] password)
 {
     return(PbeUtilities.GenerateCipherParameters(algID.ObjectID.Id, password, false, algID.Parameters));
 }
 public static ICipherParameters GenerateCipherParameters(DerObjectIdentifier algorithmOid, char[] password, bool wrongPkcs12Zero, Asn1Encodable pbeParameters)
 {
     return(PbeUtilities.GenerateCipherParameters(algorithmOid.Id, password, wrongPkcs12Zero, pbeParameters));
 }
Beispiel #10
0
 public static Asn1Encodable GenerateAlgorithmParameters(DerObjectIdentifier algorithmOid, byte[] salt, int iterationCount)
 {
     return(PbeUtilities.GenerateAlgorithmParameters(algorithmOid.Id, salt, iterationCount));
 }