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; } return null; }
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; }
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())); } } }
public static PgpPublicKey ReadPublicKey(Stream inputStream) { inputStream = PgpUtilities.GetDecoderStream(inputStream); PgpPublicKeyRingBundle pgpPub = new PgpPublicKeyRingBundle(inputStream); 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."); }
static void Main(string[] args) { //password = args[0].ToCharArray(); //var context = new CryptoContext(PasswordCallback, "AES-128", "SHA-1"); //var crypto = new PgpCrypto(context); try { using (var inputStream = File.OpenRead(@"C:\projects\OutlookPrivacyPlugin\Deja.Crypto.Test\private\andrew-pubring.gpg")) using (var decodeStream = PgpUtilities.GetDecoderStream(inputStream)) { var pgpPub = new PgpPublicKeyRingBundle(decodeStream); foreach (PgpPublicKeyRing kRing in pgpPub.GetKeyRings()) { // The master key is normally the first key returned. var masterKey = kRing.GetPublicKey(); if (!masterKey.IsMasterKey) { foreach (PgpPublicKey k in kRing.GetPublicKeys()) if (k.IsMasterKey) Console.WriteLine("{0:X}", k.KeyId); } foreach (PgpPublicKey k in kRing.GetPublicKeys()) { Console.WriteLine("{0:X}", k.KeyId); } } } } catch (Exception) { throw; } }
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(); } } }
public PgpPublicKey[] GetPublicKeyUserIdsForEncryption() { logger.Debug("GetPublicKeyUserIdsForEncryption"); using (var inputStream = File.OpenRead(Context.PublicKeyRingFile)) using (var decoderStream = PgpUtilities.GetDecoderStream(inputStream)) { var pgpPub = new PgpPublicKeyRingBundle(decoderStream); var keyUserIds = new List<PgpPublicKey>(); foreach (PgpPublicKeyRing kRing in pgpPub.GetKeyRings()) { foreach (PgpPublicKey k in kRing.GetPublicKeys()) { // Break on key with specific user id. For testing only. //var idIter = GetMasterPublicKey(k.KeyId).GetUserIds().GetEnumerator(); //idIter.MoveNext(); //if(((string)idIter.Current).StartsWith("And")) //{ // System.Diagnostics.Debugger.Break(); //} if (!IsKeyValid(k)) continue; if (!IsEncryptionKey(k)) continue; var masterKey = k; if (!k.IsMasterKey) masterKey = GetMasterPublicKey(k.KeyId); if (!keyUserIds.Any(kk => kk.KeyId == masterKey.KeyId)) keyUserIds.Add(masterKey); } } return keyUserIds.ToArray(); } }
/// <summary> /// A simple routine that opens a key ring file and loads the first available key suitable for encryption. /// </summary> /// <param name="inputStream">The input stream.</param> /// <returns></returns> /// <exception cref="System.ArgumentException">Can't find encryption key in key ring.</exception> 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."); }
/// <summary> /// Encrypt string using a PGP public key /// </summary> /// <param name="plain">plain text to encrypt</param> /// <param name="armoredPublicKey">public key in ASCII "-----BEGIN PGP PUBLIC KEY BLOCK----- .. -----END PGP PUBLIC KEY BLOCK-----" format</param> /// <returns>PGP message string</returns> public static string PGPEncrypt(string plain, string armoredPublicKey) { // encode data byte[] data = Encoding.UTF8.GetBytes(plain); // create the WinAuth public key PgpPublicKey publicKey = null; using (MemoryStream ms = new MemoryStream(Encoding.ASCII.GetBytes(armoredPublicKey))) { using (Stream dis = PgpUtilities.GetDecoderStream(ms)) { PgpPublicKeyRingBundle bundle = new PgpPublicKeyRingBundle(dis); foreach (PgpPublicKeyRing keyring in bundle.GetKeyRings()) { foreach (PgpPublicKey key in keyring.GetPublicKeys()) { if (key.IsEncryptionKey == true && key.IsRevoked() == false) { publicKey = key; break; } } } } } // encrypt the data using PGP using (MemoryStream encryptedStream = new MemoryStream()) { using (ArmoredOutputStream armored = new ArmoredOutputStream(encryptedStream)) { PgpEncryptedDataGenerator pedg = new PgpEncryptedDataGenerator(Org.BouncyCastle.Bcpg.SymmetricKeyAlgorithmTag.Cast5, true, new Org.BouncyCastle.Security.SecureRandom()); pedg.AddMethod(publicKey); using (Stream pedgStream = pedg.Open(armored, new byte[4096])) { PgpCompressedDataGenerator pcdg = new PgpCompressedDataGenerator(Org.BouncyCastle.Bcpg.CompressionAlgorithmTag.Zip); using (Stream pcdgStream = pcdg.Open(pedgStream)) { PgpLiteralDataGenerator pldg = new PgpLiteralDataGenerator(); using (Stream encrypter = pldg.Open(pcdgStream, PgpLiteralData.Binary, "", (long)data.Length, DateTime.Now)) { encrypter.Write(data, 0, data.Length); } } } } return Encoding.ASCII.GetString(encryptedStream.ToArray()); } }
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; }
/// <summary> /// Imports public pgp keys from the specified stream. /// </summary> /// <remarks> /// Imports public pgp keys from the specified stream. /// </remarks> /// <param name="stream">The raw key data.</param> /// <exception cref="System.ArgumentNullException"> /// <paramref name="stream"/> is <c>null</c>. /// </exception> /// <exception cref="System.IO.IOException"> /// <para>An error occurred while parsing the raw key-ring data</para> /// <para>-or-</para> /// <para>An error occured while saving the public key-ring bundle.</para> /// </exception> public override void Import (Stream stream) { if (stream == null) throw new ArgumentNullException ("stream"); using (var armored = new ArmoredInputStream (stream)) { var imported = new PgpPublicKeyRingBundle (armored); if (imported.Count == 0) return; int publicKeysAdded = 0; foreach (PgpPublicKeyRing pubring in imported.GetKeyRings ()) { if (!PublicKeyRingBundle.Contains (pubring.GetPublicKey ().KeyId)) { PublicKeyRingBundle = PgpPublicKeyRingBundle.AddPublicKeyRing (PublicKeyRingBundle, pubring); publicKeysAdded++; } } if (publicKeysAdded > 0) SavePublicKeyRingBundle (); } }
/// <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 void Import (PgpPublicKeyRingBundle bundle) { if (bundle == null) throw new ArgumentNullException (nameof (bundle)); int publicKeysAdded = 0; foreach (PgpPublicKeyRing pubring in bundle.GetKeyRings ()) { if (!PublicKeyRingBundle.Contains (pubring.GetPublicKey ().KeyId)) { PublicKeyRingBundle = PgpPublicKeyRingBundle.AddPublicKeyRing (PublicKeyRingBundle, pubring); publicKeysAdded++; } } if (publicKeysAdded > 0) SavePublicKeyRingBundle (); }
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(); } } }
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; } } }
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; }
public PgpPublicKey GetPublicKey(long KeyId) { using (var inputStream = File.OpenRead(Context.PublicKeyRingFile)) { using (var decodeStream = PgpUtilities.GetDecoderStream(inputStream)) { var pgpPub = new PgpPublicKeyRingBundle(decodeStream); foreach (PgpPublicKeyRing kRing in pgpPub.GetKeyRings()) { foreach (PgpPublicKey k in kRing.GetPublicKeys()) { if (k.KeyId == KeyId) return k; } } return null; } } }
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"); } }
public PgpPublicKeyRing GetPublicKeyRing(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() + ">"; // Each KeyRing is a single key set (master + subs) foreach (PgpPublicKeyRing kRing in pgpPub.GetKeyRings()) { var masterKey = kRing.GetPublicKey(); if (!masterKey.IsMasterKey) masterKey = GetMasterPublicKey(masterKey.KeyId); if (!IsKeyValid(masterKey)) continue; // Skip key's that don't match if (!masterKey.GetUserIds().Cast<string>().Any(id => id.ToLower().Contains(emailSearch))) continue; return kRing; } return null; } }
public PgpPublicKey GetMasterPublicKey(long keyId) { logger.Debug("GetMasterPublicKey: {0:X}", keyId); using (var inputStream = File.OpenRead(Context.PublicKeyRingFile)) using (var decodeStream = PgpUtilities.GetDecoderStream(inputStream)) { var pgpPub = new PgpPublicKeyRingBundle(decodeStream); foreach (PgpPublicKeyRing kRing in pgpPub.GetKeyRings()) { // The master key is normally the first key returned. var masterKey = kRing.GetPublicKey(); if(!masterKey.IsMasterKey) { foreach (PgpPublicKey k in kRing.GetPublicKeys()) if (k.IsMasterKey) masterKey = k; } foreach (PgpPublicKey k in kRing.GetPublicKeys()) { if (k.KeyId == keyId) return masterKey; } } return null; } }
/// <summary> /// /// </summary> /// <remarks> /// PgpPublicKeyRing is a key set. The first key is the master key, rest are sub-keys. /// </remarks> /// <param name="email"></param> /// <returns></returns> public PgpPublicKey GetPublicKeyForEncryption(string email) { logger.Debug("GetPublicKeyForEncryption: {0}", email); using (var inputStream = File.OpenRead(Context.PublicKeyRingFile)) using (var decoderStream = PgpUtilities.GetDecoderStream(inputStream)) { var pgpPub = new PgpPublicKeyRingBundle(decoderStream); var emailSearch = "<" + email.ToLower().Trim() + ">"; // Each KeyRing is a single key set (master + subs) foreach (PgpPublicKeyRing kRing in pgpPub.GetKeyRings()) { var masterKey = kRing.GetPublicKey(); if (!masterKey.IsMasterKey) masterKey = GetMasterPublicKey(masterKey.KeyId); // Skip key's that don't match if (!masterKey.GetUserIds().Cast<string>().Any(id => id.ToLower().Contains(emailSearch))) { logger.Debug("Skipping key ring: {0}", masterKey.KeyId.ToString("X")); continue; } logger.Debug("Found correct master key, searching for encryption key..."); PgpPublicKey encryptionKey = null; foreach (PgpPublicKey k in kRing.GetPublicKeys()) { if (!IsKeyValid(k) || !IsEncryptionKey(k)) continue; // Prefer sub-keys to master keys if (!k.IsMasterKey) return k; // Use master if no other options available. encryptionKey = k; } if (encryptionKey != null) return encryptionKey; } return null; } }