예제 #1
0
        /// <summary>
        /// Generate a public secret key pair.
        /// </summary>
        /// <param name="publicKey">The stream where public key data is written to.</param>
        /// <param name="secretKey">The stream where secret key data is written to.</param>
        /// <param name="identity">The unique identity of the public secret key pair (Name (comments) &lt;[email protected]&gt;).</param>
        /// <param name="password">The password used to protect the secret key.</param>
        /// <param name="isCritical">True, if should be treated as critical, false otherwise.</param>
        /// <param name="secondsKeyValid">The number of seconds the key is valid, or zero if no expiry.</param>
        /// <param name="secondsSignatureValid">The number of seconds the signature is valid, or zero if no expiry.</param>
        /// <param name="protectedKeys">Should the public and secret key data be protected.</param>
        /// <param name="publicExponent">The public exponent (e; the public key is now represented as {e, n}).</param>
        /// <param name="strength">The strength of the cipher.</param>
        /// <param name="hashAlgorithm">The preferred hash algorithm to use to create the hash value.</param>
        /// <param name="publicKeyAlgorithm">The public key algorithm type.</param>
        /// <param name="certificateLevel">The certification level.</param>
        /// <param name="symmetricKeyAlgorithm">The symmetric key algorithm used for cryptography.</param>
        /// <returns>The unique key id of the public secret key pair.</returns>
        public long Generate(System.IO.Stream publicKey, System.IO.Stream secretKey, Openpgp.Identity identity,
                             string password, bool isCritical = false, long secondsKeyValid = 0, long secondsSignatureValid = 0,
                             bool protectedKeys = true, long publicExponent = 3, int strength = 4096, Nequeo.Cryptography.HashcodeType hashAlgorithm = HashcodeType.SHA512,
                             Openpgp.PublicKeyAlgorithmType publicKeyAlgorithm = Openpgp.PublicKeyAlgorithmType.RsaGeneral,
                             Openpgp.CertificateLevelType certificateLevel     = Openpgp.CertificateLevelType.DefaultCertification,
                             Nequeo.Cryptography.SymmetricKeyAlgorithmType symmetricKeyAlgorithm = Nequeo.Cryptography.SymmetricKeyAlgorithmType.Aes256)
        {
            // Create the rsa key paramaters from the strength and public exponent.
            Key.Crypto.Generators.RsaKeyPairGenerator        keyPair      = new Key.Crypto.Generators.RsaKeyPairGenerator();
            Key.Crypto.Parameters.RsaKeyGenerationParameters keyPairParam =
                new Key.Crypto.Parameters.RsaKeyGenerationParameters(
                    Key.Math.BigInteger.ValueOf(publicExponent), new Key.Security.SecureRandom(), strength, 25);

            // Initialise the parameters and generate the public private key pair.
            keyPair.Init(keyPairParam);
            Key.Crypto.AsymmetricCipherKeyPair rsaKeyPair = keyPair.GenerateKeyPair();

            // Seperate the keys.
            Key.Crypto.Parameters.RsaKeyParameters           rsaPrivatePublic   = (Key.Crypto.Parameters.RsaKeyParameters)rsaKeyPair.Public;
            Key.Crypto.Parameters.RsaPrivateCrtKeyParameters rsaCrtPrivateParam = (Key.Crypto.Parameters.RsaPrivateCrtKeyParameters)rsaKeyPair.Private;

            // If file is not protected.
            if (!protectedKeys)
            {
                secretKey = new Key.Bcpg.ArmoredOutputStream(secretKey);
            }

            // Create the signature subpackets.
            Key.Bcpg.OpenPgp.PgpSignatureSubpacketGenerator signatureSubpacketGenerator = new Key.Bcpg.OpenPgp.PgpSignatureSubpacketGenerator();
            signatureSubpacketGenerator.SetKeyExpirationTime(isCritical, secondsKeyValid);
            signatureSubpacketGenerator.SetPreferredHashAlgorithms(isCritical, new int[] { (int)Openpgp.PublicSecretKey.GetHashAlgorithm(hashAlgorithm) });
            signatureSubpacketGenerator.SetSignatureExpirationTime(isCritical, secondsSignatureValid);

            // Create the signature subpackets.
            Key.Bcpg.OpenPgp.PgpSignatureSubpacketGenerator signatureSubpacketUnHashedGenerator = new Key.Bcpg.OpenPgp.PgpSignatureSubpacketGenerator();
            signatureSubpacketUnHashedGenerator.SetKeyExpirationTime(isCritical, secondsKeyValid);
            signatureSubpacketUnHashedGenerator.SetPreferredHashAlgorithms(isCritical, new int[] { (int)Openpgp.PublicSecretKey.GetHashAlgorithm(hashAlgorithm) });
            signatureSubpacketUnHashedGenerator.SetSignatureExpirationTime(isCritical, secondsSignatureValid);

            // Generate the packets
            Key.Bcpg.OpenPgp.PgpSignatureSubpacketVector hashedPackets   = signatureSubpacketGenerator.Generate();
            Key.Bcpg.OpenPgp.PgpSignatureSubpacketVector unhashedPackets = signatureSubpacketUnHashedGenerator.Generate();

            // Create the secret key.
            Key.Bcpg.OpenPgp.PgpSecretKey pgpSecretKey = new Key.Bcpg.OpenPgp.PgpSecretKey
                                                         (
                GetCertificateLevelType(certificateLevel),
                GetPublicKeyAlgorithm(publicKeyAlgorithm),
                rsaPrivatePublic,
                rsaCrtPrivateParam,
                DateTime.UtcNow,
                identity.ToString(),
                Openpgp.PublicSecretKey.GetSymmetricKeyAlgorithm(symmetricKeyAlgorithm),
                password.ToArray(),
                true,
                hashedPackets,
                unhashedPackets,
                new Key.Security.SecureRandom(),
                Openpgp.PublicSecretKey.GetHashAlgorithm(hashAlgorithm)
                                                         );

            // Encode the secret key.
            pgpSecretKey.Encode(secretKey);

            // If file is not protected.
            if (!protectedKeys)
            {
                secretKey.Close();
                publicKey = new Key.Bcpg.ArmoredOutputStream(publicKey);
            }

            // Get the public key from the secret key.
            Key.Bcpg.OpenPgp.PgpPublicKey pgpPublicKey = pgpSecretKey.PublicKey;
            pgpPublicKey.Encode(publicKey);

            // If file is not protected.
            if (!protectedKeys)
            {
                publicKey.Close();
            }

            // Return the key id.
            return(pgpSecretKey.KeyId);
        }
예제 #2
0
        /// <summary>
        /// Generate a public secret key pair RSA crypto service provider.
        /// </summary>
        /// <param name="publicKey">The stream where public key data is written to.</param>
        /// <param name="secretKey">The stream where secret key data is written to.</param>
        /// <param name="identity">The unique identity of the public secret key pair (Name (comments) &lt;[email protected]&gt;).</param>
        /// <param name="password">The password used to protect the secret key.</param>
        /// <param name="keyID">The unique key id of the public secret key pair.</param>
        /// <param name="isCritical">True, if should be treated as critical, false otherwise.</param>
        /// <param name="secondsKeyValid">The number of seconds the key is valid, or zero if no expiry.</param>
        /// <param name="secondsSignatureValid">The number of seconds the signature is valid, or zero if no expiry.</param>
        /// <param name="protectedKeys">Should the public and secret key data be protected.</param>
        /// <param name="publicExponent">The public exponent (e; the public key is now represented as {e, n}).</param>
        /// <param name="strength">The strength of the cipher.</param>
        /// <param name="hashAlgorithm">The preferred hash algorithm to use to create the hash value.</param>
        /// <param name="symmetricKeyAlgorithm">The symmetric key algorithm used for cryptography.</param>
        /// <returns>The RSA cryto service provider.</returns>
        public RSACryptoServiceProvider Generate(System.IO.Stream publicKey, System.IO.Stream secretKey, string identity, string password,
                                                 out long keyID, bool isCritical = false, long secondsKeyValid = 0, long secondsSignatureValid = 0,
                                                 bool protectedKeys = true, long publicExponent = 3, int strength = 4096,
                                                 Nequeo.Cryptography.HashcodeType hashAlgorithm = HashcodeType.SHA512,
                                                 Nequeo.Cryptography.SymmetricKeyAlgorithmType symmetricKeyAlgorithm = Nequeo.Cryptography.SymmetricKeyAlgorithmType.Aes256)
        {
            // Create the rsa key paramaters from the strength and public exponent.
            Key.Crypto.Generators.RsaKeyPairGenerator        keyPair      = new Key.Crypto.Generators.RsaKeyPairGenerator();
            Key.Crypto.Parameters.RsaKeyGenerationParameters keyPairParam =
                new Key.Crypto.Parameters.RsaKeyGenerationParameters(
                    Key.Math.BigInteger.ValueOf(publicExponent), new Key.Security.SecureRandom(), strength, 25);

            // Initialise the parameters and generate the public private key pair.
            keyPair.Init(keyPairParam);
            Key.Crypto.AsymmetricCipherKeyPair rsaKeyPair = keyPair.GenerateKeyPair();

            // Seperate the keys.
            Key.Crypto.Parameters.RsaKeyParameters           rsaPrivatePublic   = (Key.Crypto.Parameters.RsaKeyParameters)rsaKeyPair.Public;
            Key.Crypto.Parameters.RsaPrivateCrtKeyParameters rsaCrtPrivateParam = (Key.Crypto.Parameters.RsaPrivateCrtKeyParameters)rsaKeyPair.Private;

            // If file is not protected.
            if (!protectedKeys)
            {
                secretKey = new Key.Bcpg.ArmoredOutputStream(secretKey);
            }

            // Create the signature subpackets.
            Key.Bcpg.OpenPgp.PgpSignatureSubpacketGenerator signatureSubpacketGenerator = new Key.Bcpg.OpenPgp.PgpSignatureSubpacketGenerator();
            signatureSubpacketGenerator.SetKeyExpirationTime(isCritical, secondsKeyValid);
            signatureSubpacketGenerator.SetPreferredHashAlgorithms(isCritical, new int[] { (int)GetHashAlgorithm(hashAlgorithm) });
            signatureSubpacketGenerator.SetSignatureExpirationTime(isCritical, secondsSignatureValid);

            // Create the signature subpackets.
            Key.Bcpg.OpenPgp.PgpSignatureSubpacketGenerator signatureSubpacketUnHashedGenerator = new Key.Bcpg.OpenPgp.PgpSignatureSubpacketGenerator();
            signatureSubpacketUnHashedGenerator.SetKeyExpirationTime(isCritical, secondsKeyValid);
            signatureSubpacketUnHashedGenerator.SetPreferredHashAlgorithms(isCritical, new int[] { (int)GetHashAlgorithm(hashAlgorithm) });
            signatureSubpacketUnHashedGenerator.SetSignatureExpirationTime(isCritical, secondsSignatureValid);

            // Generate the packets
            Key.Bcpg.OpenPgp.PgpSignatureSubpacketVector hashedPackets   = signatureSubpacketGenerator.Generate();
            Key.Bcpg.OpenPgp.PgpSignatureSubpacketVector unhashedPackets = signatureSubpacketUnHashedGenerator.Generate();

            // Create the secret key.
            Key.Bcpg.OpenPgp.PgpSecretKey pgpSecretKey = new Key.Bcpg.OpenPgp.PgpSecretKey
                                                         (
                Key.Bcpg.OpenPgp.PgpSignature.DefaultCertification,
                Key.Bcpg.PublicKeyAlgorithmTag.RsaGeneral,
                rsaPrivatePublic,
                rsaCrtPrivateParam,
                DateTime.UtcNow,
                identity,
                GetSymmetricKeyAlgorithm(symmetricKeyAlgorithm),
                password.ToArray(),
                true,
                hashedPackets,
                unhashedPackets,
                new Key.Security.SecureRandom(),
                GetHashAlgorithm(hashAlgorithm)
                                                         );

            // Encode the secret key.
            pgpSecretKey.Encode(secretKey);

            // If file is not protected.
            if (!protectedKeys)
            {
                secretKey.Close();
                publicKey = new Key.Bcpg.ArmoredOutputStream(publicKey);
            }

            // Get the public key from the secret key.
            Key.Bcpg.OpenPgp.PgpPublicKey pgpPublicKey = pgpSecretKey.PublicKey;
            pgpPublicKey.Encode(publicKey);

            // If file is not protected.
            if (!protectedKeys)
            {
                publicKey.Close();
            }

            // Assign the rsa parameters.
            RSAParameters rsaParam = new RSAParameters();

            rsaParam.D        = rsaCrtPrivateParam.Exponent.ToByteArrayUnsigned();
            rsaParam.DP       = rsaCrtPrivateParam.DP.ToByteArrayUnsigned();
            rsaParam.DQ       = rsaCrtPrivateParam.DQ.ToByteArrayUnsigned();
            rsaParam.InverseQ = rsaCrtPrivateParam.QInv.ToByteArrayUnsigned();
            rsaParam.P        = rsaCrtPrivateParam.P.ToByteArrayUnsigned();
            rsaParam.Q        = rsaCrtPrivateParam.Q.ToByteArrayUnsigned();
            rsaParam.Modulus  = rsaCrtPrivateParam.Modulus.ToByteArrayUnsigned();
            rsaParam.Exponent = rsaCrtPrivateParam.PublicExponent.ToByteArrayUnsigned();

            // Create the encyption provider.
            RSACryptoServiceProvider rsaProvider = new RSACryptoServiceProvider();

            rsaProvider.ImportParameters(rsaParam);

            // Return the rsa provider.
            keyID = pgpSecretKey.KeyId;
            return(rsaProvider);
        }