Class AsymmetricKey

All Implemented Interfaces:
Serializable, Block, Dumpable

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.

See Also:
  • Field Details

    • serialVersionUID

      public static final long serialVersionUID
      See Also:
    • publicKey

      protected byte[] publicKey
    • privateKey

      protected byte[] privateKey
  • Constructor Details

    • AsymmetricKey

      public AsymmetricKey(byte[] b) throws IOException

      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

      public AsymmetricKey(AsymmetricKey ak)

      Copy Constructor.

      This constuctor allows to create a copy of an AsymmetricKey

      Parameters:
      ak - the key to copy
    • AsymmetricKey

      public AsymmetricKey() throws IOException

      Creates a new Asymmetric key based on the default values.

      Throws:
      IOException - if an error happens during generation
    • AsymmetricKey

      public AsymmetricKey(AlgorithmParameter params) throws IOException

      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

      public 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.

      Parameters:
      params - the parameters to be used
      allowPrecalculated - true if a precalculated key is allowed
      Throws:
      IOException - if the key can not be generated with the given parameters
  • Method Details

    • setCacheFileName

      public static String setCacheFileName(String name)
    • getCacheFileName

      public static String getCacheFileName()
    • parse

      protected final void parse(org.bouncycastle.asn1.ASN1Encodable to) throws IOException
      Specified by:
      parse in class AbstractBlock
      Throws:
      IOException
    • hasPrivateKey

      public boolean hasPrivateKey()

      Checks if the object contains a private key.

      Returns:
      true if the object contains a private key
    • dumpValueNotation

      public String dumpValueNotation(String prefix)

      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

      public String dumpValueNotation(String prefix, DumpType dumpType)

      Generates the ASN1 notation of the object.

      Specified by:
      dumpValueNotation in interface Block
      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

      public org.bouncycastle.asn1.ASN1Object toAsn1Object(DumpType dt) throws IOException

      Dumps the key as ASN1 object.

      Specified by:
      toAsn1Object in interface Dumpable
      Parameters:
      dt - the dump type to be used
      Returns:
      the ASN1 object suitable for encoding
      Throws:
      IOException - if not encodable
    • encrypt

      public byte[] encrypt(byte[] b) throws IOException

      Encrypts a byte array using the key contained in this object.

      Specified by:
      encrypt in class Key
      Parameters:
      b - the plain text byte array to encrypt
      Returns:
      the encrypted byte array including padding
      Throws:
      IOException
    • decrypt

      public byte[] decrypt(byte[] b) throws IOException

      Decrypts a byte array using the key contained in this object.

      Specified by:
      decrypt in class Key
      Parameters:
      b - the encrypted byte array
      Returns:
      the plain text byte array
      Throws:
      IOException
    • sign

      public byte[] sign(byte[] b) throws IOException

      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

      public byte[] sign(byte[] b, Algorithm mac) throws IOException

      Signs a byte array.

      Parameters:
      b - the byte array to be signed
      mac - the hashing algorithm to be used
      Returns:
      the signature
      Throws:
      IOException - if unable to carry out signature
    • verify

      public boolean verify(byte[] b, byte[] sig) throws IOException

      Verifies a given signature accourding to the objects public key.

      Parameters:
      b - the byte array representing the message
      sig - the byte array representing the signature
      Returns:
      true if signature could be verified successfully
      Throws:
      IOException - if signature processing failed
    • verify

      public boolean verify(byte[] b, byte[] sig, Algorithm mac) throws IOException

      Verifies a given signature accourding to the objects public key.

      Parameters:
      b - the byte array representing the message
      sig - the byte array representing the signature
      mac - 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

      public Algorithm getAlgorithm()

      Gets the algorithm of this key type.

      Returns:
      the algorithm used for generation
    • getAlgorithmParameter

      public AlgorithmParameter getAlgorithmParameter()

      Gets the full algorithm parameters of this key.

      Returns:
      the algorithm parameters used for generation
    • getPadding

      public Padding getPadding()

      Gets the padding used for encryption.

      Returns:
      the padding which is used for encryption
    • setPadding

      public Padding setPadding(Padding p)

      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

      public Mode getMode()

      Gets the mode used for encryption.

      Returns:
      the mode set used for encryption
    • setMode

      public Mode setMode(Mode m)

      Sets the mode used for encryption.

      Parameters:
      m - the mode to be set
      Returns:
      the mode previously set
    • equals

      public 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)

      Overrides:
      equals in class Object
      Parameters:
      key - the other key
      Returns:
      true if both keys are considered equivalent
    • hashCode

      public int hashCode()

      returns the hashcode of the dump representation.

      Overrides:
      hashCode in class Object
    • toString

      public String toString()

      Gets a textual representation of the objects parameters (without the keys).

      Overrides:
      toString in class Object
      Returns:
      the string