Class AsymmetricKey
- All Implemented Interfaces:
Serializable,Block,Dumpable
Asymmetric Key Handling.
This class parses and encodes Asymmetric keys from/to ASN.1. It furthermore handles encoding and decoding of encrypted material.
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected byte[]protected byte[]static final longFields inherited from class net.messagevortex.asn1.AbstractBlock
CRLF -
Constructor Summary
ConstructorsConstructorDescriptionCreates a new Asymmetric key based on the default values.AsymmetricKey(byte[] b) Creates an asymmetric key based on the byte sequence.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.Copy Constructor. -
Method Summary
Modifier and TypeMethodDescriptionbyte[]decrypt(byte[] b) Decrypts a byte array using the key contained in this object.dumpValueNotation(String prefix) Generates the ASN1 notation of the object.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.booleantests two asymmetric keys for equality.Gets the algorithm of this key type.Gets the full algorithm parameters of this key.intGets the size of the key stored in this object.static Stringstatic doubleGets the current probability for dequeing a used key (nolrmally 1.0)intGets the size of the key stored in this object.getMode()Gets the mode used for encryption.Gets the padding used for encryption.byte[]Gets the private key of this object.byte[]Gets the public key in binary representation.inthashCode()returns the hashcode of the dump representation.booleanChecks if the object contains a private key.protected final voidparse(org.bouncycastle.asn1.ASN1Encodable to) static StringsetCacheFileName(String name) static doublesetDequeueProbability(double probability) Sets the probability of reusing a precalculated key again.Sets the mode used for encryption.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[]Signs a byte array.org.bouncycastle.asn1.ASN1ObjecttoAsn1Object(DumpType dt) Dumps the key as ASN1 object.toString()Gets a textual representation of the objects parameters (without the keys).booleanverify(byte[] b, byte[] sig) Verifies a given signature accourding to the objects public key.booleanVerifies a given signature accourding to the objects public key.Methods inherited from class net.messagevortex.asn1.AbstractBlock
fromBase64, fromHex, parse, prepareDump, toBase64, toBitString, toBytes, toDer, toHex
-
Field Details
-
serialVersionUID
public static final long serialVersionUID- See Also:
-
publicKey
protected byte[] publicKey -
privateKey
protected byte[] privateKey
-
-
Constructor Details
-
AsymmetricKey
Creates an asymmetric key based on the byte sequence.
- Parameters:
b- the byte array containing the key.- Throws:
IOException- if an error occures during parsing
-
AsymmetricKey
Copy Constructor.
This constuctor allows to create a copy of an AsymmetricKey
- Parameters:
ak- the key to copy
-
AsymmetricKey
Creates a new Asymmetric key based on the default values.
- Throws:
IOException- if an error happens during generation
-
AsymmetricKey
creates a new asymmetric key based on the parameters given.
If available a precalculated key will be offered.
- Parameters:
params- the parameters to be used- Throws:
IOException- if the key can not be generated with the given parameters
-
AsymmetricKey
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.
- Parameters:
params- the parameters to be usedallowPrecalculated- true if a precalculated key is allowed- Throws:
IOException- if the key can not be generated with the given parameters
-
-
Method Details
-
setCacheFileName
-
getCacheFileName
-
parse
- Specified by:
parsein classAbstractBlock- Throws:
IOException
-
hasPrivateKey
public boolean hasPrivateKey()Checks if the object contains a private key.
- Returns:
- true if the object contains a private key
-
dumpValueNotation
Generates the ASN1 notation of the object.
- Parameters:
prefix- the line prefix to be used (normally "")- Returns:
- the string representation of the ASN1 dump
-
dumpValueNotation
Generates the ASN1 notation of the object.
- Specified by:
dumpValueNotationin interfaceBlock- Parameters:
prefix- the line prefix to be used (normally "")dumpType- the dump type to be used (normally DumpType.PUBLIC_ONLY)- Returns:
- the string representation of the ASN1 dump
-
toAsn1Object
Dumps the key as ASN1 object.
- Specified by:
toAsn1Objectin interfaceDumpable- Parameters:
dt- the dump type to be used- Returns:
- the ASN1 object suitable for encoding
- Throws:
IOException- if not encodable
-
encrypt
Encrypts a byte array using the key contained in this object.
- Specified by:
encryptin classKey- Parameters:
b- the plain text byte array to encrypt- Returns:
- the encrypted byte array including padding
- Throws:
IOException
-
decrypt
Decrypts a byte array using the key contained in this object.
- Specified by:
decryptin classKey- Parameters:
b- the encrypted byte array- Returns:
- the plain text byte array
- Throws:
IOException
-
sign
Signs a byte array.
This method uses the default hashing algorithm.
- Parameters:
b- the byte array to be signed- Returns:
- the signature
- Throws:
IOException- if unable to carry out signature
-
sign
Signs a byte array.
- Parameters:
b- the byte array to be signedmac- the hashing algorithm to be used- Returns:
- the signature
- Throws:
IOException- if unable to carry out signature
-
verify
Verifies a given signature accourding to the objects public key.
- Parameters:
b- the byte array representing the messagesig- the byte array representing the signature- Returns:
- true if signature could be verified successfully
- Throws:
IOException- if signature processing failed
-
verify
Verifies a given signature accourding to the objects public key.
- Parameters:
b- the byte array representing the messagesig- the byte array representing the signaturemac- the mac algorithm to verify the signature- Returns:
- true if signature could be verified successfully
- Throws:
IOException- if signature processing failed
-
setDequeueProbability
public static double setDequeueProbability(double probability) Sets the probability of reusing a precalculated key again.
This capability is used to reduce cpu load during tests.
- Parameters:
probability- the new probability to be set- Returns:
- the previously set probability
-
getDequeueProbability
public static double getDequeueProbability()Gets the current probability for dequeing a used key (nolrmally 1.0)
- Returns:
- the current probability set
-
setPublicKey
public byte[] setPublicKey(byte[] b) Sets the public key.
- Parameters:
b- the byte array representing the public key- Returns:
- the previously set public key
- Throws:
NullPointerException- if key was tried to set to null
-
getPublicKey
public byte[] getPublicKey()Gets the public key in binary representation.
- Returns:
- the public key
-
setPrivateKey
public byte[] setPrivateKey(byte[] b) Sets the private key of this object.
- Parameters:
b- the byte representation of the key to be set.- Returns:
- the previously set private key
-
getPrivateKey
public byte[] getPrivateKey()Gets the private key of this object.
- Returns:
- the pyte representation of the private key
-
getAlgorithm
Gets the algorithm of this key type.
- Returns:
- the algorithm used for generation
-
getAlgorithmParameter
Gets the full algorithm parameters of this key.
- Returns:
- the algorithm parameters used for generation
-
getPadding
Gets the padding used for encryption.
- Returns:
- the padding which is used for encryption
-
setPadding
Sets the padding used for encryption.
- Parameters:
p- the padding to be set- Returns:
- the previously set padding
-
getKeySize
public int getKeySize()Gets the size of the key stored in this object.
- Returns:
- the key size in bits
-
getBlockSize
public int getBlockSize()Gets the size of the key stored in this object.
- Returns:
- the key size in bits
-
getMode
Gets the mode used for encryption.
- Returns:
- the mode set used for encryption
-
setMode
Sets the mode used for encryption.
- Parameters:
m- the mode to be set- Returns:
- the mode previously set
-
equals
tests two asymmetric keys for equality.
Two keys are considered equal if they contain the same parameters and the same keys (public and private)
-
hashCode
public int hashCode()returns the hashcode of the dump representation.
-
toString
Gets a textual representation of the objects parameters (without the keys).
-