/// <summary>Create a key pair from a PgpPrivateKey and a PgpPublicKey.</summary> /// <param name="pub">The public key.</param> /// <param name="priv">The private key.</param> public PgpKeyPair( PgpPublicKey pub, PgpPrivateKey priv) { this.pub = pub; this.priv = priv; }
/** * Construct a PgpSecretKey using the passed in private key and public key. This constructor will not add any * certifications but assumes that pubKey already has what is required. * * @param privKey the private key component. * @param pubKey the public key component. * @param checksumCalculator a calculator for the private key checksum * @param isMasterKey true if the key is a master key, false otherwise. * @param keyEncryptor an encryptor for the key if required (null otherwise). * @throws PgpException if there is an issue creating the secret key packet. */ public PgpSecretKey( PgpPrivateKey privKey, PgpPublicKey pubKey, bool isMasterKey, IPbeSecretKeyEncryptor keyEncryptor) { this.pub = pubKey; this.secret = buildSecretKeyPacket(isMasterKey, privKey, pubKey, keyEncryptor); }
private static SecretKeyPacket buildSecretKeyPacket(bool isMasterKey, PgpPrivateKey privKey, PgpPublicKey pubKey, IPbeSecretKeyEncryptor keyEncryptor) { BcpgObject secKey = (BcpgObject)privKey.Key; if (secKey == null) { if (isMasterKey) { return(new SecretKeyPacket(pubKey.publicPk, SymmetricKeyAlgorithmTag.Null, null, null, new byte[0])); } else { return(new SecretSubkeyPacket(pubKey.publicPk, SymmetricKeyAlgorithmTag.Null, null, null, new byte[0])); } } try { MemoryOutputStream bOut = new MemoryOutputStream(); BcpgOutputStream pOut = new BcpgOutputStream(bOut); pOut.WriteObject(secKey); byte[] keyData = bOut.ToArray(); byte[] checkData = checksum(keyEncryptor.ChecksumCalculatorFactory, keyData, keyData.Length); pOut.Write(checkData, 0, checkData.Length); PgpPbeKeyEncryptionParameters encParams = keyEncryptor.AlgorithmDetails; SymmetricKeyAlgorithmTag encAlgorithm = (keyEncryptor != null) ? encParams.Algorithm : SymmetricKeyAlgorithmTag.Null; if (encAlgorithm != SymmetricKeyAlgorithmTag.Null) { keyData = bOut.ToArray(); // include checksum byte[] encData = keyEncryptor.Wrap(keyData).Collect(); byte[] iv = encParams.GetIV(); S2k s2k = encParams.S2k; int s2kUsage; if (keyEncryptor.ChecksumCalculatorFactory != null) { if (keyEncryptor.ChecksumCalculatorFactory.AlgorithmDetails.Algorithm != HashAlgorithmTag.Sha1) { throw new PgpException("only SHA1 supported for key checksum calculations."); } s2kUsage = SecretKeyPacket.UsageSha1; } else { s2kUsage = SecretKeyPacket.UsageChecksum; } if (isMasterKey) { return(new SecretKeyPacket(pubKey.publicPk, encAlgorithm, s2kUsage, s2k, iv, encData)); } else { return(new SecretSubkeyPacket(pubKey.publicPk, encAlgorithm, s2kUsage, s2k, iv, encData)); } } else { if (isMasterKey) { return(new SecretKeyPacket(pubKey.publicPk, encAlgorithm, null, null, bOut.ToArray())); } else { return(new SecretSubkeyPacket(pubKey.publicPk, encAlgorithm, null, null, bOut.ToArray())); } } } catch (PgpException e) { throw e; } catch (Exception e) { throw new PgpException("Exception encrypting key", e); } }
PgpSecretKey( PgpPrivateKey privKey, PgpPublicKey pubKey, IPbeSecretKeyEncryptor keyEncryptor) : this(privKey, pubKey, false, keyEncryptor) { }