static void GenerateKey(string pubring, string secring, string nameReal, string nameEmail, DateTime expireDate, string passphrase, bool use4096) { if (string.IsNullOrEmpty(pubring)) { Console.WriteLine("ERROR: 'pubring' is not a valid file path."); ReadKeyOrReturnAfterLapse(); return; } try { var fi = new FileInfo(pubring); if (fi.Exists) { Console.WriteLine("ERROR: 'pubring' file already exists."); ReadKeyOrReturnAfterLapse(); return; } if (!fi.Directory.Exists) { fi.Directory.Create(); } } catch { } if (string.IsNullOrEmpty(secring)) { Console.WriteLine("ERROR: 'secring' is not a valid file path."); ReadKeyOrReturnAfterLapse(); return; } try { var fi = new FileInfo(secring); if (fi.Exists) { Console.WriteLine("ERROR: 'secring' file already exists."); ReadKeyOrReturnAfterLapse(); return; } if (!fi.Directory.Exists) { fi.Directory.Create(); } } catch { } if (string.IsNullOrEmpty(nameReal)) { Console.WriteLine("ERROR: 'nameReal' is not valid."); ReadKeyOrReturnAfterLapse(); return; } if (string.IsNullOrEmpty(nameEmail) || !nameEmail.IsValidEmailAddress()) { Console.WriteLine("ERROR: 'nameEmail' is not valid."); ReadKeyOrReturnAfterLapse(); return; } if (!string.IsNullOrWhiteSpace(passphrase)) { if (passphrase.Length < 7) { Console.WriteLine("ERROR: 'passphrase' must be at least 7 characters long."); ReadKeyOrReturnAfterLapse(); return; } if (!passphrase.Any(c => char.IsDigit(c))) { Console.WriteLine("ERROR: 'passphrase' must contain at least 1 number."); ReadKeyOrReturnAfterLapse(); return; } } //string pubring, string secring, string nameReal, string nameEmail, DateTime expireDate, string passphrase, bool use4096 Console.WriteLine(); Console.WriteLine("Key parameters:"); Console.WriteLine(DIVIDER); Console.WriteLine("pubring: " + pubring); Console.WriteLine("secring: " + secring); Console.WriteLine("name-real: " + nameReal); Console.WriteLine("name-email: " + nameEmail); Console.WriteLine("expire-date: " + expireDate.ToString()); if (use4096) { Console.WriteLine("key-length: 4096"); } else { Console.WriteLine("key-length: 2048"); } if (string.IsNullOrWhiteSpace(passphrase)) { Console.WriteLine("WARNING * * * * * * * *"); Console.WriteLine("The private key does not have a passphrase."); Console.WriteLine("It is strongly recommended that you use a passphrase with your private key."); Console.WriteLine("* * * * * * * * * * * *"); } Console.WriteLine(DIVIDER); // check to execute if (!IsYforContinue()) { Console.WriteLine(); Console.WriteLine("Canceling key generation."); Console.WriteLine(); return; } Console.WriteLine(); Console.WriteLine(); Console.WriteLine(DIVIDER); Console.WriteLine("Please wait while we generate your keys. Generating secure keys takes time..."); Console.WriteLine(DIVIDER); Console.WriteLine(); try { PgpKeyRingGenerator krgen = PgpKeyRingHelper.CreateKeyRingGenerator(nameReal + " <" + nameEmail + ">", expireDate, passphrase, use4096); // Generate public key ring, dump to file. PgpPublicKeyRing pkr = krgen.GeneratePublicKeyRing(); bool armorPublic = true; if (armorPublic) { using (ArmoredOutputStream pubout = new ArmoredOutputStream(new FileStream(pubring, System.IO.FileMode.Create))) { pkr.Encode(pubout); pubout.Close(); } } else { using (BufferedStream pubout = new BufferedStream(new FileStream(pubring, System.IO.FileMode.Create))) { pkr.Encode(pubout); pubout.Close(); } } bool armorPrivate = false; // Generate private key, dump to file. PgpSecretKeyRing skr = krgen.GenerateSecretKeyRing(); if (armorPrivate) { using (ArmoredOutputStream secout = new ArmoredOutputStream(new FileStream(secring, System.IO.FileMode.Create))) { skr.Encode(secout); secout.Close(); } } else { using (BufferedStream secout = new BufferedStream(new FileStream(secring, System.IO.FileMode.Create))) { skr.Encode(secout); secout.Close(); } } Console.WriteLine(); Console.WriteLine("PGP key pairs successfully generated."); Console.WriteLine(); ReadKeyOrReturnAfterLapse(); } catch (Exception ex) { Console.WriteLine(); Console.WriteLine("Exception: " + ex.GetType().Name + " - " + ex.Message); Console.WriteLine(); ReadKeyOrReturnAfterLapse(); } }
public static void Main(string[] args) { if (args.Length == 1) { Stream fis = File.OpenRead(args[0]); var ring = new PgpPublicKeyRing( PgpUtilities.GetDecoderStream(fis)); var key = ring.GetPublicKey(); // iterate through all direct key signautures and look for NotationData subpackets foreach (PgpSignature sig in key.GetSignaturesOfType(PgpSignature.DirectKey)) { Console.WriteLine("Signature date is: " + sig.GetHashedSubPackets().GetSignatureCreationTime()); var data = sig.GetHashedSubPackets().GetNotationDataOccurences(); for (int i = 0; i < data.Length; i++) { Console.WriteLine("Found Notaion named '" + data[i].GetNotationName() +"' with content '" + data[i].GetNotationValue() + "'."); } } fis.Close(); } else if (args.Length == 5) { Stream secFis = File.OpenRead(args[0]); Stream pubFis = File.OpenRead(args[2]); // gather command line arguments PgpSecretKeyRing secRing = new PgpSecretKeyRing( PgpUtilities.GetDecoderStream(secFis)); String secretKeyPass = args[1]; PgpPublicKeyRing ring = new PgpPublicKeyRing( PgpUtilities.GetDecoderStream(pubFis)); String notationName = args[3]; String notationValue = args[4]; // create the signed keyRing PgpPublicKeyRing sRing = null; sRing = new PgpPublicKeyRing( new MemoryStream( SignPublicKey(secRing.GetSecretKey(), secretKeyPass, ring.GetPublicKey(), notationName, notationValue, true), false)); ring = sRing; secFis.Close(); pubFis.Close(); Stream fos = File.Create("SignedKey.asc"); // write the created keyRing to file ArmoredOutputStream aOut = new ArmoredOutputStream(fos); sRing.Encode(aOut); aOut.Close(); // Note: ArmoredOutputStream.Close() leaves underlying stream open fos.Close(); } else { Console.Error.WriteLine("usage: DirectKeySignature secretKeyFile secretKeyPass publicKeyFile(key to be signed) NotationName NotationValue"); Console.Error.WriteLine("or: DirectKeySignature signedPublicKeyFile"); } }