private static void ExportKeyPair( Stream secretOut, Stream publicOut, AsymmetricKeyParameter publicKey, AsymmetricKeyParameter privateKey, string identity, char[] passPhrase, bool armor) { if (armor) { secretOut = new ArmoredOutputStream(secretOut); } PgpSecretKey secretKey = new PgpSecretKey( PgpSignature.DefaultCertification, PublicKeyAlgorithmTag.RsaGeneral, publicKey, privateKey, DateTime.UtcNow, identity, SymmetricKeyAlgorithmTag.Cast5, passPhrase, null, null, new SecureRandom() ); secretKey.Encode(secretOut); if (armor) { secretOut.Close(); publicOut = new ArmoredOutputStream(publicOut); } PgpPublicKey key = secretKey.PublicKey; key.Encode(publicOut); if (armor) { publicOut.Close(); } }
public void Encode(Stream outStr) { //IL_0008: Unknown result type (might be due to invalid IL or missing references) if (outStr == null) { throw new ArgumentNullException("outStr"); } global::System.Collections.IEnumerator enumerator = ((global::System.Collections.IEnumerable)keys).GetEnumerator(); try { while (enumerator.MoveNext()) { PgpSecretKey pgpSecretKey = (PgpSecretKey)enumerator.get_Current(); pgpSecretKey.Encode(outStr); } } finally { global::System.IDisposable disposable = enumerator as global::System.IDisposable; if (disposable != null) { disposable.Dispose(); } } enumerator = ((global::System.Collections.IEnumerable)extraPubKeys).GetEnumerator(); try { while (enumerator.MoveNext()) { PgpPublicKey pgpPublicKey = (PgpPublicKey)enumerator.get_Current(); pgpPublicKey.Encode(outStr); } } finally { global::System.IDisposable disposable2 = enumerator as global::System.IDisposable; if (disposable2 != null) { disposable2.Dispose(); } } }
/// <summary> /// Build a PGP key pair /// </summary> /// <param name="bits">number of bits in key, e.g. 2048</param> /// <param name="identifier">key identifier, e.g. "Your Name <*****@*****.**>" </param> /// <param name="password">key password or null</param> /// <param name="privateKey">returned ascii private key</param> /// <param name="publicKey">returned ascii public key</param> public static void PGPGenerateKey(int bits, string identifier, string password, out string privateKey, out string publicKey) { // generate a new RSA keypair RsaKeyPairGenerator gen = new RsaKeyPairGenerator(); gen.Init(new RsaKeyGenerationParameters(BigInteger.ValueOf(0x101), new Org.BouncyCastle.Security.SecureRandom(), bits, 80)); AsymmetricCipherKeyPair pair = gen.GenerateKeyPair(); // create PGP subpacket PgpSignatureSubpacketGenerator hashedGen = new PgpSignatureSubpacketGenerator(); hashedGen.SetKeyFlags(true, PgpKeyFlags.CanCertify | PgpKeyFlags.CanSign | PgpKeyFlags.CanEncryptCommunications | PgpKeyFlags.CanEncryptStorage); hashedGen.SetPreferredCompressionAlgorithms(false, new int[] { (int)CompressionAlgorithmTag.Zip }); hashedGen.SetPreferredHashAlgorithms(false, new int[] { (int)HashAlgorithmTag.Sha1 }); hashedGen.SetPreferredSymmetricAlgorithms(false, new int[] { (int)SymmetricKeyAlgorithmTag.Cast5 }); PgpSignatureSubpacketVector sv = hashedGen.Generate(); PgpSignatureSubpacketGenerator unhashedGen = new PgpSignatureSubpacketGenerator(); // create the PGP key PgpSecretKey secretKey = new PgpSecretKey( PgpSignature.DefaultCertification, PublicKeyAlgorithmTag.RsaGeneral, pair.Public, pair.Private, DateTime.Now, identifier, SymmetricKeyAlgorithmTag.Cast5, (password != null ? password.ToCharArray() : null), hashedGen.Generate(), unhashedGen.Generate(), new Org.BouncyCastle.Security.SecureRandom()); // extract the keys using (MemoryStream ms = new MemoryStream()) { using (ArmoredOutputStream ars = new ArmoredOutputStream(ms)) { secretKey.Encode(ars); } privateKey = Encoding.ASCII.GetString(ms.ToArray()); } using (MemoryStream ms = new MemoryStream()) { using (ArmoredOutputStream ars = new ArmoredOutputStream(ms)) { secretKey.PublicKey.Encode(ars); } publicKey = Encoding.ASCII.GetString(ms.ToArray()); } }
public void SaveKey(PgpSecretKey key, string publicPath, string secretPath) { using (FileStream pubStream = new FileStream(publicPath, FileMode.Create)) key.PublicKey.Encode (pubStream); using (FileStream secStream = new FileStream(secretPath, FileMode.Create)) key.Encode (secStream); }