Class SunJceCrypt

  • All Implemented Interfaces:
    ICrypt

    public class SunJceCrypt
    extends AbstractCrypt
    Provide some simple means to encrypt and decrypt strings such as passwords. The whole implementation is based around Sun's security providers and uses the PBEWithMD5AndDES method to encrypt and decrypt the data.
    Author:
    Juergen Donnerstag
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static java.lang.String DEFAULT_CRYPT_METHOD
      Name of the default encryption method
      static byte[] SALT
      Salt
    • Constructor Summary

      Constructors 
      Constructor Description
      SunJceCrypt()
      Constructor
      SunJceCrypt​(java.lang.String cryptMethod)
      Constructor that uses a custom encryption method (cipher).
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      protected javax.crypto.Cipher createCipher​(javax.crypto.SecretKey key, java.security.spec.AlgorithmParameterSpec spec, int mode)
      Creates the Cipher that will do the de-/encryption.
      protected java.security.spec.KeySpec createKeySpec()  
      protected java.security.spec.AlgorithmParameterSpec createParameterSpec()  
      protected byte[] crypt​(byte[] input, int mode)
      Crypts the given byte array
      protected javax.crypto.SecretKey generateSecretKey()
      Generate the de-/encryption key.
      • Methods inherited from class java.lang.Object

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

      • DEFAULT_CRYPT_METHOD

        public static final java.lang.String DEFAULT_CRYPT_METHOD
        Name of the default encryption method
        See Also:
        Constant Field Values
      • SALT

        public static final byte[] SALT
        Salt
    • Constructor Detail

      • SunJceCrypt

        public SunJceCrypt()
        Constructor
      • SunJceCrypt

        public SunJceCrypt​(java.lang.String cryptMethod)
        Constructor that uses a custom encryption method (cipher). You may need to override createKeySpec() and/or createParameterSpec() for the custom cipher.
        Parameters:
        cryptMethod - the name of encryption method (the cipher)
    • Method Detail

      • crypt

        protected byte[] crypt​(byte[] input,
                               int mode)
                        throws java.security.GeneralSecurityException
        Crypts the given byte array
        Specified by:
        crypt in class AbstractCrypt
        Parameters:
        input - byte array to be encrypted
        mode - crypt mode
        Returns:
        the input crypted. Null in case of an error
        Throws:
        java.security.GeneralSecurityException
      • createCipher

        protected javax.crypto.Cipher createCipher​(javax.crypto.SecretKey key,
                                                   java.security.spec.AlgorithmParameterSpec spec,
                                                   int mode)
                                            throws java.security.GeneralSecurityException
        Creates the Cipher that will do the de-/encryption.
        Parameters:
        key - the secret key to use
        spec - the parameters spec to use
        mode - the mode (Cipher.ENCRYPT_MODE or Cipher.DECRYPT_MODE)
        Returns:
        the cipher that will do the de-/encryption
        Throws:
        java.security.GeneralSecurityException
      • generateSecretKey

        protected javax.crypto.SecretKey generateSecretKey()
                                                    throws java.security.NoSuchAlgorithmException,
                                                           java.security.spec.InvalidKeySpecException
        Generate the de-/encryption key.

        Note: if you don't provide your own encryption key, the implementation will use a default. Be aware that this is potential security risk. Thus make sure you always provide your own one.

        Returns:
        secretKey the security key generated
        Throws:
        java.security.NoSuchAlgorithmException - unable to find encryption algorithm specified
        java.security.spec.InvalidKeySpecException - invalid encryption key
      • createParameterSpec

        protected java.security.spec.AlgorithmParameterSpec createParameterSpec()
        Returns:
        the parameter spec to be used for the configured crypt method
      • createKeySpec

        protected java.security.spec.KeySpec createKeySpec()
        Returns:
        the key spec to be used for the configured crypt method