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);
        }
        public static EncryptedPrivateKeyInfo CreateEncryptedPrivateKeyInfo(
            string			algorithm,
            char[]			passPhrase,
            byte[]			salt,
            int				iterationCount,
            PrivateKeyInfo	keyInfo)
        {
            IBufferedCipher cipher = PbeUtilities.CreateEngine(algorithm) as IBufferedCipher;
            if (cipher == null)
                throw new Exception("Unknown encryption algorithm: " + algorithm);

            Asn1Encodable pbeParameters = PbeUtilities.GenerateAlgorithmParameters(
                algorithm, salt, iterationCount);
            ICipherParameters cipherParameters = PbeUtilities.GenerateCipherParameters(
                algorithm, passPhrase, pbeParameters);
            cipher.Init(true, cipherParameters);
            byte[] encoding = cipher.DoFinal(keyInfo.GetEncoded());

            DerObjectIdentifier oid = PbeUtilities.GetObjectIdentifier(algorithm);
            AlgorithmIdentifier algID = new AlgorithmIdentifier(oid, pbeParameters);
            return new EncryptedPrivateKeyInfo(algID, encoding);
        }
Ejemplo n.º 3
0
        private void EncodePrivateKey()
        {
            X9ECParameters ecP = X962NamedCurves.GetByOid(X9ObjectIdentifiers.Prime239v3);

            //
            // named curve
            //
            X962Parameters _params = new X962Parameters(X9ObjectIdentifiers.Prime192v1);

            X9ECPoint pPoint = new X9ECPoint(
                new FpPoint(ecP.Curve, new FpFieldElement(BigInteger.Two, BigInteger.One),
                new FpFieldElement(BigInteger.ValueOf(4), BigInteger.ValueOf(3)),
                true));

            Asn1OctetString p = (Asn1OctetString) pPoint.ToAsn1Object();

            if (p == null)
                Fail("failed to convert to ASN.1");

            PrivateKeyInfo info = new PrivateKeyInfo(new AlgorithmIdentifier(X9ObjectIdentifiers.IdECPublicKey, _params), new ECPrivateKeyStructure(BigInteger.Ten).ToAsn1Object());

            if (!Arrays.AreEqual(info.GetEncoded(), namedPriv))
            {
                Fail("failed private named generation");
            }

            Asn1Object o = Asn1Object.FromByteArray(namedPriv);

            if (!info.Equals(o))
            {
                Fail("failed private named equality");
            }

            //
            // explicit curve parameters
            //
            _params = new X962Parameters(ecP);

            info = new PrivateKeyInfo(new AlgorithmIdentifier(X9ObjectIdentifiers.IdECPublicKey, _params), new ECPrivateKeyStructure(BigInteger.ValueOf(20)).ToAsn1Object());

            if (!Arrays.AreEqual(info.GetEncoded(), expPriv))
            {
                Fail("failed private explicit generation");
            }

            o = Asn1Object.FromByteArray(expPriv);

            if (!info.Equals(o))
            {
                Fail("failed private explicit equality");
            }
        }
Ejemplo n.º 4
0
        private void EncodePrivateKey()
        {
            X9ECParameters ecP = X962NamedCurves.GetByOid(X9ObjectIdentifiers.Prime192v1);

            //
            // named curve
            //
            X962Parameters _params = new X962Parameters(X9ObjectIdentifiers.Prime192v1);

            PrivateKeyInfo info = new PrivateKeyInfo(new AlgorithmIdentifier(X9ObjectIdentifiers.IdECPublicKey, _params),
                new ECPrivateKeyStructure(ecP.N.BitLength, BigInteger.Ten).ToAsn1Object());

            if (!Arrays.AreEqual(info.GetEncoded(), namedPriv))
            {
                Fail("failed private named generation");
            }

            Asn1Object o = Asn1Object.FromByteArray(namedPriv);

            if (!info.Equals(o))
            {
                Fail("failed private named equality");
            }

            //
            // explicit curve parameters
            //
            ecP = X962NamedCurves.GetByOid(X9ObjectIdentifiers.Prime239v3);

            _params = new X962Parameters(ecP);

            info = new PrivateKeyInfo(new AlgorithmIdentifier(X9ObjectIdentifiers.IdECPublicKey, _params),
                new ECPrivateKeyStructure(ecP.N.BitLength, BigInteger.ValueOf(20)).ToAsn1Object());

            if (!Arrays.AreEqual(info.GetEncoded(), expPriv))
            {
                Fail("failed private explicit generation");
            }

            o = Asn1Object.FromByteArray(expPriv);

            if (!info.Equals(o))
            {
                Fail("failed private explicit equality");
            }
        }