Class CryptoEngine

java.lang.Object
pygar.cryptography.CryptoEngine
Direct Known Subclasses:
CryptoDefault

public abstract class CryptoEngine
extends java.lang.Object
Objects that implement the abstract class CryptoEngine supply methods for the standard cryptography algorithms. We bundle the algorithms them with an object so that the algorithms can be applied where needed through an exemplar object instantiated with the configuration. This indirect method allows each application to select an appropriate version of the algorithms. Attention: the class may contain an order sensitivity. The abstract functions include functions that set the password for the KeyStore for public key encryption and one of these functions must be called before calling the functions that perform encryption. A preferred implementation of this abstract class would force the code to provide the password as part of the constructor; then, the KeyStore will always be ready when needed.
  • Field Summary

    Fields 
    Modifier and Type Field Description
    java.lang.String crypt_algorithm  
    protected java.lang.String entityAlias  
    boolean fullyConfigured  
    protected char[] privateEntryPass  
    protected java.lang.String privateEntryPassword  
    protected java.security.KeyStore privateKeyStore  
    protected java.security.KeyStore publicKeyStore  
    protected java.security.KeyStore sessionKeyStore  
  • Constructor Summary

    Constructors 
    Constructor Description
    CryptoEngine()  
  • Method Summary

    Modifier and Type Method Description
    static java.lang.String byteArrayToHex​(byte[] ba)
    A function to convert a byte array to a string with hexadecimal conversion
    abstract void crypt​(java.io.InputStream inStream, java.io.OutputStream outStream, javax.crypto.Cipher cipher)
    Perform a symmetric key encryption or decryption on stream.
    abstract void decryptStream​(java.io.DataInputStream in, java.io.OutputStream out)
    Decrypt the text on the input stream using the current entities private key and the public key encryption system.
    abstract void encryptStream​(java.lang.String name, java.io.InputStream in, java.io.DataOutputStream out)
    Encrypt clear text from an input stream using Public Key Encryption applying the public key of the named destination and placing the encrypted text on the output stream.
    abstract java.security.PrivateKey getPrivateKey()
    Return the private key of the current entity from the KeyStore
    abstract java.security.PublicKey getPublicKey​(java.lang.String entityName)
    Return the public key of an entity from the KeyStore
    static byte[] hexStringToByteArray​(java.lang.String s)
    A function to convert a string containing a byte array written as hexadecimal into a byte array.
    abstract javax.crypto.SecretKey randomKey()
    Generate a random symmetric encryption key.
    abstract void setPassword​(char[] password)
    Supply the password for the keystore used with the CryptoEngine.
    abstract void setPassword​(java.lang.String password)
    Supply the password for the keystore used with the CryptoEngine.
    abstract java.lang.String signText​(java.lang.String text)
    Sign a text string by computing its encrypted value under this entities private key.
    abstract javax.crypto.SecretKey unwrapSecretKey​(byte[] wrappedKey)
    UnWrap a secret key contained in a byte array using the private key for this entity and return the SecretKeySpec
    abstract boolean verifySignedText​(java.lang.String name, java.lang.String text, java.lang.String signature)
    Check a signature by decrypting it with the public key of the declared name and comparing the decrypted text with the original text.
    abstract byte[] wrapSecretKey​(java.security.Key spec, java.lang.String name)
    Wrap a secret key using the public key of the named entity and return the wrapped key as a byte array.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

  • Constructor Details

  • Method Details

    • setPassword

      public abstract void setPassword​(java.lang.String password)
      Supply the password for the keystore used with the CryptoEngine. Generally, this will be available at a later time after the object is created because it might require special actions to supply the password value.
      Parameters:
      password -
    • setPassword

      public abstract void setPassword​(char[] password)
      Supply the password for the keystore used with the CryptoEngine. Generally, this will be available at a later time after the object is created because it might require special actions to supply the password value.
      Parameters:
      password -
    • getPublicKey

      public abstract java.security.PublicKey getPublicKey​(java.lang.String entityName) throws KeyNotFound
      Return the public key of an entity from the KeyStore
      Parameters:
      entityName - name of the entity
      Returns:
      PublicKey
      Throws:
      KeyNotFound
    • getPrivateKey

      public abstract java.security.PrivateKey getPrivateKey() throws KeyNotFound, java.lang.Exception
      Return the private key of the current entity from the KeyStore
      Returns:
      PrivateKey
      Throws:
      KeyNotFound
      java.lang.Exception
    • randomKey

      public abstract javax.crypto.SecretKey randomKey()
      Generate a random symmetric encryption key. Such keys are used for the session encryption and also in the practical implementation of public key encryption.
      Returns:
      SecretKeySpec
    • wrapSecretKey

      public abstract byte[] wrapSecretKey​(java.security.Key spec, java.lang.String name)
      Wrap a secret key using the public key of the named entity and return the wrapped key as a byte array.
      Parameters:
      spec - an object that contains an encryption key
      Returns:
      byte[] the wrapped secret key
    • unwrapSecretKey

      public abstract javax.crypto.SecretKey unwrapSecretKey​(byte[] wrappedKey)
      UnWrap a secret key contained in a byte array using the private key for this entity and return the SecretKeySpec
      Parameters:
      wrapped - a byte array containing the wrapped key object that contains an encryption key
      Returns:
      SecretKeySpec the secret key
    • encryptStream

      public abstract void encryptStream​(java.lang.String name, java.io.InputStream in, java.io.DataOutputStream out) throws KeyNotFound, java.lang.Exception
      Encrypt clear text from an input stream using Public Key Encryption applying the public key of the named destination and placing the encrypted text on the output stream.
      Parameters:
      name -
      in -
      out -
      Throws:
      KeyNotFound
      java.lang.Exception
      java.security.NoSuchAlgorithmException
    • decryptStream

      public abstract void decryptStream​(java.io.DataInputStream in, java.io.OutputStream out) throws java.security.NoSuchAlgorithmException, java.lang.Exception
      Decrypt the text on the input stream using the current entities private key and the public key encryption system.
      Parameters:
      in -
      out -
      Throws:
      java.security.NoSuchAlgorithmException
      java.lang.Exception
    • crypt

      public abstract void crypt​(java.io.InputStream inStream, java.io.OutputStream outStream, javax.crypto.Cipher cipher) throws java.io.IOException, java.security.GeneralSecurityException
      Perform a symmetric key encryption or decryption on stream. In this system, unencrypted data is text but encrypted data is a byte stream.
      Parameters:
      inStream - input data
      outStream - output data
      cipher - an object of type Cipher
      Throws:
      java.io.IOException
      java.security.GeneralSecurityException
    • signText

      public abstract java.lang.String signText​(java.lang.String text)
      Sign a text string by computing its encrypted value under this entities private key. The implementation supplies a standard algorithm and converts the signature to a hexadecimal text representation.
      Parameters:
      text - the text to be signed
      Returns:
      a digital signature of the input text as a hexadecimal string representation
    • verifySignedText

      public abstract boolean verifySignedText​(java.lang.String name, java.lang.String text, java.lang.String signature)
      Check a signature by decrypting it with the public key of the declared name and comparing the decrypted text with the original text.
      Parameters:
      name -
      signature -
      Returns:
    • hexStringToByteArray

      public static byte[] hexStringToByteArray​(java.lang.String s)
      A function to convert a string containing a byte array written as hexadecimal into a byte array. By our conventions, a hexadecimal representation of a string must have an even number of characters. This function will throw an indexing out of bounds exception if this assumption is false.
      Parameters:
      s - A string containing a hexadecimal representation of an array of bytes.
      Returns:
      byte array
    • byteArrayToHex

      public static java.lang.String byteArrayToHex​(byte[] ba)
      A function to convert a byte array to a string with hexadecimal conversion
      Parameters:
      ba -
      Returns:
      string containing 2 characters for each byte, the pair being the hexadecimal equivalent of the byte.