Class CryptoUtils


  • public final class CryptoUtils
    extends java.lang.Object
    Simple usage of standard cipher features from JRE.
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private CryptoUtils()
      Private constructor to ensure that the class acts as a true utility class i.e.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      private static javax.crypto.Cipher createCipher​(java.lang.String algorithm, byte[] secretKey, int mode)
      Creates a cipher for a given algorithm and secret.
      static java.lang.String decrypt​(java.lang.String algo, byte[] secretKey, byte[] encrypted)
      Decrypts a bytes array.
      static java.lang.String decrypt​(java.lang.String algo, java.lang.String base64Secret, byte[] encrypted)
      Decrypts a bytes array.
      private static byte[] doFinal​(java.lang.String algo, byte[] secretKey, int mode, byte[] what)
      Does final processing.
      static byte[] encrypt​(java.lang.String algo, byte[] secretKey, java.lang.String content)
      Encrypts a content string.
      static byte[] encrypt​(java.lang.String algo, java.lang.String base64Secret, java.lang.String content)
      Encrypts a content string.
      static java.lang.String makeNonce​(java.lang.String secretKey)
      Generates a nonce as recommended in section 3.2.1 of RFC-2617, but without the ETag field.
      • Methods inherited from class java.lang.Object

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

      • CryptoUtils

        private CryptoUtils()
        Private constructor to ensure that the class acts as a true utility class i.e. it isn't instantiable and extensible.
    • Method Detail

      • createCipher

        private static javax.crypto.Cipher createCipher​(java.lang.String algorithm,
                                                        byte[] secretKey,
                                                        int mode)
                                                 throws java.security.GeneralSecurityException
        Creates a cipher for a given algorithm and secret.
        Parameters:
        algorithm - The cryptographic algorithm.
        secretKey - The cryptographic secret.
        mode - The cipher mode, either Cipher.ENCRYPT_MODE or Cipher.DECRYPT_MODE.
        Returns:
        The new cipher.
        Throws:
        java.security.GeneralSecurityException
      • decrypt

        public static java.lang.String decrypt​(java.lang.String algo,
                                               byte[] secretKey,
                                               byte[] encrypted)
                                        throws java.security.GeneralSecurityException
        Decrypts a bytes array.
        Parameters:
        algo - The cryptographic algorithm.
        secretKey - The cryptographic secret key.
        encrypted - The encrypted bytes.
        Returns:
        The decrypted content string.
        Throws:
        java.security.GeneralSecurityException
      • decrypt

        public static java.lang.String decrypt​(java.lang.String algo,
                                               java.lang.String base64Secret,
                                               byte[] encrypted)
                                        throws java.security.GeneralSecurityException
        Decrypts a bytes array.
        Parameters:
        algo - The cryptographic algorithm.
        base64Secret - The cryptographic secret key, encoded as a Base64 string.
        encrypted - The encrypted bytes.
        Returns:
        The decrypted content string.
        Throws:
        java.security.GeneralSecurityException
      • doFinal

        private static byte[] doFinal​(java.lang.String algo,
                                      byte[] secretKey,
                                      int mode,
                                      byte[] what)
                               throws java.security.GeneralSecurityException
        Does final processing.
        Parameters:
        algo - The cryptographic algorithm.
        secretKey - The cryptographic secret key.
        mode - The processing mode, either Cipher.DECRYPT_MODE or Cipher.ENCRYPT_MODE.
        what - The byte array to process.
        Returns:
        The processed byte array.
        Throws:
        java.security.GeneralSecurityException
      • encrypt

        public static byte[] encrypt​(java.lang.String algo,
                                     byte[] secretKey,
                                     java.lang.String content)
                              throws java.security.GeneralSecurityException
        Encrypts a content string.
        Parameters:
        algo - The cryptographic algorithm.
        secretKey - The cryptographic secret key.
        content - The content string to encrypt.
        Returns:
        The encrypted bytes.
        Throws:
        java.security.GeneralSecurityException
      • encrypt

        public static byte[] encrypt​(java.lang.String algo,
                                     java.lang.String base64Secret,
                                     java.lang.String content)
                              throws java.security.GeneralSecurityException
        Encrypts a content string.
        Parameters:
        algo - The cryptographic algorithm.
        base64Secret - The cryptographic secret, encoded as a Base64 string.
        content - The content string to encrypt.
        Returns:
        The encrypted bytes.
        Throws:
        java.security.GeneralSecurityException
      • makeNonce

        public static java.lang.String makeNonce​(java.lang.String secretKey)
        Generates a nonce as recommended in section 3.2.1 of RFC-2617, but without the ETag field. The format is:
         Base64.encodeBytes(currentTimeMS + ":"
                 + md5String(currentTimeMS + ":" + secretKey))
         
        Parameters:
        secretKey - a secret value known only to the creator of the nonce. It's inserted into the nonce, and can be used later to validate the nonce.