Class CryptoUtil

  • Direct Known Subclasses:
    CryptoUtilJ8

    public class CryptoUtil
    extends Object
    Helper class to provde generic functions to work with CryptoStreams. The code uses parts from Markus Hahn's Blowfish library found at http://blowfishj.sourceforge.net/
    Author:
    Siegfried Goeschl , Markus Hahn
    • Constructor Detail

      • CryptoUtil

        protected CryptoUtil()
      • CryptoUtil

        protected CryptoUtil​(byte[] salt,
                             int count)
        Parameters:
        salt - the salt for the PBE algorithm
        count - the iteration for PBEParameterSpec
    • Method Detail

      • getInstance

        public static CryptoUtil getInstance()
        Factory method to get a default instance
        Returns:
        an instance of the CryptoUtil
      • getInstance

        public static CryptoUtil getInstance​(byte[] salt,
                                             int count)
        Factory method to get a default instance
        Parameters:
        salt - the salt for the PBE algorithm
        count - the iteration for PBEParameterSpec
        Returns:
        an instance of the CryptoUtil
      • encrypt

        public void encrypt​(CryptoStreamFactory factory,
                            Object source,
                            Object target,
                            char[] password)
                     throws GeneralSecurityException,
                            IOException
        Copies from a source to a target object using encryption and a caller supplied CryptoStreamFactory.
        Parameters:
        factory - the factory to create the crypto streams
        source - the source object
        target - the target object
        password - the password to use for encryption
        Throws:
        GeneralSecurityException - accessing JCE failed
        IOException - accessing the souce failed
      • encryptString

        public String encryptString​(CryptoStreamFactory factory,
                                    String plainText,
                                    char[] password,
                                    boolean withClearCode)
                             throws GeneralSecurityException,
                                    IOException
        Encrypts a string into a hex string.
        Parameters:
        factory - the factory to create the crypto streams
        plainText - the plain text to be encrypted
        password - the password for encryption
        withClearCode - boolean to indicate, that a string containing how it was decoded is included
        Returns:
        the encrypted string
        Throws:
        GeneralSecurityException - accessing JCE failed
        IOException - accessing the souce failed
      • decryptStringWithClearCode

        public String decryptStringWithClearCode​(String cipherText,
                                                 char[] password)
                                          throws GeneralSecurityException,
                                                 IOException
        Decrypts an encrypted string into the plain text. The encrypted string must be a hex string created by encryptString. Decrypts encrypted text after CryptoParametersJ8.CLEAR_CODE_DEFAULT. Removes ClearCode length of 10 bit, before decrpyting expected as prefix.
        Parameters:
        cipherText - the encrypted text to be decrypted
        password - the password for decryption
        Returns:
        the decrypted string
        Throws:
        GeneralSecurityException - accessing JCE failed
        IOException - accessing the souce failed
      • decryptString

        public String decryptString​(String cipherText,
                                    char[] password)
                             throws GeneralSecurityException,
                                    IOException
        Decrypts an encrypted string into the plain text. The encrypted string must be a hex string created by encryptString.
        Parameters:
        cipherText - the encrypted text to be decrypted
        password - the password for decryption
        Returns:
        the decrypted string
        Throws:
        GeneralSecurityException - accessing JCE failed
        IOException - accessing the souce failed
      • decryptString

        public String decryptString​(String cipherText,
                                    char[] password,
                                    boolean withClearCode)
                             throws GeneralSecurityException,
                                    IOException
        Decrypts an encrypted string into the plain text. The encrypted string must be a hex string created by encryptString.
        Parameters:
        cipherText - the encrypted text to be decrypted
        password - the password for decryption
        withClearCode - boolean to indicate, that a string containing how it was decoded was included during encryption
        Returns:
        the decrypted string
        Throws:
        GeneralSecurityException - accessing JCE failed
        IOException - accessing the souce failed
      • decrypt

        protected void decrypt​(CryptoStreamFactory factory,
                               Object source,
                               Object target,
                               char[] password)
                        throws GeneralSecurityException,
                               IOException
        Copies from a source to a target object using decryption and a caller-suppier CryptoStreamFactory.
        Parameters:
        factory - the factory to create the crypto streams
        source - the source object
        target - the target object
        password - the password to use for decryption
        Throws:
        GeneralSecurityException - accessing JCE failed
        IOException - accessing the souce failed
      • decryptString

        public String decryptString​(CryptoStreamFactory factory,
                                    String cipherText,
                                    char[] password)
                             throws GeneralSecurityException,
                                    IOException
        Decrypts an encrypted string into the plain text. The encrypted string must be a hex string created by encryptString.
        Parameters:
        factory - the factory to create the crypto streams
        cipherText - the encrypted text to be decrypted
        password - the password for decryption
        Returns:
        the decrypted string
        Throws:
        GeneralSecurityException - accessing JCE failed
        IOException - accessing the souce failed
      • copy

        public static long copy​(InputStream is,
                                OutputStream os)
                         throws IOException
        Deprecated.
        use StreamUtil instead
        Pumps the input stream to the output stream.
        Parameters:
        is - the source input stream
        os - the target output stream
        Returns:
        the number of bytes copied
        Throws:
        IOException - the copying failed
      • getCryptoStreamFactory

        public CryptoStreamFactory getCryptoStreamFactory()
        Returns:
        the CryptoStreamFactory to be used