public class AsymmetricKey extends Key implements Serializable, Dumpable
Asymmetric Key Handling.
This class parses and encodes Asymmetric keys from/to ASN.1. It furthermore handles encoding and decoding of encrypted material.
Modifier and Type | Field and Description |
---|---|
protected byte[] |
privateKey |
protected byte[] |
publicKey |
static long |
serialVersionUID |
CRLF
Constructor and Description |
---|
AsymmetricKey()
Creates a new Asymmetric key based on the default values.
|
AsymmetricKey(AlgorithmParameter params)
creates a new asymmetric key based on the parameters given.
|
AsymmetricKey(AlgorithmParameter params,
boolean allowPrecalculated)
creates a new asymmetric key based on the parameters given.
|
AsymmetricKey(AsymmetricKey ak)
Copy Constructor.
|
AsymmetricKey(byte[] b)
Creates an asymmetric key based on the byte sequence.
|
Modifier and Type | Method and Description |
---|---|
byte[] |
decrypt(byte[] b)
Decrypts a byte array using the key contained in this object.
|
String |
dumpValueNotation(String prefix)
Generates the ASN1 notation of the object.
|
String |
dumpValueNotation(String prefix,
DumpType dumpType)
Generates the ASN1 notation of the object.
|
byte[] |
encrypt(byte[] b)
Encrypts a byte array using the key contained in this object.
|
boolean |
equals(Object key)
tests two asymmetric keys for equality.
|
Algorithm |
getAlgorithm()
Gets the algorithm of this key type.
|
AlgorithmParameter |
getAlgorithmParameter()
Gets the full algorithm parameters of this key.
|
int |
getBlockSize()
Gets the size of the key stored in this object.
|
static String |
getCacheFileName() |
static double |
getDequeueProbability()
Gets the current probability for dequeing a used key (nolrmally 1.0)
|
int |
getKeySize()
Gets the size of the key stored in this object.
|
Mode |
getMode()
Gets the mode used for encryption.
|
Padding |
getPadding()
Gets the padding used for encryption.
|
byte[] |
getPrivateKey()
Gets the private key of this object.
|
byte[] |
getPublicKey()
Gets the public key in binary representation.
|
int |
hashCode()
returns the hashcode of the dump representation.
|
boolean |
hasPrivateKey()
Checks if the object contains a private key.
|
protected void |
parse(org.bouncycastle.asn1.ASN1Encodable to) |
static String |
setCacheFileName(String name) |
static double |
setDequeueProbability(double probability)
Sets the probability of reusing a precalculated key again.
|
Mode |
setMode(Mode m)
Sets the mode used for encryption.
|
Padding |
setPadding(Padding p)
Sets the padding used for encryption.
|
byte[] |
setPrivateKey(byte[] b)
Sets the private key of this object.
|
byte[] |
setPublicKey(byte[] b)
Sets the public key.
|
byte[] |
sign(byte[] b)
Signs a byte array.
|
byte[] |
sign(byte[] b,
Algorithm mac)
Signs a byte array.
|
org.bouncycastle.asn1.ASN1Object |
toAsn1Object(DumpType dt)
Dumps the key as ASN1 object.
|
String |
toString()
Gets a textual representation of the objects parameters (without the keys).
|
boolean |
verify(byte[] b,
byte[] sig)
Verifies a given signature accourding to the objects public key.
|
boolean |
verify(byte[] b,
byte[] sig,
Algorithm mac)
Verifies a given signature accourding to the objects public key.
|
fromBase64, fromHex, parse, prepareDump, toBase64, toBitString, toBytes, toDer, toHex
public static final long serialVersionUID
protected byte[] publicKey
protected byte[] privateKey
public AsymmetricKey(byte[] b) throws IOException
Creates an asymmetric key based on the byte sequence.
b
- the byte array containing the key.IOException
- if an error occures during parsingpublic AsymmetricKey(AsymmetricKey ak)
Copy Constructor.
This constuctor allows to create a copy of an AsymmetricKey
ak
- the key to copypublic AsymmetricKey() throws IOException
Creates a new Asymmetric key based on the default values.
IOException
- if an error happens during generationpublic AsymmetricKey(AlgorithmParameter params) throws IOException
creates a new asymmetric key based on the parameters given.
If available a precalculated key will be offered.
params
- the parameters to be usedIOException
- if the key can not be generated with the given parameterspublic AsymmetricKey(AlgorithmParameter params, boolean allowPrecalculated) throws IOException
creates a new asymmetric key based on the parameters given.
This call is mainly used by the cache manager to enforce new calculation of a key.
params
- the parameters to be usedallowPrecalculated
- true if a precalculated key is allowedIOException
- if the key can not be generated with the given parameterspublic static String getCacheFileName()
protected final void parse(org.bouncycastle.asn1.ASN1Encodable to) throws IOException
parse
in class AbstractBlock
IOException
public boolean hasPrivateKey()
Checks if the object contains a private key.
public String dumpValueNotation(String prefix)
Generates the ASN1 notation of the object.
prefix
- the line prefix to be used (normally "")public String dumpValueNotation(String prefix, DumpType dumpType)
Generates the ASN1 notation of the object.
dumpValueNotation
in interface Block
prefix
- the line prefix to be used (normally "")dumpType
- the dump type to be used (normally DumpType.PUBLIC_ONLY)public org.bouncycastle.asn1.ASN1Object toAsn1Object(DumpType dt) throws IOException
Dumps the key as ASN1 object.
toAsn1Object
in interface Dumpable
dt
- the dump type to be usedIOException
- if not encodablepublic byte[] encrypt(byte[] b) throws IOException
Encrypts a byte array using the key contained in this object.
encrypt
in class Key
b
- the plain text byte array to encryptIOException
public byte[] decrypt(byte[] b) throws IOException
Decrypts a byte array using the key contained in this object.
decrypt
in class Key
b
- the encrypted byte arrayIOException
public byte[] sign(byte[] b) throws IOException
Signs a byte array.
This method uses the default hashing algorithm.
b
- the byte array to be signedIOException
- if unable to carry out signaturepublic byte[] sign(byte[] b, Algorithm mac) throws IOException
Signs a byte array.
b
- the byte array to be signedmac
- the hashing algorithm to be usedIOException
- if unable to carry out signaturepublic boolean verify(byte[] b, byte[] sig) throws IOException
Verifies a given signature accourding to the objects public key.
b
- the byte array representing the messagesig
- the byte array representing the signatureIOException
- if signature processing failedpublic boolean verify(byte[] b, byte[] sig, Algorithm mac) throws IOException
Verifies a given signature accourding to the objects public key.
b
- the byte array representing the messagesig
- the byte array representing the signaturemac
- the mac algorithm to verify the signatureIOException
- if signature processing failedpublic static double setDequeueProbability(double probability)
Sets the probability of reusing a precalculated key again.
This capability is used to reduce cpu load during tests.
probability
- the new probability to be setpublic static double getDequeueProbability()
Gets the current probability for dequeing a used key (nolrmally 1.0)
public byte[] setPublicKey(byte[] b)
Sets the public key.
b
- the byte array representing the public keyNullPointerException
- if key was tried to set to nullpublic byte[] getPublicKey()
Gets the public key in binary representation.
public byte[] setPrivateKey(byte[] b)
Sets the private key of this object.
b
- the byte representation of the key to be set.public byte[] getPrivateKey()
Gets the private key of this object.
public Algorithm getAlgorithm()
Gets the algorithm of this key type.
public AlgorithmParameter getAlgorithmParameter()
Gets the full algorithm parameters of this key.
public Padding getPadding()
Gets the padding used for encryption.
public Padding setPadding(Padding p)
Sets the padding used for encryption.
p
- the padding to be setpublic int getKeySize()
Gets the size of the key stored in this object.
public int getBlockSize()
Gets the size of the key stored in this object.
public Mode getMode()
Gets the mode used for encryption.
public Mode setMode(Mode m)
Sets the mode used for encryption.
m
- the mode to be setpublic boolean equals(Object key)
tests two asymmetric keys for equality.
Two keys are considered equal if they contain the same parameters and the same keys (public and private)
public int hashCode()
returns the hashcode of the dump representation.
Copyright © 2023. All rights reserved.