Top Description Fields Constructors Methods
org.bouncycastle.openpgp

public Class PGPSecretKey

extends Object
Class Inheritance
Imports
java.io.ByteArrayInputStream, .ByteArrayOutputStream, .IOException, .InputStream, .OutputStream, java.util.ArrayList, .Iterator, .List, org.bouncycastle.bcpg.BCPGInputStream, .BCPGObject, .BCPGOutputStream, .DSASecretBCPGKey, .ECSecretBCPGKey, .EdSecretBCPGKey, .ElGamalSecretBCPGKey, .HashAlgorithmTags, .PublicKeyAlgorithmTags, .PublicKeyPacket, .PublicSubkeyPacket, .RSASecretBCPGKey, .S2K, .SecretKeyPacket, .SecretSubkeyPacket, .SignatureSubpacketTags, .SymmetricKeyAlgorithmTags, .TrustPacket, .UserAttributePacket, .UserIDPacket, org.bouncycastle.gpg.SExprParser, org.bouncycastle.openpgp.operator.KeyFingerPrintCalculator, .PBEProtectionRemoverFactory, .PBESecretKeyDecryptor, .PBESecretKeyEncryptor, .PGPContentSignerBuilder, .PGPDigestCalculator, org.bouncycastle.util.Arrays

general class to handle and construct a PGP secret key object.

Field Summary

Modifier and TypeField and Description
pack-priv PGPPublicKey
pack-priv SecretKeyPacket

Constructor Summary

AccessConstructor and Description
public
pack-priv
PGPSecretKey(PGPPrivateKey privKey, PGPPublicKey pubKey, PGPDigestCalculator checksumCalculator, PBESecretKeyEncryptor keyEncryptor)

public
PGPSecretKey(PGPPrivateKey
the private key component.
privKey
,
PGPPublicKey
the public key component.
pubKey
,
PGPDigestCalculator
a calculator for the private key checksum
checksumCalculator
,
boolean
true if the key is a master key, false otherwise.
isMasterKey
,
PBESecretKeyEncryptor
an encryptor for the key if required (null otherwise).
keyEncryptor
)

Construct a PGPSecretKey using the passed in private key and public key.

public
PGPSecretKey(int
the type of certification to be added.
certificationLevel
,
PGPKeyPair
the public/private keys to use.
keyPair
,
String
the id to bind to the key.
id
,
PGPSignatureSubpacketVector
the hashed packets to be added to the certification.
hashedPcks
,
PGPSignatureSubpacketVector
the unhashed packets to be added to the certification.
unhashedPcks
,
PGPContentSignerBuilder
the builder for generating the certification.
certificationSignerBuilder
,
PBESecretKeyEncryptor
an encryptor for the key if required (null otherwise).
keyEncryptor
)

Construct a PGPSecretKey using the passed in private/public key pair and binding it to the passed in id using a generated certification of certificationLevel.The secret key checksum is calculated using the original non-digest based checksum.

public
PGPSecretKey(PGPKeyPair
the master public/private keys for the new subkey.
masterKeyPair
,
PGPKeyPair
the public/private keys to use.
keyPair
,
PGPDigestCalculator
a calculator for the private key checksum
checksumCalculator
,
PGPContentSignerBuilder
the builder for generating the certification.
certificationSignerBuilder
,
PBESecretKeyEncryptor
an encryptor for the key if required (null otherwise).
keyEncryptor
)

Construct a PGPSecretKey sub-key using the passed in private/public key pair and binding it to the master key pair.

public
PGPSecretKey(PGPKeyPair
the master public/private keys for the new subkey.
masterKeyPair
,
PGPKeyPair
the public/private keys to use.
keyPair
,
PGPDigestCalculator
calculator for PGP key checksums.
checksumCalculator
,
PGPSignatureSubpacketVector
the hashed packets to be added to the certification.
hashedPcks
,
PGPSignatureSubpacketVector
the unhashed packets to be added to the certification.
unhashedPcks
,
PGPContentSignerBuilder
the builder for generating the certification.
certificationSignerBuilder
,
PBESecretKeyEncryptor
an encryptor for the key if required (null otherwise).
keyEncryptor
)

Construct a PGPSecretKey sub-key using the passed in private/public key pair and binding it to the master key pair.

public
PGPSecretKey(int
the type of certification to be added.
certificationLevel
,
PGPKeyPair
the public/private keys to use.
keyPair
,
String
the id to bind to the key.
id
,
PGPDigestCalculator
a calculator for the private key checksum.
checksumCalculator
,
PGPSignatureSubpacketVector
the hashed packets to be added to the certification.
hashedPcks
,
PGPSignatureSubpacketVector
the unhashed packets to be added to the certification.
unhashedPcks
,
PGPContentSignerBuilder
the builder for generating the certification.
certificationSignerBuilder
,
PBESecretKeyEncryptor
an encryptor for the key if required (null otherwise).
keyEncryptor
)

Construct a PGPSecretKey using the passed in private/public key pair and binding it to the passed in id using a generated certification of certificationLevel.

Method Summary

Modifier and TypeMethod and Description
private static PGPPublicKey
buildPublicKey(boolean isMasterKey, PGPPublicKey pubKey)

private static SecretKeyPacket
buildSecretKeyPacket(boolean isMasterKey, PGPPrivateKey privKey, PGPPublicKey pubKey, PBESecretKeyEncryptor keyEncryptor, PGPDigestCalculator checksumCalculator)

private static PGPPublicKey
certifiedPublicKey(int certificationLevel, PGPKeyPair keyPair, String id, PGPSignatureSubpacketVector hashedPcks, PGPSignatureSubpacketVector unhashedPcks, PGPContentSignerBuilder certificationSignerBuilder)

private static byte[]
checksum(PGPDigestCalculator digCalc, byte[] bytes, int length)

pack-priv static boolean
constantTimeAreEqual(int len, byte[] a, int aOff, byte[] b, int bOff)

public static PGPSecretKey
copyWithNewPassword(PGPSecretKey
the PGPSecretKey to be copied.
key
,
PBESecretKeyDecryptor
the current decryptor based on the current password for key.
oldKeyDecryptor
,
PBESecretKeyEncryptor
a new encryptor based on a new password for encrypting the secret key material.
newKeyEncryptor
)

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

public static PGPSecretKey
copyWithNewPassword(PGPSecretKey
the PGPSecretKey to be copied.
key
,
PBESecretKeyDecryptor
the current decryptor based on the current password for key.
oldKeyDecryptor
,
PBESecretKeyEncryptor
a new encryptor based on a new password for encrypting the secret key material.
newKeyEncryptor
,
PGPDigestCalculator
digest based checksum calculator for private key data.
checksumCalculator
)

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

public void
encode(OutputStream outStream)

private byte[]
public PGPKeyPair

Returns:

PGPPrivateKey the unencrypted private key.
extractKeyPair
(PBESecretKeyDecryptor
factory to use to generate a decryptor for the passed in secretKey.
decryptorFactory
)

Extract a PGPPrivate key from the SecretKey's encrypted contents.

public PGPPrivateKey

Returns:

PGPPrivateKey the unencrypted private key.
extractPrivateKey
(PBESecretKeyDecryptor
factory to use to generate a decryptor for the passed in secretKey.
decryptorFactory
)

Extract a PGPPrivate key from the SecretKey's encrypted contents.

public byte[]
public int

Returns:

the algorithm used to encrypt the secret key.
getKeyEncryptionAlgorithm
()

return the algorithm the key is encrypted with.

public long

Returns:

the keyID associated with this key.
getKeyID
()

Return the keyID of the public key associated with this key.

public PGPPublicKey

Returns:

the public key for this key.
getPublicKey
()

Return the public key associated with this key.

public S2K

Returns:

the key's S2K, null if one is not present.
getS2K
()

Return the S2K used to process this key

public int

Returns:

the key's S2K usage
getS2KUsage
()

Return the S2K usage associated with this key.

public Iterator<PGPUserAttributeSubpacketVector>

Returns:

an iterator of PGPUserAttributeSubpacketVector.
getUserAttributes
()

Return any user attribute vectors associated with the key.

public Iterator<String>

Returns:

an iterator of Strings.
getUserIDs
()

Return any userIDs associated with the key.

public boolean

Returns:

true if a master key.
isMasterKey
()

Return true if this is a master key.

public boolean

Returns:

boolean whether or not the private key is empty
isPrivateKeyEmpty
()

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

public boolean

Returns:

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

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

public static PGPSecretKey

Returns:

a secret key object.
parseSecretKeyFromSExpr
(InputStream inputStream, PBEProtectionRemoverFactory keyProtectionRemoverFactory, PGPPublicKey pubKey)

Deprecated use org.bouncycastle.gpg.SExprParser - it will also allow you to verify the protection checksum if it is available.
Parse a secret key from one of the GPG S expression keys associating it with the passed in public key.
public static PGPSecretKey

Returns:

a secret key object.
parseSecretKeyFromSExpr
(InputStream inputStream, PBEProtectionRemoverFactory keyProtectionRemoverFactory, KeyFingerPrintCalculator fingerPrintCalculator)

Deprecated use org.bouncycastle.gpg.SExprParser - it will also allow you to verify the protection checksum if it is available.
Parse a secret key from one of the GPG S expression keys.
public static PGPSecretKey

Returns:

a new secret key.
replacePublicKey
(PGPSecretKey
secret key to change
secretKey
,
PGPPublicKey
new public key.
publicKey
)

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

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

pubback to summary
pack-priv PGPPublicKey pub
secretback to summary
pack-priv SecretKeyPacket secret

Constructor Detail

PGPSecretKeyback to summary
public PGPSecretKey(SecretKeyPacket secret, PGPPublicKey pub)
PGPSecretKeyback to summary
pack-priv PGPSecretKey(PGPPrivateKey privKey, PGPPublicKey pubKey, PGPDigestCalculator checksumCalculator, PBESecretKeyEncryptor keyEncryptor) throws PGPException
PGPSecretKeyback to summary
public PGPSecretKey(PGPPrivateKey privKey, PGPPublicKey pubKey, PGPDigestCalculator checksumCalculator, boolean isMasterKey, PBESecretKeyEncryptor keyEncryptor) throws PGPException

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.

Parameters
privKey:PGPPrivateKey

the private key component.

pubKey:PGPPublicKey

the public key component.

checksumCalculator:PGPDigestCalculator

a calculator for the private key checksum

isMasterKey:boolean

true if the key is a master key, false otherwise.

keyEncryptor:PBESecretKeyEncryptor

an encryptor for the key if required (null otherwise).

Exceptions
PGPException:
if there is an issue creating the secret key packet.
PGPSecretKeyback to summary
public PGPSecretKey(int certificationLevel, PGPKeyPair keyPair, String id, PGPSignatureSubpacketVector hashedPcks, PGPSignatureSubpacketVector unhashedPcks, PGPContentSignerBuilder certificationSignerBuilder, PBESecretKeyEncryptor keyEncryptor) throws PGPException

Construct a PGPSecretKey using the passed in private/public key pair and binding it to the passed in id using a generated certification of certificationLevel.The secret key checksum is calculated using the original non-digest based checksum.

Parameters
certificationLevel:int

the type of certification to be added.

keyPair:PGPKeyPair

the public/private keys to use.

id:String

the id to bind to the key.

hashedPcks:PGPSignatureSubpacketVector

the hashed packets to be added to the certification.

unhashedPcks:PGPSignatureSubpacketVector

the unhashed packets to be added to the certification.

certificationSignerBuilder:PGPContentSignerBuilder

the builder for generating the certification.

keyEncryptor:PBESecretKeyEncryptor

an encryptor for the key if required (null otherwise).

Exceptions
PGPException:
if there is an issue creating the secret key packet or the certification.
PGPSecretKeyback to summary
public PGPSecretKey(PGPKeyPair masterKeyPair, PGPKeyPair keyPair, PGPDigestCalculator checksumCalculator, PGPContentSignerBuilder certificationSignerBuilder, PBESecretKeyEncryptor keyEncryptor) throws PGPException

Construct a PGPSecretKey sub-key using the passed in private/public key pair and binding it to the master key pair. The secret key checksum is calculated using the passed in checksum calculator.

Parameters
masterKeyPair:PGPKeyPair

the master public/private keys for the new subkey.

keyPair:PGPKeyPair

the public/private keys to use.

checksumCalculator:PGPDigestCalculator

a calculator for the private key checksum

certificationSignerBuilder:PGPContentSignerBuilder

the builder for generating the certification.

keyEncryptor:PBESecretKeyEncryptor

an encryptor for the key if required (null otherwise).

Exceptions
PGPException:
if there is an issue creating the secret key packet or the certification.
PGPSecretKeyback to summary
public PGPSecretKey(PGPKeyPair masterKeyPair, PGPKeyPair keyPair, PGPDigestCalculator checksumCalculator, PGPSignatureSubpacketVector hashedPcks, PGPSignatureSubpacketVector unhashedPcks, PGPContentSignerBuilder certificationSignerBuilder, PBESecretKeyEncryptor keyEncryptor) throws PGPException

Construct a PGPSecretKey sub-key using the passed in private/public key pair and binding it to the master key pair. The secret key checksum is calculated using the passed in checksum calculator.

Parameters
masterKeyPair:PGPKeyPair

the master public/private keys for the new subkey.

keyPair:PGPKeyPair

the public/private keys to use.

checksumCalculator:PGPDigestCalculator

calculator for PGP key checksums.

hashedPcks:PGPSignatureSubpacketVector

the hashed packets to be added to the certification.

unhashedPcks:PGPSignatureSubpacketVector

the unhashed packets to be added to the certification.

certificationSignerBuilder:PGPContentSignerBuilder

the builder for generating the certification.

keyEncryptor:PBESecretKeyEncryptor

an encryptor for the key if required (null otherwise).

Exceptions
PGPException:
if there is an issue creating the secret key packet or the certification.
PGPSecretKeyback to summary
public PGPSecretKey(int certificationLevel, PGPKeyPair keyPair, String id, PGPDigestCalculator checksumCalculator, PGPSignatureSubpacketVector hashedPcks, PGPSignatureSubpacketVector unhashedPcks, PGPContentSignerBuilder certificationSignerBuilder, PBESecretKeyEncryptor keyEncryptor) throws PGPException

Construct a PGPSecretKey using the passed in private/public key pair and binding it to the passed in id using a generated certification of certificationLevel.

Parameters
certificationLevel:int

the type of certification to be added.

keyPair:PGPKeyPair

the public/private keys to use.

id:String

the id to bind to the key.

checksumCalculator:PGPDigestCalculator

a calculator for the private key checksum.

hashedPcks:PGPSignatureSubpacketVector

the hashed packets to be added to the certification.

unhashedPcks:PGPSignatureSubpacketVector

the unhashed packets to be added to the certification.

certificationSignerBuilder:PGPContentSignerBuilder

the builder for generating the certification.

keyEncryptor:PBESecretKeyEncryptor

an encryptor for the key if required (null otherwise).

Exceptions
PGPException:
if there is an issue creating the secret key packet or the certification.

Method Detail

buildPublicKeyback to summary
private static PGPPublicKey buildPublicKey(boolean isMasterKey, PGPPublicKey pubKey)
buildSecretKeyPacketback to summary
private static SecretKeyPacket buildSecretKeyPacket(boolean isMasterKey, PGPPrivateKey privKey, PGPPublicKey pubKey, PBESecretKeyEncryptor keyEncryptor, PGPDigestCalculator checksumCalculator) throws PGPException
certifiedPublicKeyback to summary
private static PGPPublicKey certifiedPublicKey(int certificationLevel, PGPKeyPair keyPair, String id, PGPSignatureSubpacketVector hashedPcks, PGPSignatureSubpacketVector unhashedPcks, PGPContentSignerBuilder certificationSignerBuilder) throws PGPException
checksumback to summary
private static byte[] checksum(PGPDigestCalculator digCalc, byte[] bytes, int length) throws PGPException
constantTimeAreEqualback to summary
pack-priv static boolean constantTimeAreEqual(int len, byte[] a, int aOff, byte[] b, int bOff)
copyWithNewPasswordback to summary
public static PGPSecretKey copyWithNewPassword(PGPSecretKey key, PBESecretKeyDecryptor oldKeyDecryptor, PBESecretKeyEncryptor newKeyEncryptor) throws PGPException

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

Parameters
key:PGPSecretKey

the PGPSecretKey to be copied.

oldKeyDecryptor:PBESecretKeyDecryptor

the current decryptor based on the current password for key.

newKeyEncryptor:PBESecretKeyEncryptor

a new encryptor based on a new password for encrypting the secret key material.

copyWithNewPasswordback to summary
public static PGPSecretKey copyWithNewPassword(PGPSecretKey key, PBESecretKeyDecryptor oldKeyDecryptor, PBESecretKeyEncryptor newKeyEncryptor, PGPDigestCalculator checksumCalculator) throws PGPException

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

Parameters
key:PGPSecretKey

the PGPSecretKey to be copied.

oldKeyDecryptor:PBESecretKeyDecryptor

the current decryptor based on the current password for key.

newKeyEncryptor:PBESecretKeyEncryptor

a new encryptor based on a new password for encrypting the secret key material.

checksumCalculator:PGPDigestCalculator

digest based checksum calculator for private key data.

encodeback to summary
public void encode(OutputStream outStream) throws IOException
extractKeyDataback to summary
private byte[] extractKeyData(PBESecretKeyDecryptor decryptorFactory) throws PGPException
extractKeyPairback to summary
public PGPKeyPair extractKeyPair(PBESecretKeyDecryptor decryptorFactory) throws PGPException

Extract a PGPPrivate key from the SecretKey's encrypted contents.

Parameters
decryptorFactory:PBESecretKeyDecryptor

factory to use to generate a decryptor for the passed in secretKey.

Returns:PGPKeyPair

PGPPrivateKey the unencrypted private key.

Exceptions
PGPException:
on failure.
extractPrivateKeyback to summary
public PGPPrivateKey extractPrivateKey(PBESecretKeyDecryptor decryptorFactory) throws PGPException

Extract a PGPPrivate key from the SecretKey's encrypted contents.

Parameters
decryptorFactory:PBESecretKeyDecryptor

factory to use to generate a decryptor for the passed in secretKey.

Returns:PGPPrivateKey

PGPPrivateKey the unencrypted private key.

Exceptions
PGPException:
on failure.
getEncodedback to summary
public byte[] getEncoded() throws IOException
getKeyEncryptionAlgorithmback to summary
public int getKeyEncryptionAlgorithm()

return the algorithm the key is encrypted with.

Returns:int

the algorithm used to encrypt the secret key.

getKeyIDback to summary
public long getKeyID()

Return the keyID of the public key associated with this key.

Returns:long

the keyID associated with this key.

getPublicKeyback to summary
public PGPPublicKey getPublicKey()

Return the public key associated with this key.

Returns:PGPPublicKey

the public key for this key.

getS2Kback to summary
public S2K getS2K()

Return the S2K used to process this key

Returns:S2K

the key's S2K, null if one is not present.

getS2KUsageback to summary
public int getS2KUsage()

Return the S2K usage associated with this key.

Returns:int

the key's S2K usage

getUserAttributesback to summary
public Iterator<PGPUserAttributeSubpacketVector> getUserAttributes()

Return any user attribute vectors associated with the key.

Returns:Iterator<PGPUserAttributeSubpacketVector>

an iterator of PGPUserAttributeSubpacketVector.

getUserIDsback to summary
public Iterator<String> getUserIDs()

Return any userIDs associated with the key.

Returns:Iterator<String>

an iterator of Strings.

isMasterKeyback to summary
public boolean isMasterKey()

Return true if this is a master key.

Returns:boolean

true if a master key.

isPrivateKeyEmptyback to summary
public boolean isPrivateKeyEmpty()

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

Returns:boolean

boolean whether or not the private key is empty

isSigningKeyback to summary
public boolean isSigningKey()

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

Note

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

Returns:boolean

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

parseSecretKeyFromSExprback to summary
public static PGPSecretKey parseSecretKeyFromSExpr(InputStream inputStream, PBEProtectionRemoverFactory keyProtectionRemoverFactory, PGPPublicKey pubKey) throws IOException, PGPException

Deprecated

use org.bouncycastle.gpg.SExprParser - it will also allow you to verify the protection checksum if it is available.

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

Returns:PGPSecretKey

a secret key object.

parseSecretKeyFromSExprback to summary
public static PGPSecretKey parseSecretKeyFromSExpr(InputStream inputStream, PBEProtectionRemoverFactory keyProtectionRemoverFactory, KeyFingerPrintCalculator fingerPrintCalculator) throws IOException, PGPException

Deprecated

use org.bouncycastle.gpg.SExprParser - it will also allow you to verify the protection checksum if it is available.

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

Returns:PGPSecretKey

a secret key object.

replacePublicKeyback to summary
public static PGPSecretKey replacePublicKey(PGPSecretKey secretKey, PGPPublicKey publicKey)

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

Parameters
secretKey:PGPSecretKey

secret key to change

publicKey:PGPPublicKey

new public key.

Returns:PGPSecretKey

a new secret key.

Exceptions
IllegalArgumentException:
if keyIDs do not match.