Inheritance: Asn1Encodable
Exemplo n.º 1
0
        public static PbeS2Parameters GetInstance(object obj)
        {
            if (obj == null)
            {
                return(null);
            }
            PbeS2Parameters existing = obj as PbeS2Parameters;

            if (existing != null)
            {
                return(existing);
            }
            return(new PbeS2Parameters(Asn1Sequence.GetInstance(obj)));
        }
Exemplo n.º 2
0
        public static PbeS2Parameters GetInstance(object obj)
        {
            if (obj == null)
            {
                return(null);
            }
            PbeS2Parameters pbeS2Parameters = obj as PbeS2Parameters;

            if (pbeS2Parameters != null)
            {
                return(pbeS2Parameters);
            }
            return(new PbeS2Parameters(Asn1Sequence.GetInstance(obj)));
        }
Exemplo n.º 3
0
        public static PbeS2Parameters GetInstance(object obj)
        {
            if (obj == null)
            {
                return(null);
            }
            PbeS2Parameters existing = obj as PbeS2Parameters;

            if (existing != null)
            {
                return(existing);
            }

// Disable warning since using GetInstance would result in an infinite loop
#pragma warning disable 618
            return(new PbeS2Parameters(Asn1Sequence.GetInstance(obj)));

#pragma warning restore 618
        }
Exemplo n.º 4
0
            public override void PerformTest()
            {
                char[] password = "******".ToCharArray();
                PbeParametersGenerator generator = new Pkcs5S2ParametersGenerator();

                EncryptedPrivateKeyInfo info = null;
                try
                {
                    info = EncryptedPrivateKeyInfo.GetInstance(Asn1Object.FromByteArray(sample));
                }
                catch (System.Exception e)
                {
                    Fail("failed construction - exception " + e.ToString(), e);
                }

                PbeS2Parameters alg = new PbeS2Parameters((Asn1Sequence) info.EncryptionAlgorithm.Parameters);
                Pbkdf2Params func = Pbkdf2Params.GetInstance(alg.KeyDerivationFunc.Parameters);
                EncryptionScheme scheme = alg.EncryptionScheme;

                if (func.KeyLength != null)
                {
                    keySize = func.KeyLength.IntValue * 8;
                }

                int iterationCount = func.IterationCount.IntValue;
                byte[] salt = func.GetSalt();

                generator.Init(PbeParametersGenerator.Pkcs5PasswordToBytes(password), salt, iterationCount);

                DerObjectIdentifier algOid = scheme.ObjectID;

                byte[] iv;
                if (algOid.Equals(PkcsObjectIdentifiers.RC2Cbc))
                {
                    RC2CbcParameter rc2Params = RC2CbcParameter.GetInstance(scheme.Asn1Object);
                    iv = rc2Params.GetIV();
                }
                else
                {
                    iv = ((Asn1OctetString) scheme.Asn1Object).GetOctets();
                }

                ICipherParameters param = new ParametersWithIV(
                    generator.GenerateDerivedParameters(algOid.Id, keySize), iv);

                cipher.Init(false, param);

                byte[] data = info.GetEncryptedData();
                byte[] outBytes = new byte[cipher.GetOutputSize(data.Length)];
                int len = cipher.ProcessBytes(data, 0, data.Length, outBytes, 0);

                try
                {
                    len += cipher.DoFinal(outBytes, len);
                }
                catch (Exception e)
                {
                    Fail("failed DoFinal - exception " + e.ToString());
                }

                if (result.Length != len)
                {
                    Fail("failed length");
                }

                for (int i = 0; i != len; i++)
                {
                    if (outBytes[i] != result[i])
                    {
                        Fail("failed comparison");
                    }
                }
            }