Ejemplo n.º 1
0
        public static EncryptedPrivateKeyInfo CreateEncryptedPrivateKeyInfo(
            string algorithm,
            char[]                      passPhrase,
            byte[]                      salt,
            int iterationCount,
            PrivateKeyInfo keyInfo)
        {
            if (!PbeUtilities.IsPbeAlgorithm(algorithm))
            {
                throw new ArgumentException("attempt to use non-PBE algorithm with PBE EncryptedPrivateKeyInfo generation");
            }

            IBufferedCipher cipher = PbeUtilities.CreateEngine(algorithm) as IBufferedCipher;

            if (cipher == null)
            {
                // TODO Throw exception?
            }

            Asn1Encodable parameters = PbeUtilities.GenerateAlgorithmParameters(
                algorithm, salt, iterationCount);

            ICipherParameters keyParameters = PbeUtilities.GenerateCipherParameters(
                algorithm, passPhrase, parameters);

            cipher.Init(true, keyParameters);

            byte[] keyBytes = keyInfo.GetEncoded();
            byte[] encoding = cipher.DoFinal(keyBytes);

            DerObjectIdentifier oid   = PbeUtilities.GetObjectIdentifier(algorithm);
            AlgorithmIdentifier algID = new AlgorithmIdentifier(oid, parameters);

            return(new EncryptedPrivateKeyInfo(algID, encoding));
        }