コード例 #1
0
    private static PgpPublicKey ReadPublicKey(Stream inputStream)
    {
        inputStream = PgpUtilities.GetDecoderStream(inputStream);

        PgpPublicKeyRingBundle pgpPub = new PgpPublicKeyRingBundle(inputStream);

        //
        // we just loop through the collection till we find a key suitable for encryption, in the real
        // world you would probably want to be a bit smarter about this.
        //

        //
        // iterate through the key rings.
        //

        foreach (PgpPublicKeyRing kRing in pgpPub.GetKeyRings())
        {
            foreach (PgpPublicKey k in kRing.GetPublicKeys())
            {
                if (k.IsEncryptionKey)
                {
                    return(k);
                }
            }
        }

        throw new ArgumentException("Can't find encryption key in key ring.");
    }
コード例 #2
0
        private static PgpPublicKey ReadPublicKey(Stream publicKeyStream)
        {
            if (publicKeyStream.CanSeek)
            {
                publicKeyStream.Seek(0, SeekOrigin.Begin);
            }

            PgpPublicKeyRingBundle pgpPub = new PgpPublicKeyRingBundle(PgpUtilities.GetDecoderStream(publicKeyStream));

            //
            // we just loop through the collection till we find a key suitable for encryption, in the real
            // world you would probably want to be a bit smarter about this.
            //

            foreach (PgpPublicKeyRing keyRing in pgpPub.GetKeyRings())
            {
                foreach (PgpPublicKey key in keyRing.GetPublicKeys())
                {
                    if (key.IsEncryptionKey)
                    {
                        return(key);
                    }
                }
            }

            throw new ArgumentException("Can't find encryption key in key ring.");
        }
コード例 #3
0
        public static IEnumerable <PgpPublicKeyMetaData> GetPublicKeys(Stream inputStream) //, bool disallowPrivateKeys)
        {
            inputStream = PgpUtilities.GetDecoderStream(inputStream);

            //if (disallowPrivateKeys)
            //{
            //    PgpSecretKeyRingBundle pgpKeyRing = null;
            //    try
            //    {
            //        pgpKeyRing = new PgpSecretKeyRingBundle(inputStream);
            //    }
            //    catch
            //    {
            //    }
            //    if (pgpKeyRing != null && pgpKeyRing.Count > 0)
            //    {
            //        throw new System.Security.SecurityException("Private keys are not allowed.");
            //    }
            //}

            var list = new List <PgpPublicKeyMetaData>();

            PgpPublicKeyRingBundle pgpPub = new PgpPublicKeyRingBundle(inputStream);

            foreach (PgpPublicKeyRing keyRing in pgpPub.GetKeyRings())
            {
                foreach (PgpPublicKey key in keyRing.GetPublicKeys())
                {
                    var keyMeta = new PgpPublicKeyMetaData();
                    keyMeta.Load(key);
                    list.Add(keyMeta);
                }
            }
            return(list);
        }
コード例 #4
0
        private PgpPublicKey getFirstPublicKey(PgpPublicKeyRingBundle publicKeyRingBundle)
        {
            foreach (PgpPublicKeyRing kRing in publicKeyRingBundle.GetKeyRings())
            {
                /*
                 * PgpPublicKey key = kRing.GetPublicKeys()
                 *  .Cast<PgpPublicKey>()
                 *  .Where(k => k.IsEncryptionKey)
                 *  .FirstOrDefault();
                 *
                 * if (key != null)
                 * {
                 *  return key;
                 * }
                 */
                IEnumerator ikeys = kRing.GetPublicKeys().GetEnumerator();
                if (ikeys.MoveNext())
                {
                    PgpPublicKey key = (PgpPublicKey)ikeys.Current;
                    if (key.IsEncryptionKey)
                    {
                        return(key);
                    }
                }
            }

            return(null);
        }
コード例 #5
0
    private PgpPublicKey GetPublicKey(string public_key_path)
    {
        PgpPublicKey public_key = null;

        using (Stream keyin = File.OpenRead(public_key_path))
        {
            using (Stream public_key_stream = PgpUtilities.GetDecoderStream(keyin))
            {
                PgpPublicKeyRingBundle public_key_bundle = new PgpPublicKeyRingBundle(public_key_stream);

                foreach (PgpPublicKeyRing public_key_ring in public_key_bundle.GetKeyRings())
                {
                    foreach (PgpPublicKey key in public_key_ring.GetPublicKeys())
                    {
                        long modified_key_id = key.KeyId & 0x00000000FFFFFFFF;

                        if (modified_key_id == m_KeyId)
                        {
                            public_key = key;
                            break;
                        }
                    }
                }

                if (public_key == null)
                {
                    throw new Exception("The public key value is null");
                }
            }
            return(public_key);
        }
    }
コード例 #6
0
ファイル: PgpCrypto.cs プロジェクト: ktw/OutlookPrivacyPlugin
        public string[] GetPublicKeyUserIdsForSign()
        {
            using (var inputStream = File.OpenRead(Context.PublicKeyRingFile))
            {
                using (var decoderStream = PgpUtilities.GetDecoderStream(inputStream))
                {
                    var pgpPub     = new PgpPublicKeyRingBundle(decoderStream);
                    var keyUserIds = new List <string>();

                    foreach (PgpPublicKeyRing kRing in pgpPub.GetKeyRings())
                    {
                        foreach (PgpPublicKey k in kRing.GetPublicKeys())
                        {
                            if (!IsSigningKey(k))
                            {
                                continue;
                            }

                            foreach (string id in k.GetUserIds())
                            {
                                keyUserIds.Add(id);
                            }
                        }
                    }

                    return(keyUserIds.ToArray());
                }
            }
        }
コード例 #7
0
        private List <PgpPublicKey> GetAllSignatureKeys()
        {
            List <PgpPublicKey> PgpSignatureKeys = new List <PgpPublicKey>();

            foreach (var publicKeyStream in this.SignatureKeys)
            {
                if (publicKeyStream.CanSeek)
                {
                    publicKeyStream.Seek(0, SeekOrigin.Begin);
                }

                PgpPublicKeyRingBundle pgpPub = new PgpPublicKeyRingBundle(PgpUtilities.GetDecoderStream(publicKeyStream));

                foreach (PgpPublicKeyRing keyRing in pgpPub.GetKeyRings())
                {
                    foreach (PgpPublicKey key in keyRing.GetPublicKeys())
                    {
                        if (key.IsEncryptionKey)
                        {
                            PgpSignatureKeys.Add(key);
                        }
                    }
                }
            }

            return(PgpSignatureKeys);
        }
コード例 #8
0
        private PgpPublicKey GetPublicKeyByUserId(PgpPublicKeyRingBundle publicKeyRingBundle, String publicKeyUserId)
        {
            foreach (PgpPublicKeyRing kRing in publicKeyRingBundle.GetKeyRings())
            {
                PgpPublicKey key = kRing.GetPublicKeys()

                                   .Cast <PgpPublicKey>()

                                   .Where(k => k.IsEncryptionKey)

                                   .FirstOrDefault();


                if (key != null)
                {
                    foreach (String userId in key.GetUserIds())
                    {
                        if (userId.Contains(publicKeyUserId))
                        {
                            return(key);
                        }
                    }
                }
            }

            return(null);
        }
コード例 #9
0
        public String EncryptKeycode(String publicKeyStr, String unencryptedKeycode)
        {
            byte[] unencryptedByteArray = System.Text.Encoding.ASCII.GetBytes(unencryptedKeycode);
            byte[] decodedPublicKey     = System.Text.Encoding.ASCII.GetBytes(publicKeyStr);

            PgpPublicKey key = null;

            Stream decodedStream            = PgpUtilities.GetDecoderStream(new MemoryStream(decodedPublicKey));
            PgpPublicKeyRingBundle pubRings = new PgpPublicKeyRingBundle(decodedStream);

            foreach (PgpPublicKeyRing pgpPub in pubRings.GetKeyRings())
            {
                foreach (PgpPublicKey publicKey in pgpPub.GetPublicKeys())
                {
                    if (publicKey.IsEncryptionKey)
                    {
                        key = publicKey;
                        break;
                    }
                }
            }

            if (key == null)
            {
                throw new SendSafely.Exceptions.InvalidKeyException("Can't find encryption key in key ring.");
            }

            PgpEncryptedDataGenerator cPk = new PgpEncryptedDataGenerator(SymmetricKeyAlgorithmTag.Aes256, true);

            cPk.AddMethod(key);

            PgpLiteralDataGenerator lData = new PgpLiteralDataGenerator();

            // Write the data to a literal
            MemoryStream bOut;

            using (bOut = new MemoryStream())
            {
                using (Stream pOut = lData.Open(bOut, PgpLiteralData.Binary, PgpLiteralData.Console, unencryptedByteArray.Length, DateTime.Now))
                {
                    pOut.Write(unencryptedByteArray, 0, unencryptedByteArray.Length);
                }
            }
            lData.Close();

            byte[] bytes = bOut.ToArray();

            MemoryStream encOut = new MemoryStream();

            using (ArmoredOutputStream armoredOut = new ArmoredOutputStream(encOut))
            {
                using (Stream cOut = cPk.Open(armoredOut, bytes.Length))
                {
                    cOut.Write(bytes, 0, bytes.Length);
                }
            }

            return(System.Text.Encoding.Default.GetString(encOut.ToArray()));
        }
コード例 #10
0
        /// <summary>
        /// Verifys the signature of a RPM package.
        /// </summary>
        /// <param name="pgpPublicKey">
        /// A <see cref="Stream"/> which contains the public key used to verify the data.
        /// </param>
        /// <returns>
        /// <see langword="true"/> if the verification was successful; otherwise, <see langword="false"/>.
        /// </returns>
        public bool Verify(Stream pgpPublicKeyStream)
        {
            var bundle = new PgpPublicKeyRingBundle(PgpUtilities.GetDecoderStream(pgpPublicKeyStream));
            var publicKeyRings = bundle.GetKeyRings().OfType<PgpPublicKeyRing>();
            var publicKeys = publicKeyRings.SelectMany(x => x.GetPublicKeys().OfType<PgpPublicKey>());
            var publicKey = publicKeys.FirstOrDefault();

            return Verify(publicKey);
        }
コード例 #11
0
 private PgpPublicKey GetFirstPublicKey(PgpPublicKeyRingBundle publicKeyRingBundle)
 {
     foreach (PgpPublicKeyRing keyRing in publicKeyRingBundle.GetKeyRings())
     {
         PgpPublicKey key = keyRing.GetPublicKeys().Cast <PgpPublicKey>().Where(i => i.IsEncryptionKey).FirstOrDefault();
         if (key != null)
         {
             return(key);
         }
     }
     return(null);
 }
コード例 #12
0
ファイル: PgpCrypto.cs プロジェクト: ktw/OutlookPrivacyPlugin
        public PgpPublicKey GetPublicKeyForEncryption(string email)
        {
            using (var inputStream = File.OpenRead(Context.PublicKeyRingFile))
            {
                using (var decoderStream = PgpUtilities.GetDecoderStream(inputStream))
                {
                    var pgpPub      = new PgpPublicKeyRingBundle(decoderStream);
                    var emailSearch = "<" + email.ToLower().Trim() + ">";

                    foreach (PgpPublicKeyRing kRing in pgpPub.GetKeyRings())
                    {
                        foreach (PgpPublicKey k in kRing.GetPublicKeys())
                        {
                            if (!IsEncryptionKey(k))
                            {
                                continue;
                            }

                            if (!k.IsMasterKey)
                            {
                                foreach (PgpSignature sig in k.GetSignaturesOfType(24))
                                {
                                    var pubKey = this.GetPublicKey(sig.KeyId);
                                    if (!pubKey.IsMasterKey)
                                    {
                                        continue;
                                    }

                                    foreach (string id in pubKey.GetUserIds())
                                    {
                                        if (id.ToLower().IndexOf(emailSearch) > -1)
                                        {
                                            return(k);
                                        }
                                    }
                                }
                            }

                            foreach (string id in k.GetUserIds())
                            {
                                if (id.ToLower().IndexOf(emailSearch) > -1)
                                {
                                    return(k);
                                }
                            }
                        }
                    }

                    return(null);
                }
            }
        }
コード例 #13
0
    public static string ReadKeyDirectly(string stringKeyData)
    {
        Stream fs = new MemoryStream(System.Text.Encoding.ASCII.GetBytes(stringKeyData));

        fs.Seek(0, SeekOrigin.Begin);
        PgpPublicKeyRingBundle pubRings = new PgpPublicKeyRingBundle(PgpUtilities.GetDecoderStream(fs));

        foreach (PgpPublicKeyRing pubRing in pubRings.GetKeyRings())
        {
            return(Convert.ToBase64String(pubRing.GetEncoded()));
        }
        return(null);
    }
コード例 #14
0
ファイル: PgpCrypto.cs プロジェクト: ktw/OutlookPrivacyPlugin
        public string[] GetPublicKeyUserIdsForEncryption()
        {
            using (var inputStream = File.OpenRead(Context.PublicKeyRingFile))
            {
                using (var decoderStream = PgpUtilities.GetDecoderStream(inputStream))
                {
                    var pgpPub     = new PgpPublicKeyRingBundle(decoderStream);
                    var keyUserIds = new List <string>();

                    foreach (PgpPublicKeyRing kRing in pgpPub.GetKeyRings())
                    {
                        foreach (PgpPublicKey k in kRing.GetPublicKeys())
                        {
                            if (!IsEncryptionKey(k))
                            {
                                continue;
                            }

                            if (!k.IsMasterKey)
                            {
                                foreach (PgpSignature sig in k.GetSignaturesOfType(24))
                                {
                                    var pubKey = this.GetPublicKey(sig.KeyId);
                                    if (!pubKey.IsMasterKey)
                                    {
                                        continue;
                                    }

                                    foreach (string id in pubKey.GetUserIds())
                                    {
                                        if (!keyUserIds.Contains(id))
                                        {
                                            keyUserIds.Add(id);
                                        }
                                    }
                                }
                            }

                            foreach (string id in k.GetUserIds())
                            {
                                keyUserIds.Add(id);
                            }
                        }
                    }

                    return(keyUserIds.ToArray());
                }
            }
        }
コード例 #15
0
        protected PgpPublicKey GetFirstPublicKey(PgpPublicKeyRingBundle PGP_PublicKeyRingBundle)
        {
            foreach (PgpPublicKeyRing PGP_Public_kRing in PGP_PublicKeyRingBundle.GetKeyRings())
            {
                foreach (PgpPublicKey PGP_PublicKey in PGP_Public_kRing.GetPublicKeys())
                {
                    if (PGP_PublicKey.IsEncryptionKey)
                    {
                        return(PGP_PublicKey);
                    }
                }
            }

            return(null);
        }
コード例 #16
0
        public static void Main(
            string[] args)
        {
            Stream fs = File.OpenRead(args[0]);

            //
            // Read the public key rings
            //
            PgpPublicKeyRingBundle pubRings = new PgpPublicKeyRingBundle(
                PgpUtilities.GetDecoderStream(fs));

            fs.Close();

            foreach (PgpPublicKeyRing pgpPub in pubRings.GetKeyRings())
            {
                try
                {
                    //PgpPublicKey pubKey =
                    pgpPub.GetPublicKey();
                }
                catch (Exception e)
                {
                    Console.Error.WriteLine(e.Message);
                    Console.Error.WriteLine(e.StackTrace);
                    continue;
                }

                bool first = true;

                foreach (PgpPublicKey pgpKey in pgpPub.GetPublicKeys())
                {
                    if (first)
                    {
                        Console.WriteLine("Key ID: " + pgpKey.KeyId.ToString("X"));
                        first = false;
                    }
                    else
                    {
                        Console.WriteLine("Key ID: " + pgpKey.KeyId.ToString("X") + " (subkey)");
                    }

                    Console.WriteLine("            Algorithm: " + GetAlgorithm(pgpKey.Algorithm));
                    Console.WriteLine("            Fingerprint: " + Hex.ToHexString(pgpKey.GetFingerprint()));
                }
            }
        }
コード例 #17
0
ファイル: PgpFileEncryption.cs プロジェクト: sahvishal/matrix
        private PgpPublicKey ReadPublicKey(Stream input)
        {
            var pgpPub = new PgpPublicKeyRingBundle(PgpUtilities.GetDecoderStream(input));

            foreach (PgpPublicKeyRing keyRing in pgpPub.GetKeyRings())
            {
                foreach (PgpPublicKey key in keyRing.GetPublicKeys())
                {
                    if (key.IsEncryptionKey)
                    {
                        return(key);
                    }
                }
            }

            throw new ArgumentException("Can't find encryption key in key ring.");
        }
コード例 #18
0
        /// <summary>
        /// Opens a key ring file and loads the first available key suitable for encryption.
        /// </summary>
        /// <param name="inputStream"></param>
        /// <returns></returns>
        /// <exception cref="IOException"></exception>
        /// <exception cref="PgpException"></exception>
        internal static PgpPublicKey ReadPublicKey(Stream inputStream)
        {
            PgpPublicKeyRingBundle _pgpPubKeyRingBundle = new PgpPublicKeyRingBundle(PgpUtilities.GetDecoderStream(inputStream));

            foreach (PgpPublicKeyRing _pgpPubKeyRing in _pgpPubKeyRingBundle.GetKeyRings())
            {
                foreach (PgpPublicKey _pgpPubKey in _pgpPubKeyRing.GetPublicKeys())
                {
                    if (_pgpPubKey.IsEncryptionKey)
                    {
                        return(_pgpPubKey);
                    }
                }
            }

            throw new ArgumentException("Encryption key not found in key ring.");
        }
コード例 #19
0
 public static PgpPublicKey LoadPublicKeyFromString(string key)
 {
     using (Stream s = Tools.GenerateStreamFromString(key)) {
         var pgp = new PgpPublicKeyRingBundle(PgpUtilities.GetDecoderStream(s));
         foreach (PgpPublicKeyRing keyRing in pgp.GetKeyRings())
         {
             foreach (PgpPublicKey publicKey in keyRing.GetPublicKeys())
             {
                 if (publicKey.IsEncryptionKey)
                 {
                     return(publicKey);
                 }
             }
         }
     }
     return(null);
 }
コード例 #20
0
ファイル: Form1.cs プロジェクト: rm0909685/Capstone
        /// <summary>
        /// Reads the public encryption keys from the input stream.
        /// </summary>
        /// <param name="inputStream">The input stream.</param>
        /// <returns>The PGP public encryption keys.</returns>
        public static IEnumerable <PgpPublicKey> ReadPublicEncryptionKeys(Stream inputStream)
        {
            inputStream = PgpUtilities.GetDecoderStream(inputStream);

            PgpPublicKeyRingBundle pkrb = new PgpPublicKeyRingBundle(inputStream);

            foreach (PgpPublicKeyRing pkr in pkrb.GetKeyRings())
            {
                foreach (PgpPublicKey pk in pkr.GetPublicKeys())
                {
                    if (pk.IsEncryptionKey)
                    {
                        yield return(pk);
                    }
                }
            }
        }
コード例 #21
0
        /// <summary>
        /// Creates an OpenPGP signing PgpPublicKey using an input stream
        /// </summary>
        /// <param name="inputStream">the input stream that contains the signing PgpPublicKey</param>
        /// <returns>A PgpPublicKey</returns>
        public static PgpPublicKey ReadPublicKey(Stream inputStream)
        {
            if (inputStream == null)
            {
                throw new ArgumentNullException("inputStream can not be null!");
            }

            try
            {
                inputStream = PgpUtilities.GetDecoderStream(inputStream);

                PgpPublicKeyRingBundle pgpPub = new PgpPublicKeyRingBundle(inputStream);

                //
                // we just loop through the collection till we find a key suitable for encryption, in the real
                // world you would probably want to be a bit smarter about this.
                //

                //
                // iterate through the key rings.
                //

                foreach (PgpPublicKeyRing kRing in pgpPub.GetKeyRings())
                {
                    foreach (PgpPublicKey k in kRing.GetPublicKeys())
                    {
                        //Console.WriteLine("k.IsEncryptionKey: " + k.IsEncryptionKey);
                        //Console.WriteLine("k.IsMasterKey    : " + k.IsMasterKey);
                        //Console.WriteLine("k.KeyId          : " + k.KeyId);
                        //Console.WriteLine();

                        if (!k.IsEncryptionKey)
                        {
                            return(k);
                        }
                    }
                }

                throw new ArgumentException("Can't find signature PgpPublicKey key in key ring.");
            }
            finally
            {
                inputStream.Dispose();
            }
        }
コード例 #22
0
        public PgpPublicKey LoadPublicKey(Stream publicKey)
        {
            var armoredStream = PgpUtilities.GetDecoderStream(publicKey);
            var pgpPub        = new PgpPublicKeyRingBundle(armoredStream);

            foreach (PgpPublicKeyRing keyRing in pgpPub.GetKeyRings())
            {
                foreach (PgpPublicKey key in keyRing.GetPublicKeys())
                {
                    if (key.IsEncryptionKey)
                    {
                        return(key);
                    }
                }
            }

            throw new ArgumentException("Can't find encryption key in key ring.");
        }
コード例 #23
0
        private static PgpPublicKey ReadPublicKey(Stream inputStream)
        {
            inputStream = PgpUtilities.GetDecoderStream(inputStream);
            PgpPublicKeyRingBundle pgpPub = new PgpPublicKeyRingBundle(inputStream);

            foreach (PgpPublicKeyRing kRing in pgpPub.GetKeyRings())
            {
                foreach (PgpPublicKey k in kRing.GetPublicKeys())
                {
                    if (k.IsEncryptionKey)
                    {
                        return(k);
                    }
                }
            }

            throw new ArgumentException("Can't find encryption key in key ring.");
        }
コード例 #24
0
        private static PgpPublicKey ReadPublicKey(string publicKey)
        {
            var keyStream     = new MemoryStream(Encoding.ASCII.GetBytes(publicKey));
            var decoderStream = PgpUtilities.GetDecoderStream(keyStream);
            var keyBundle     = new PgpPublicKeyRingBundle(decoderStream);

            foreach (var keyRing in keyBundle.GetKeyRings().Cast <PgpPublicKeyRing>())
            {
                foreach (var key in keyRing.GetPublicKeys().Cast <PgpPublicKey>())
                {
                    if (key.IsEncryptionKey)
                    {
                        return(key);
                    }
                }
            }
            return(null);
        }
コード例 #25
0
        /**
         * A simple routine that opens a key ring file and loads the first available key
         * suitable for encryption.
         *
         * @param input
         * @return
         * @throws IOException
         * @throws PGPException
         */
        internal static PgpPublicKey ReadPublicKey(Stream input, string userId)
        {
            PgpPublicKeyRingBundle pgpPub = new PgpPublicKeyRingBundle(
                PgpUtilities.GetDecoderStream(input));

            foreach (PgpPublicKeyRing keyRing in pgpPub.GetKeyRings())
            {
                foreach (PgpPublicKey key in keyRing.GetPublicKeys())
                {

                    if (key.IsEncryptionKey &&  key.GetUserIds().OfType<string>().Any(x => x != null && x.Contains(userId)))
                    {
                        return key;
                    }
                }
            }

            throw new ArgumentException("Can't find encryption key in key ring.");
        }
コード例 #26
0
        private PgpPublicKey readPublicKey(Stream stream)
        {
            stream = PgpUtilities.GetDecoderStream(stream);
            PgpPublicKeyRingBundle pkBun = new PgpPublicKeyRingBundle(stream);
            var pkRings = pkBun.GetKeyRings();

            foreach (var ring in pkRings)
            {
                var pks = ((PgpPublicKeyRing)ring).GetPublicKeys();
                foreach (var key in pks)
                {
                    if (((PgpPublicKey)key).IsEncryptionKey)
                    {
                        return((PgpPublicKey)key);
                    }
                }
            }
            throw new ArgumentException("Invalid public key");
        }
コード例 #27
0
        public PgpPublicKey LoadPubKey(string path)
        {
            using (FileStream keyStream = File.OpenRead(path)) {
                Stream decStream = PgpUtilities.GetDecoderStream(keyStream);
                PgpPublicKeyRingBundle pgpPub = new PgpPublicKeyRingBundle(decStream);

                foreach (PgpPublicKeyRing keyRing in pgpPub.GetKeyRings())
                {
                    foreach (PgpPublicKey key in keyRing.GetPublicKeys())
                    {
                        if (key.IsEncryptionKey)
                        {
                            return(key);
                        }
                    }
                }
                throw new ArgumentException("Key not found");
            }
        }
コード例 #28
0
ファイル: OpenPgpContext.cs プロジェクト: xiaowei-asp/MimeKit
        /// <summary>
        /// Imports a public pgp keyring bundle.
        /// </summary>
        /// <remarks>
        /// Imports a public pgp keyring bundle.
        /// </remarks>
        /// <param name="bundle">The pgp keyring bundle.</param>
        /// <exception cref="System.ArgumentNullException">
        /// <paramref name="bundle"/> is <c>null</c>.
        /// </exception>
        public override void Import(PgpPublicKeyRingBundle bundle)
        {
            if (bundle == null)
            {
                throw new ArgumentNullException(nameof(bundle));
            }

            int publicKeysAdded = 0;

            foreach (PgpPublicKeyRing pubring in bundle.GetKeyRings())
            {
                PublicKeyRingBundle = PgpPublicKeyRingBundle.AddPublicKeyRing(PublicKeyRingBundle, pubring);
                publicKeysAdded++;
            }

            if (publicKeysAdded > 0)
            {
                SavePublicKeyRingBundle();
            }
        }
コード例 #29
0
ファイル: Utilities.cs プロジェクト: pefthymiou/PGPConsoleApp
        internal static PgpPublicKey ReadPublicKey(byte[] inputData)
        {
            using (Stream inputStream = PgpUtilities.GetDecoderStream(new MemoryStream(inputData)))
            {
                PgpPublicKeyRingBundle keyRingBundle = new PgpPublicKeyRingBundle(inputStream);

                foreach (PgpPublicKeyRing keyRing in keyRingBundle.GetKeyRings())
                {
                    foreach (PgpPublicKey publicKey in keyRing.GetPublicKeys())
                    {
                        if (publicKey.IsEncryptionKey)
                        {
                            return(publicKey);
                        }
                    }
                }
            }

            throw new ArgumentException("Can't find encryption key in key ring.");
        }
コード例 #30
0
 private static PgpPublicKey GetFirstPublicKey(PgpPublicKeyRingBundle publicKeyRingBundle)
 {
     foreach (PgpPublicKeyRing kRing in publicKeyRingBundle.GetKeyRings())
     {
         var keys = kRing.GetPublicKeys();
         foreach (var key in keys)
         {
             PgpPublicKey foundKey = (PgpPublicKey)key;
             //PgpPublicKey key = kRing.GetPublicKeys()
             //.Cast<PgpPublicKey>()
             // .Where(k => k.IsEncryptionKey)
             //  .FirstOrDefault();
             if (foundKey != null && foundKey.IsEncryptionKey)
             {
                 return(foundKey);
             }
         }
     }
     return(null);
 }
コード例 #31
0
        /// <summary>
        /// Find first suitable public key for encryption.
        /// </summary>
        /// <param name="publicKeyFile">Path to public key file</param>
        /// <returns>PgpPublicKey from public key file location</returns>
        internal static PgpPublicKey ReadPublicKey(string publicKeyFile)
        {
            using (Stream publicKeyStream = File.OpenRead(publicKeyFile))
                using (Stream decoderStream = PgpUtilities.GetDecoderStream(publicKeyStream))
                {
                    PgpPublicKeyRingBundle pgpPub = new PgpPublicKeyRingBundle(decoderStream);

                    foreach (PgpPublicKeyRing kRing in pgpPub.GetKeyRings())
                    {
                        foreach (PgpPublicKey k in kRing.GetPublicKeys())
                        {
                            if (k.IsEncryptionKey)
                            {
                                return(k);
                            }
                        }
                    }
                }
            throw new ArgumentException("Can't find encryption key in key ring.");
        }
コード例 #32
0
        public static void Main(string[] args)
        {
            PgpPublicKeyRingBundle pubRings;
            using (Stream fs = File.OpenRead(args[0]))
            {

                pubRings = new PgpPublicKeyRingBundle(PgpUtilities.GetDecoderStream(fs));
            }

            foreach (PgpPublicKeyRing pgpPub in pubRings.GetKeyRings())
            {
                try
                {
                    //PgpPublicKey pubKey =
                    pgpPub.GetPublicKey();
                }
                catch (Exception e)
                {
                    Console.Error.WriteLine(e.Message);
                    Console.Error.WriteLine(e.StackTrace);
                    continue;
                }

                var first = true;
                foreach (PgpPublicKey pgpKey in pgpPub.GetPublicKeys())
                {
                    if (first)
                    {
                        Console.WriteLine("Key ID: " +  pgpKey.KeyId.ToString("X"));
                        first = false;
                    }
                    else
                    {
                        Console.WriteLine("Key ID: " + pgpKey.KeyId.ToString("X") + " (subkey)");
                    }

                    Console.WriteLine("            Algorithm: " + GetAlgorithm(pgpKey.Algorithm));
                    Console.WriteLine("            Fingerprint: " + Hex.ToHexString(pgpKey.GetFingerprint()));
                }
            }
        }
コード例 #33
0
		/**
		 * A simple routine that opens a key ring file and loads the first available key
		 * suitable for encryption.
		 * 
		 * @param input
		 * @return
		 * @throws IOException
		 * @throws PGPException
		 */
		internal static PgpPublicKey ReadPublicKey(Stream input)
		{
			PgpPublicKeyRingBundle pgpPub = new PgpPublicKeyRingBundle(
				PgpUtilities.GetDecoderStream(input));

			//
			// we just loop through the collection till we find a key suitable for encryption, in the real
			// world you would probably want to be a bit smarter about this.
			//

			foreach (PgpPublicKeyRing keyRing in pgpPub.GetKeyRings())
			{
				foreach (PgpPublicKey key in keyRing.GetPublicKeys())
				{
					if (key.IsEncryptionKey)
					{
						return key;
					}
				}
			}

			throw new ArgumentException("Can't find encryption key in key ring.");
		}
コード例 #34
0
        public void PerformTest3()
        {
            PgpPublicKeyRingBundle pubRings = new PgpPublicKeyRingBundle(pub3);

            int count = 0;

            byte[] encRing = pubRings.GetEncoded();

            pubRings = new PgpPublicKeyRingBundle(encRing);

            foreach (PgpPublicKeyRing pgpPub1 in pubRings.GetKeyRings())
            {
                count++;

                int keyCount = 0;

                byte[] bytes = pgpPub1.GetEncoded();

                PgpPublicKeyRing pgpPub2 = new PgpPublicKeyRing(bytes);

                foreach (PgpPublicKey pubK in pgpPub2.GetPublicKeys())
                {
                    keyCount++;
                    pubK.GetSignatures();
                }

                if (keyCount != 2)
                {
                    Fail("wrong number of public keys");
                }
            }

            if (count != 1)
            {
                Fail("wrong number of public keyrings");
            }

            PgpSecretKeyRingBundle secretRings2 = new PgpSecretKeyRingBundle(sec3);

            count = 0;

            encRing = secretRings2.GetEncoded();

            PgpSecretKeyRingBundle secretRings = new PgpSecretKeyRingBundle(encRing);

            foreach (PgpSecretKeyRing pgpSec1 in secretRings2.GetKeyRings())
            {
                count++;

                int keyCount = 0;

                byte[] bytes = pgpSec1.GetEncoded();

                PgpSecretKeyRing pgpSec2 = new PgpSecretKeyRing(bytes);

                foreach (PgpSecretKey k in pgpSec2.GetSecretKeys())
                {
                    keyCount++;
                    k.ExtractPrivateKey(sec3pass1);
                }

                if (keyCount != 2)
                {
                    Fail("wrong number of secret keys");
                }
            }

            if (count != 1)
            {
                Fail("wrong number of secret keyrings");
            }
        }
コード例 #35
0
        public void PerformTest8()
        {
            PgpPublicKeyRingBundle pubRings = new PgpPublicKeyRingBundle(pub8);

            int count = 0;

            byte[] encRing = pubRings.GetEncoded();

            pubRings = new PgpPublicKeyRingBundle(encRing);

            foreach (PgpPublicKeyRing pgpPub1 in pubRings.GetKeyRings())
            {
                count++;

                int keyCount = 0;

                byte[] bytes = pgpPub1.GetEncoded();

                PgpPublicKeyRing pgpPub2 = new PgpPublicKeyRing(bytes);

                foreach (PgpPublicKey o in pgpPub2.GetPublicKeys())
                {
                    if (o == null)
                        Fail("null key found");

                    keyCount++;
                }

                if (keyCount != 2)
                {
                    Fail("wrong number of public keys");
                }
            }

            if (count != 2)
            {
                Fail("wrong number of public keyrings");
            }

            PgpSecretKeyRingBundle secretRings1 = new PgpSecretKeyRingBundle(sec8);

            count = 0;

            encRing = secretRings1.GetEncoded();

            PgpSecretKeyRingBundle secretRings2 = new PgpSecretKeyRingBundle(encRing);

            foreach (PgpSecretKeyRing pgpSec1 in secretRings1.GetKeyRings())
            {
                count++;

                int keyCount = 0;

                byte[] bytes = pgpSec1.GetEncoded();

                PgpSecretKeyRing pgpSec2 = new PgpSecretKeyRing(bytes);

                foreach (PgpSecretKey k in pgpSec2.GetSecretKeys())
                {
                    keyCount++;
                    k.ExtractPrivateKey(sec8pass);
                }

                if (keyCount != 2)
                {
                    Fail("wrong number of secret keys");
                }
            }

            if (count != 1)
            {
                Fail("wrong number of secret keyrings");
            }
        }
コード例 #36
0
        public void PerformTest6()
        {
            PgpPublicKeyRingBundle pubRings = new PgpPublicKeyRingBundle(pub6);

            foreach (PgpPublicKeyRing pgpPub in pubRings.GetKeyRings())
            {
                foreach (PgpPublicKey k in pgpPub.GetPublicKeys())
                {
                    if (k.KeyId == 0x5ce086b5b5a18ff4L)
                    {
                        int count = 0;

                        foreach (PgpSignature sig in k.GetSignaturesOfType(PgpSignature.SubkeyRevocation))
                        {
                            if (sig == null)
                                Fail("null signature found");

                            count++;
                        }

                        if (count != 1)
                        {
                            Fail("wrong number of revocations in test6.");
                        }
                    }
                }
            }

            byte[] encRing = pubRings.GetEncoded();
        }
コード例 #37
0
        public void PerformTest2()
        {
            PgpPublicKeyRingBundle pubRings = new PgpPublicKeyRingBundle(pub2);

            int count = 0;

            byte[] encRing = pubRings.GetEncoded();

            pubRings = new PgpPublicKeyRingBundle(encRing);

            foreach (PgpPublicKeyRing pgpPub1 in pubRings.GetKeyRings())
            {
                count++;

                int keyCount = 0;

                byte[] bytes = pgpPub1.GetEncoded();

                PgpPublicKeyRing pgpPub2 = new PgpPublicKeyRing(bytes);

                foreach (PgpPublicKey pk in pgpPub2.GetPublicKeys())
                {
                    byte[] pkBytes = pk.GetEncoded();

                    PgpPublicKeyRing pkR = new PgpPublicKeyRing(pkBytes);

                    keyCount++;
                }

                if (keyCount != 2)
                {
                    Fail("wrong number of public keys");
                }
            }

            if (count != 2)
            {
                Fail("wrong number of public keyrings");
            }

            PgpSecretKeyRingBundle secretRings2 = new PgpSecretKeyRingBundle(sec2);

            count = 0;

            encRing = secretRings2.GetEncoded();
            PgpSecretKeyRingBundle secretRings = new PgpSecretKeyRingBundle(encRing);

            foreach (PgpSecretKeyRing pgpSec1 in secretRings2.GetKeyRings())
            {
                count++;

                int keyCount = 0;

                byte[] bytes = pgpSec1.GetEncoded();

                PgpSecretKeyRing pgpSec2 = new PgpSecretKeyRing(bytes);

                foreach (PgpSecretKey k in pgpSec2.GetSecretKeys())
                {
                    keyCount++;
                    PgpPublicKey pk = k.PublicKey;

                    if (pk.KeyId == -1413891222336124627L)
                    {
                        int sCount = 0;

                        foreach (PgpSignature pgpSignature in pk.GetSignaturesOfType(PgpSignature.SubkeyBinding))
                        {
                            int type = pgpSignature.SignatureType;
                            if (type != PgpSignature.SubkeyBinding)
                            {
                                Fail("failed to return correct signature type");
                            }
                            sCount++;
                        }

                        if (sCount != 1)
                        {
                            Fail("failed to find binding signature");
                        }
                    }

                    pk.GetSignatures();

                    if (k.KeyId == -4049084404703773049L
                        || k.KeyId == -1413891222336124627L)
                    {
                        k.ExtractPrivateKey(sec2pass1);
                    }
                    else if (k.KeyId == -6498553574938125416L
                        || k.KeyId == 59034765524361024L)
                    {
                        k.ExtractPrivateKey(sec2pass2);
                    }
                }

                if (keyCount != 2)
                {
                    Fail("wrong number of secret keys");
                }
            }

            if (count != 2)
            {
                Fail("wrong number of secret keyrings");
            }
        }
コード例 #38
0
        public void PerformTest1()
        {
            PgpPublicKeyRingBundle pubRings = new PgpPublicKeyRingBundle(pub1);

            int count = 0;

            foreach (PgpPublicKeyRing pgpPub1 in pubRings.GetKeyRings())
            {
                count++;

                int keyCount = 0;
                byte[] bytes = pgpPub1.GetEncoded();

                PgpPublicKeyRing pgpPub2 = new PgpPublicKeyRing(bytes);

                foreach (PgpPublicKey pubKey in pgpPub2.GetPublicKeys())
                {
                    keyCount++;

                    foreach (PgpSignature sig in pubKey.GetSignatures())
                    {
                        if (sig == null)
                            Fail("null signature found");
                    }
                }

                if (keyCount != 2)
                {
                    Fail("wrong number of public keys");
                }
            }

            if (count != 1)
            {
                Fail("wrong number of public keyrings");
            }

            //
            // exact match
            //
            count = 0;
            foreach (PgpPublicKeyRing pgpPub3 in pubRings.GetKeyRings("test (Test key) <*****@*****.**>"))
            {
                if (pgpPub3 == null)
                    Fail("null keyring found");

                count++;
            }

            if (count != 1)
            {
                Fail("wrong number of public keyrings on exact match");
            }

            //
            // partial match 1 expected
            //
            count = 0;
            foreach (PgpPublicKeyRing pgpPub4 in pubRings.GetKeyRings("test", true))
            {
                if (pgpPub4 == null)
                    Fail("null keyring found");

                count++;
            }

            if (count != 1)
            {
                Fail("wrong number of public keyrings on partial match 1");
            }

            //
            // partial match 0 expected
            //
            count = 0;
            foreach (PgpPublicKeyRing pgpPub5 in pubRings.GetKeyRings("XXX", true))
            {
                if (pgpPub5 == null)
                    Fail("null keyring found");

                count++;
            }

            if (count != 0)
            {
                Fail("wrong number of public keyrings on partial match 0");
            }

            //
            // case-insensitive partial match
            //
            count = 0;
            foreach (PgpPublicKeyRing pgpPub6 in pubRings.GetKeyRings("*****@*****.**", true, true))
            {
                if (pgpPub6 == null)
                    Fail("null keyring found");

                count++;
            }

            if (count != 1)
            {
                Fail("wrong number of public keyrings on case-insensitive partial match");
            }

            PgpSecretKeyRingBundle secretRings = new PgpSecretKeyRingBundle(sec1);
            count = 0;

            foreach (PgpSecretKeyRing pgpSec1 in secretRings.GetKeyRings())
            {
                count++;

                int keyCount = 0;

                byte[] bytes = pgpSec1.GetEncoded();

                PgpSecretKeyRing pgpSec2 = new PgpSecretKeyRing(bytes);

                foreach (PgpSecretKey k in pgpSec2.GetSecretKeys())
                {
                    keyCount++;
                    PgpPublicKey pk = k.PublicKey;

                    pk.GetSignatures();

                    byte[] pkBytes = pk.GetEncoded();

                    PgpPublicKeyRing pkR = new PgpPublicKeyRing(pkBytes);
                }

                if (keyCount != 2)
                {
                    Fail("wrong number of secret keys");
                }
            }

            if (count != 1)
            {
                Fail("wrong number of secret keyrings");
            }

            //
            // exact match
            //
            count = 0;
            foreach (PgpSecretKeyRing o1 in secretRings.GetKeyRings("test (Test key) <*****@*****.**>"))
            {
                if (o1 == null)
                    Fail("null keyring found");

                count++;
            }

            if (count != 1)
            {
                Fail("wrong number of secret keyrings on exact match");
            }

            //
            // partial match 1 expected
            //
            count = 0;
            foreach (PgpSecretKeyRing o2 in secretRings.GetKeyRings("test", true))
            {
                if (o2 == null)
                    Fail("null keyring found");

                count++;
            }

            if (count != 1)
            {
                Fail("wrong number of secret keyrings on partial match 1");
            }

            //
            // exact match 0 expected
            //
            count = 0;
            foreach (PgpSecretKeyRing o3 in secretRings.GetKeyRings("test", false))
            {
                if (o3 == null)
                    Fail("null keyring found");

                count++;
            }

            if (count != 0)
            {
                Fail("wrong number of secret keyrings on partial match 0");
            }

            //
            // case-insensitive partial match
            //
            count = 0;
            foreach (PgpSecretKeyRing o4 in secretRings.GetKeyRings("*****@*****.**", true, true))
            {
                if (o4 == null)
                    Fail("null keyring found");

                count++;
            }

            if (count != 1)
            {
                Fail("wrong number of secret keyrings on case-insensitive partial match");
            }
        }