<PackageReference Include="BouncyCastle.Cryptography" Version="2.7.0-beta.98" />

PublicKeyEncSessionPacket

Basic packet for a PGP public key.
using Org.BouncyCastle.Utilities; using Org.BouncyCastle.Utilities.IO; using System.IO; namespace Org.BouncyCastle.Bcpg { public class PublicKeyEncSessionPacket : ContainedPacket { private readonly int m_version; private readonly ulong m_keyID; private readonly PublicKeyAlgorithmTag m_algorithm; private readonly byte[][] m_data; public int Version => m_version; public long KeyId => (long)m_keyID; public PublicKeyAlgorithmTag Algorithm => m_algorithm; internal PublicKeyEncSessionPacket(BcpgInputStream bcpgIn) { m_version = bcpgIn.RequireByte(); m_keyID = StreamUtilities.RequireUInt64BE(bcpgIn); m_algorithm = (PublicKeyAlgorithmTag)bcpgIn.RequireByte(); PublicKeyAlgorithmTag algorithm = m_algorithm; if ((uint)(algorithm - 1) > 1) { switch (algorithm) { case PublicKeyAlgorithmTag.ElGamalEncrypt: case PublicKeyAlgorithmTag.ElGamalGeneral: { MPInteger mPInteger = new MPInteger(bcpgIn); MPInteger mPInteger2 = new MPInteger(bcpgIn); m_data = new byte[2][] { mPInteger.GetEncoded(), mPInteger2.GetEncoded() }; break; } case PublicKeyAlgorithmTag.ECDH: m_data = new byte[1][] { Streams.ReadAll(bcpgIn) }; break; default: throw new IOException("unknown PGP public key algorithm encountered"); } } else m_data = new byte[1][] { new MPInteger(bcpgIn).GetEncoded() }; } public PublicKeyEncSessionPacket(long keyId, PublicKeyAlgorithmTag algorithm, byte[][] data) { m_version = 3; m_keyID = (ulong)keyId; m_algorithm = algorithm; m_data = new byte[data.Length][]; for (int i = 0; i < data.Length; i++) { m_data[i] = Arrays.Clone(data[i]); } } public byte[][] GetEncSessionKey() { return m_data; } public override void Encode(BcpgOutputStream bcpgOut) { MemoryStream memoryStream = new MemoryStream(); memoryStream.WriteByte((byte)m_version); StreamUtilities.WriteUInt64BE(memoryStream, m_keyID); memoryStream.WriteByte((byte)m_algorithm); byte[][] data = m_data; foreach (byte[] array in data) { memoryStream.Write(array, 0, array.Length); } bcpgOut.WritePacket(PacketTag.PublicKeyEncryptedSession, memoryStream.ToArray()); } } }