Esempio n. 1
0
        public static EncryptedPrivateKeyInfo createEncryptedPrivateKeyInfo(
            String algorithm,
            char[] passPhrase,
            byte[] salt,
            int iterationCount,
            PrivateKeyInfo keyInfo)
        {
            if (!PBEUtil.isPBEAlgorithm(algorithm))
            {
                throw new Exception("attempt to use non-PBE algorithm with PBE EncryptedPrivateKeyInfo generation");
            }

            ASN1Encodable    parameters    = PBEUtil.generateAlgorithmParameters(algorithm, salt, iterationCount);
            CipherParameters keyParameters = PBEUtil.generateCipherParameters(algorithm, passPhrase, parameters);

            byte[] encoding = null;
            Object engine   = PBEUtil.createEngine(algorithm);

            if (engine is BufferedBlockCipher)
            {
                BufferedBlockCipher cipher = (BufferedBlockCipher)engine;

                cipher.init(true, keyParameters);

                byte[] keyBytes = keyInfo.getEncoded();

                int encLen = cipher.getOutputSize(keyBytes.Length);

                encoding = new byte[encLen];

                int off = cipher.processBytes(keyBytes, 0, keyBytes.Length, encoding, 0);

                cipher.doFinal(encoding, off);
            }
            else if (engine is StreamCipher)
            {
                StreamCipher cipher = (StreamCipher)engine;

                cipher.init(true, keyParameters);

                byte[] keyBytes = keyInfo.getEncoded();

                encoding = new byte[keyBytes.Length];

                cipher.processBytes(keyBytes, 0, keyBytes.Length, encoding, 0);
            }

            return(new EncryptedPrivateKeyInfo(new AlgorithmIdentifier(PBEUtil.getObjectIdentifier(algorithm), parameters), encoding));
        }