Table of Contents

Class PgpSecretKey

Namespace
Org.BouncyCastle.Bcpg.OpenPgp
Assembly
BouncyCastle.Cryptography.dll
public class PgpSecretKey : PgpObject
Inheritance
PgpSecretKey
Inherited Members

Remarks

General class to handle a PGP secret key object.

Constructors

PgpSecretKey(int, PgpKeyPair, string, SymmetricKeyAlgorithmTag, HashAlgorithmTag, bool, char[], bool, PgpSignatureSubpacketVector, PgpSignatureSubpacketVector, SecureRandom)

public PgpSecretKey(int certificationLevel, PgpKeyPair keyPair, string id, SymmetricKeyAlgorithmTag encAlgorithm, HashAlgorithmTag hashAlgorithm, bool utf8PassPhrase, char[] passPhrase, bool useSha1, PgpSignatureSubpacketVector hashedPackets, PgpSignatureSubpacketVector unhashedPackets, SecureRandom rand)

Parameters

certificationLevel int
keyPair PgpKeyPair
id string
encAlgorithm SymmetricKeyAlgorithmTag
hashAlgorithm HashAlgorithmTag
utf8PassPhrase bool
passPhrase char[]
useSha1 bool
hashedPackets PgpSignatureSubpacketVector
unhashedPackets PgpSignatureSubpacketVector
rand SecureRandom

Remarks

If utf8PassPhrase is true, conversion of the passphrase to bytes uses Encoding.UTF8.GetBytes(), otherwise the conversion is performed using Convert.ToByte(), which is the historical behaviour of the library (1.7 and earlier).

PgpSecretKey(int, PgpKeyPair, string, SymmetricKeyAlgorithmTag, HashAlgorithmTag, byte[], bool, PgpSignatureSubpacketVector, PgpSignatureSubpacketVector, SecureRandom)

public PgpSecretKey(int certificationLevel, PgpKeyPair keyPair, string id, SymmetricKeyAlgorithmTag encAlgorithm, HashAlgorithmTag hashAlgorithm, byte[] rawPassPhrase, bool useSha1, PgpSignatureSubpacketVector hashedPackets, PgpSignatureSubpacketVector unhashedPackets, SecureRandom rand)

Parameters

certificationLevel int
keyPair PgpKeyPair
id string
encAlgorithm SymmetricKeyAlgorithmTag
hashAlgorithm HashAlgorithmTag
rawPassPhrase byte[]
useSha1 bool
hashedPackets PgpSignatureSubpacketVector
unhashedPackets PgpSignatureSubpacketVector
rand SecureRandom

Remarks

Allows the caller to handle the encoding of the passphrase to bytes.

PgpSecretKey(int, PgpKeyPair, string, SymmetricKeyAlgorithmTag, HashAlgorithmTag, char[], bool, PgpSignatureSubpacketVector, PgpSignatureSubpacketVector, SecureRandom)

public PgpSecretKey(int certificationLevel, PgpKeyPair keyPair, string id, SymmetricKeyAlgorithmTag encAlgorithm, HashAlgorithmTag hashAlgorithm, char[] passPhrase, bool useSha1, PgpSignatureSubpacketVector hashedPackets, PgpSignatureSubpacketVector unhashedPackets, SecureRandom rand)

Parameters

certificationLevel int
keyPair PgpKeyPair
id string
encAlgorithm SymmetricKeyAlgorithmTag
hashAlgorithm HashAlgorithmTag
passPhrase char[]
useSha1 bool
hashedPackets PgpSignatureSubpacketVector
unhashedPackets PgpSignatureSubpacketVector
rand SecureRandom

Remarks

Conversion of the passphrase characters to bytes is performed using Convert.ToByte(), which is the historical behaviour of the library (1.7 and earlier).

PgpSecretKey(int, PgpKeyPair, string, SymmetricKeyAlgorithmTag, bool, char[], bool, PgpSignatureSubpacketVector, PgpSignatureSubpacketVector, SecureRandom)

public PgpSecretKey(int certificationLevel, PgpKeyPair keyPair, string id, SymmetricKeyAlgorithmTag encAlgorithm, bool utf8PassPhrase, char[] passPhrase, bool useSha1, PgpSignatureSubpacketVector hashedPackets, PgpSignatureSubpacketVector unhashedPackets, SecureRandom rand)

Parameters

certificationLevel int
keyPair PgpKeyPair
id string
encAlgorithm SymmetricKeyAlgorithmTag
utf8PassPhrase bool
passPhrase char[]
useSha1 bool
hashedPackets PgpSignatureSubpacketVector
unhashedPackets PgpSignatureSubpacketVector
rand SecureRandom

Remarks

If utf8PassPhrase is true, conversion of the passphrase to bytes uses Encoding.UTF8.GetBytes(), otherwise the conversion is performed using Convert.ToByte(), which is the historical behaviour of the library (1.7 and earlier).

PgpSecretKey(int, PgpKeyPair, string, SymmetricKeyAlgorithmTag, byte[], bool, PgpSignatureSubpacketVector, PgpSignatureSubpacketVector, SecureRandom)

public PgpSecretKey(int certificationLevel, PgpKeyPair keyPair, string id, SymmetricKeyAlgorithmTag encAlgorithm, byte[] rawPassPhrase, bool useSha1, PgpSignatureSubpacketVector hashedPackets, PgpSignatureSubpacketVector unhashedPackets, SecureRandom rand)

Parameters

certificationLevel int
keyPair PgpKeyPair
id string
encAlgorithm SymmetricKeyAlgorithmTag
rawPassPhrase byte[]
useSha1 bool
hashedPackets PgpSignatureSubpacketVector
unhashedPackets PgpSignatureSubpacketVector
rand SecureRandom

Remarks

Allows the caller to handle the encoding of the passphrase to bytes.

PgpSecretKey(int, PgpKeyPair, string, SymmetricKeyAlgorithmTag, char[], bool, PgpSignatureSubpacketVector, PgpSignatureSubpacketVector, SecureRandom)

public PgpSecretKey(int certificationLevel, PgpKeyPair keyPair, string id, SymmetricKeyAlgorithmTag encAlgorithm, char[] passPhrase, bool useSha1, PgpSignatureSubpacketVector hashedPackets, PgpSignatureSubpacketVector unhashedPackets, SecureRandom rand)

Parameters

certificationLevel int
keyPair PgpKeyPair
id string
encAlgorithm SymmetricKeyAlgorithmTag
passPhrase char[]
useSha1 bool
hashedPackets PgpSignatureSubpacketVector
unhashedPackets PgpSignatureSubpacketVector
rand SecureRandom

Remarks

Conversion of the passphrase characters to bytes is performed using Convert.ToByte(), which is the historical behaviour of the library (1.7 and earlier).

PgpSecretKey(int, PublicKeyAlgorithmTag, AsymmetricKeyParameter, AsymmetricKeyParameter, DateTime, string, SymmetricKeyAlgorithmTag, char[], PgpSignatureSubpacketVector, PgpSignatureSubpacketVector, SecureRandom)

public PgpSecretKey(int certificationLevel, PublicKeyAlgorithmTag algorithm, AsymmetricKeyParameter pubKey, AsymmetricKeyParameter privKey, DateTime time, string id, SymmetricKeyAlgorithmTag encAlgorithm, char[] passPhrase, PgpSignatureSubpacketVector hashedPackets, PgpSignatureSubpacketVector unhashedPackets, SecureRandom rand)

Parameters

certificationLevel int
algorithm PublicKeyAlgorithmTag
pubKey AsymmetricKeyParameter
privKey AsymmetricKeyParameter
time DateTime
id string
encAlgorithm SymmetricKeyAlgorithmTag
passPhrase char[]
hashedPackets PgpSignatureSubpacketVector
unhashedPackets PgpSignatureSubpacketVector
rand SecureRandom

PgpSecretKey(int, PublicKeyAlgorithmTag, AsymmetricKeyParameter, AsymmetricKeyParameter, DateTime, string, SymmetricKeyAlgorithmTag, char[], bool, PgpSignatureSubpacketVector, PgpSignatureSubpacketVector, SecureRandom)

public PgpSecretKey(int certificationLevel, PublicKeyAlgorithmTag algorithm, AsymmetricKeyParameter pubKey, AsymmetricKeyParameter privKey, DateTime time, string id, SymmetricKeyAlgorithmTag encAlgorithm, char[] passPhrase, bool useSha1, PgpSignatureSubpacketVector hashedPackets, PgpSignatureSubpacketVector unhashedPackets, SecureRandom rand)

Parameters

certificationLevel int
algorithm PublicKeyAlgorithmTag
pubKey AsymmetricKeyParameter
privKey AsymmetricKeyParameter
time DateTime
id string
encAlgorithm SymmetricKeyAlgorithmTag
passPhrase char[]
useSha1 bool
hashedPackets PgpSignatureSubpacketVector
unhashedPackets PgpSignatureSubpacketVector
rand SecureRandom

Properties

IsMasterKey

True, if this is a master key.

public bool IsMasterKey { get; }

Property Value

bool

IsPrivateKeyEmpty

Detect if the Secret Key's Private Key is empty or not

public bool IsPrivateKeyEmpty { get; }

Property Value

bool

IsSigningKey

Check if this key has an algorithm type that makes it suitable to use for signing.

public bool IsSigningKey { get; }

Property Value

bool

true if this key algorithm is suitable for use with signing.

Remarks

Note: with version 4 keys KeyFlags subpackets should also be considered when present for determining the preferred use of the key.

KeyEncryptionAlgorithm

The algorithm the key is encrypted with.

public SymmetricKeyAlgorithmTag KeyEncryptionAlgorithm { get; }

Property Value

SymmetricKeyAlgorithmTag

KeyId

The key ID of the public key associated with this key.

public long KeyId { get; }

Property Value

long

PublicKey

The public key associated with this key.

public PgpPublicKey PublicKey { get; }

Property Value

PgpPublicKey

S2k

Return the S2K used to process this key.

public S2k S2k { get; }

Property Value

S2k

S2kUsage

Return the S2K usage associated with this key.

public int S2kUsage { get; }

Property Value

int

UserAttributes

Allows enumeration of any user attribute vectors associated with the key.

public IEnumerable<PgpUserAttributeSubpacketVector> UserAttributes { get; }

Property Value

IEnumerable<PgpUserAttributeSubpacketVector>

An IEnumerable of string objects.

UserIds

Allows enumeration of any user IDs associated with the key.

public IEnumerable<string> UserIds { get; }

Property Value

IEnumerable<string>

An IEnumerable of string objects.

Methods

CopyWithNewPassword(PgpSecretKey, char[], char[], SymmetricKeyAlgorithmTag, SecureRandom)

Return a copy of the passed in secret key, encrypted using a new password and the passed in algorithm.

public static PgpSecretKey CopyWithNewPassword(PgpSecretKey key, char[] oldPassPhrase, char[] newPassPhrase, SymmetricKeyAlgorithmTag newEncAlgorithm, SecureRandom rand)

Parameters

key PgpSecretKey

The PgpSecretKey to be copied.

oldPassPhrase char[]

The current password for the key.

newPassPhrase char[]

The new password for the key.

newEncAlgorithm SymmetricKeyAlgorithmTag

The algorithm to be used for the encryption.

rand SecureRandom

Source of randomness.

Returns

PgpSecretKey

Remarks

Conversion of the passphrase characters to bytes is performed using Convert.ToByte(), which is the historical behaviour of the library (1.7 and earlier).

CopyWithNewPasswordRaw(PgpSecretKey, byte[], byte[], SymmetricKeyAlgorithmTag, SecureRandom)

Return a copy of the passed in secret key, encrypted using a new password and the passed in algorithm.

public static PgpSecretKey CopyWithNewPasswordRaw(PgpSecretKey key, byte[] rawOldPassPhrase, byte[] rawNewPassPhrase, SymmetricKeyAlgorithmTag newEncAlgorithm, SecureRandom rand)

Parameters

key PgpSecretKey

The PgpSecretKey to be copied.

rawOldPassPhrase byte[]

The current password for the key.

rawNewPassPhrase byte[]

The new password for the key.

newEncAlgorithm SymmetricKeyAlgorithmTag

The algorithm to be used for the encryption.

rand SecureRandom

Source of randomness.

Returns

PgpSecretKey

Remarks

Allows the caller to handle the encoding of the passphrase to bytes.

CopyWithNewPasswordUtf8(PgpSecretKey, char[], char[], SymmetricKeyAlgorithmTag, SecureRandom)

Return a copy of the passed in secret key, encrypted using a new password and the passed in algorithm.

public static PgpSecretKey CopyWithNewPasswordUtf8(PgpSecretKey key, char[] oldPassPhrase, char[] newPassPhrase, SymmetricKeyAlgorithmTag newEncAlgorithm, SecureRandom rand)

Parameters

key PgpSecretKey

The PgpSecretKey to be copied.

oldPassPhrase char[]

The current password for the key.

newPassPhrase char[]

The new password for the key.

newEncAlgorithm SymmetricKeyAlgorithmTag

The algorithm to be used for the encryption.

rand SecureRandom

Source of randomness.

Returns

PgpSecretKey

Remarks

The passphrase is encoded to bytes using UTF8 (Encoding.UTF8.GetBytes).

Encode(Stream)

public void Encode(Stream outStr)

Parameters

outStr Stream

ExtractPrivateKey(char[])

Extract a PgpPrivateKey from this secret key's encrypted contents.

public PgpPrivateKey ExtractPrivateKey(char[] passPhrase)

Parameters

passPhrase char[]

Returns

PgpPrivateKey

Remarks

Conversion of the passphrase characters to bytes is performed using Convert.ToByte(), which is the historical behaviour of the library (1.7 and earlier).

ExtractPrivateKeyRaw(byte[])

Extract a PgpPrivateKey from this secret key's encrypted contents.

public PgpPrivateKey ExtractPrivateKeyRaw(byte[] rawPassPhrase)

Parameters

rawPassPhrase byte[]

Returns

PgpPrivateKey

Remarks

Allows the caller to handle the encoding of the passphrase to bytes.

ExtractPrivateKeyUtf8(char[])

Extract a PgpPrivateKey from this secret key's encrypted contents.

public PgpPrivateKey ExtractPrivateKeyUtf8(char[] passPhrase)

Parameters

passPhrase char[]

Returns

PgpPrivateKey

Remarks

The passphrase is encoded to bytes using UTF8 (Encoding.UTF8.GetBytes).

GetEncoded()

public byte[] GetEncoded()

Returns

byte[]

GetFingerprint()

The fingerprint of the public key associated with this key.

public byte[] GetFingerprint()

Returns

byte[]

ParseSecretKeyFromSExpr(Stream, char[])

Parse a secret key from one of the GPG S expression keys.

public static PgpSecretKey ParseSecretKeyFromSExpr(Stream inputStream, char[] passPhrase)

Parameters

inputStream Stream
passPhrase char[]

Returns

PgpSecretKey

Remarks

Conversion of the passphrase characters to bytes is performed using Convert.ToByte(), which is the historical behaviour of the library (1.7 and earlier).

ParseSecretKeyFromSExpr(Stream, char[], PgpPublicKey)

Parse a secret key from one of the GPG S expression keys associating it with the passed in public key.

public static PgpSecretKey ParseSecretKeyFromSExpr(Stream inputStream, char[] passPhrase, PgpPublicKey pubKey)

Parameters

inputStream Stream
passPhrase char[]
pubKey PgpPublicKey

Returns

PgpSecretKey

Remarks

Conversion of the passphrase characters to bytes is performed using Convert.ToByte(), which is the historical behaviour of the library (1.7 and earlier).

ParseSecretKeyFromSExprRaw(Stream, byte[])

Parse a secret key from one of the GPG S expression keys.

public static PgpSecretKey ParseSecretKeyFromSExprRaw(Stream inputStream, byte[] rawPassPhrase)

Parameters

inputStream Stream
rawPassPhrase byte[]

Returns

PgpSecretKey

Remarks

Allows the caller to handle the encoding of the passphrase to bytes.

ParseSecretKeyFromSExprRaw(Stream, byte[], PgpPublicKey)

Parse a secret key from one of the GPG S expression keys associating it with the passed in public key.

public static PgpSecretKey ParseSecretKeyFromSExprRaw(Stream inputStream, byte[] rawPassPhrase, PgpPublicKey pubKey)

Parameters

inputStream Stream
rawPassPhrase byte[]
pubKey PgpPublicKey

Returns

PgpSecretKey

Remarks

Allows the caller to handle the encoding of the passphrase to bytes.

ParseSecretKeyFromSExprUtf8(Stream, char[])

Parse a secret key from one of the GPG S expression keys.

public static PgpSecretKey ParseSecretKeyFromSExprUtf8(Stream inputStream, char[] passPhrase)

Parameters

inputStream Stream
passPhrase char[]

Returns

PgpSecretKey

Remarks

The passphrase is encoded to bytes using UTF8 (Encoding.UTF8.GetBytes).

ParseSecretKeyFromSExprUtf8(Stream, char[], PgpPublicKey)

Parse a secret key from one of the GPG S expression keys associating it with the passed in public key.

public static PgpSecretKey ParseSecretKeyFromSExprUtf8(Stream inputStream, char[] passPhrase, PgpPublicKey pubKey)

Parameters

inputStream Stream
passPhrase char[]
pubKey PgpPublicKey

Returns

PgpSecretKey

Remarks

The passphrase is encoded to bytes using UTF8 (Encoding.UTF8.GetBytes).

ReplacePublicKey(PgpSecretKey, PgpPublicKey)

Replace the passed the public key on the passed in secret key.

public static PgpSecretKey ReplacePublicKey(PgpSecretKey secretKey, PgpPublicKey publicKey)

Parameters

secretKey PgpSecretKey

Secret key to change.

publicKey PgpPublicKey

New public key.

Returns

PgpSecretKey

A new secret key.

Exceptions

ArgumentException

If KeyId's do not match.