コード例 #1
0
 public void WriteObject(BcpgObject bcpgObject)
 {
     bcpgObject.Encode(this);
 }
コード例 #2
0
        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);
            }
        }