Class EncryptionUtil


  • public class EncryptionUtil
    extends java.lang.Object
    Utility class for Encryption
    • Constructor Summary

      Constructors 
      Constructor Description
      EncryptionUtil()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static byte[] decrypt​(java.lang.String base64EncryptedStringValue, java.lang.String key)  
      static java.lang.String decryptToString​(java.lang.String base64EncryptedStringValue, java.lang.String key)  
      static byte[] encrypt​(java.lang.String dataToEncrypt, java.lang.String key)
      Encrypts the given data
      static java.lang.String encryptToBase64String​(java.lang.String dataToEncrypt, java.lang.String key)
      Encrypts and returns base64 encoded String
      static java.lang.String printHexBinary​(byte[] data)
      Converts byte[] to hex String Copy of javax.xml.bind.DatatypeConverter (Lower case letter though)
      static java.lang.String sha256Hex​(java.lang.String data)
      Calculates the SHA-256 digest and returns the value as a hex string.
      • Methods inherited from class java.lang.Object

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

      • MD5_ALGORITHM

        public static java.lang.String MD5_ALGORITHM
      • SHA256_ALGORITHM

        public static java.lang.String SHA256_ALGORITHM
      • SHA1_ALGORITHM

        public static java.lang.String SHA1_ALGORITHM
      • SUPPORT_SEAMLESS_LOGIN_COMPATIBILITY

        public static java.lang.String SUPPORT_SEAMLESS_LOGIN_COMPATIBILITY
    • Constructor Detail

      • EncryptionUtil

        public EncryptionUtil()
    • Method Detail

      • encrypt

        public static byte[] encrypt​(java.lang.String dataToEncrypt,
                                     java.lang.String key)
                              throws java.security.NoSuchAlgorithmException,
                                     javax.crypto.NoSuchPaddingException,
                                     java.security.InvalidKeyException,
                                     javax.crypto.IllegalBlockSizeException,
                                     javax.crypto.BadPaddingException
        Encrypts the given data
        Parameters:
        dataToEncrypt -
        key -
        Returns:
        Throws:
        java.security.NoSuchAlgorithmException
        javax.crypto.NoSuchPaddingException
        java.security.InvalidKeyException
        javax.crypto.IllegalBlockSizeException
        javax.crypto.BadPaddingException
      • encryptToBase64String

        public static java.lang.String encryptToBase64String​(java.lang.String dataToEncrypt,
                                                             java.lang.String key)
                                                      throws java.security.NoSuchAlgorithmException,
                                                             javax.crypto.NoSuchPaddingException,
                                                             java.security.InvalidKeyException,
                                                             javax.crypto.IllegalBlockSizeException,
                                                             javax.crypto.BadPaddingException
        Encrypts and returns base64 encoded String
        Parameters:
        dataToEncrypt -
        key -
        Returns:
        Throws:
        java.security.NoSuchAlgorithmException
        javax.crypto.NoSuchPaddingException
        java.security.InvalidKeyException
        javax.crypto.IllegalBlockSizeException
        javax.crypto.BadPaddingException
      • decrypt

        public static byte[] decrypt​(java.lang.String base64EncryptedStringValue,
                                     java.lang.String key)
                              throws java.security.NoSuchAlgorithmException,
                                     javax.crypto.NoSuchPaddingException,
                                     java.security.InvalidKeyException,
                                     javax.crypto.IllegalBlockSizeException,
                                     javax.crypto.BadPaddingException
        Parameters:
        base64EncryptedStringValue - Previously Encrypted base64EncodedString
        key -
        Returns:
        Throws:
        java.security.NoSuchAlgorithmException
        javax.crypto.NoSuchPaddingException
        java.security.InvalidKeyException
        javax.crypto.IllegalBlockSizeException
        javax.crypto.BadPaddingException
      • decryptToString

        public static java.lang.String decryptToString​(java.lang.String base64EncryptedStringValue,
                                                       java.lang.String key)
                                                throws java.security.NoSuchAlgorithmException,
                                                       javax.crypto.NoSuchPaddingException,
                                                       java.security.InvalidKeyException,
                                                       javax.crypto.IllegalBlockSizeException,
                                                       javax.crypto.BadPaddingException
        Parameters:
        base64EncryptedStringValue - Previously Encrypted base64EncodedString
        hashKey -
        keySpecAlgorithm - ex. AES, DES etc.
        cipherAlgorithm - ex. "AES/ECB/PKCS5Padding". Refer to https://docs.oracle.com/javase/8/docs/api/javax/crypto/Cipher.html Limited modes & padding supported( as for ex. CBC mode requires additional parameters to be provided. and not supported here)
        Returns:
        Throws:
        java.security.NoSuchAlgorithmException
        javax.crypto.NoSuchPaddingException
        java.security.InvalidKeyException
        javax.crypto.IllegalBlockSizeException
        javax.crypto.BadPaddingException
      • printHexBinary

        public static java.lang.String printHexBinary​(byte[] data)
        Converts byte[] to hex String Copy of javax.xml.bind.DatatypeConverter (Lower case letter though)
        Parameters:
        data -
        Returns:
      • sha256Hex

        public static java.lang.String sha256Hex​(java.lang.String data)
        Calculates the SHA-256 digest and returns the value as a hex string.
        Parameters:
        data - Data to digest
        Returns:
        SHA-256 digest as a hex string