Class Cipher

  • Direct Known Subclasses:
    AESCipher, RSACipher

    public abstract class Cipher
    extends java.lang.Object
    This class provides the functionality of a cryptographic cipher for encryption

    If you get a totalcross.crypto.CryptoException: Illegal key size, you must download the strong cryptography files from Oracle site. In order to do that, go to the ReadMe file whole link is below the download link. In this file, search for "Unlimited Strength Java Cryptography Extension" and follow the instructions.

    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected int chaining  
      static int CHAINING_CBC
      Constant used to initialize cipher using CBC chaining.
      static int CHAINING_ECB
      Constant used to initialize cipher using ECB chaining.
      static int CHAINING_NONE
      Constant used to initialize cipher using no chaining.
      protected byte[] iv  
      protected Key key  
      protected java.lang.Object nativeHeap  
      protected int operation  
      static int OPERATION_DECRYPT
      Constant used to initialize cipher to decrypt.
      static int OPERATION_ENCRYPT
      Constant used to initialize cipher to encrypt.
      protected int padding  
      static int PADDING_NONE
      Constant used to initialize cipher using no padding.
      static int PADDING_PKCS1
      Constant used to initialize cipher using PKCS #1 padding.
      static int PADDING_PKCS5
      Constant used to initialize cipher using PKCS #5 padding.
      Comment about PKCS#5 padding:

      PKCS#5 padding is a padding scheme that always adds padding bytes even if the input size is a multiple of the block size (i.
    • Constructor Summary

      Constructors 
      Constructor Description
      Cipher()  
    • Method Summary

      All Methods Instance Methods Abstract Methods Concrete Methods 
      Modifier and Type Method Description
      protected abstract void doReset()  
      abstract java.lang.String getAlgorithm()
      Returns the name of the algorithm.
      abstract int getBlockLength()
      Returns the block length.
      byte[] getIV()
      Returns the initialization vector (IV) in a new buffer.
      byte[] getOutput()
      Finalizes the encryption or decryption operation (depending on how this cipher was initialized) by processing all the accumulated input data and returning the result in a new buffer.
      protected abstract boolean isChainingSupported​(int chaining)  
      protected abstract boolean isKeySupported​(Key key, int operation)  
      protected abstract boolean isPaddingSupported​(int padding)  
      protected abstract byte[] process​(byte[] data)  
      void reset​(int operation, Key key)
      Initializes this cipher in encryption or decryption mode, without chaining or padding.
      void reset​(int operation, Key key, int chaining)
      Initializes this cipher in encryption or decryption mode, with the given chaining mode and without a padding.
      void reset​(int operation, Key key, int chaining, byte[] iv)
      Initializes this cipher in encryption or decryption mode, with the given chaining mode, initialization vector and without a padding.
      void reset​(int operation, Key key, int chaining, byte[] iv, int padding)
      Initializes this cipher in encryption or decryption mode, with the given chaining mode, initialization vector padding.
      java.lang.String toString()
      Returns the name of the algorithm.
      void update​(byte[] data)
      Updates the input data that will be processed by this cipher algorithm.
      void update​(byte[] data, int start, int count)
      Updates the input data that will be processed by this cipher algorithm.
      void update​(int data)
      Updates the input data that will be processed by this cipher algorithm.
      • Methods inherited from class java.lang.Object

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

      • operation

        protected int operation
      • key

        protected Key key
      • chaining

        protected int chaining
      • iv

        protected byte[] iv
      • padding

        protected int padding
      • nativeHeap

        protected java.lang.Object nativeHeap
      • OPERATION_ENCRYPT

        public static final int OPERATION_ENCRYPT
        Constant used to initialize cipher to encrypt.
        See Also:
        Constant Field Values
      • OPERATION_DECRYPT

        public static final int OPERATION_DECRYPT
        Constant used to initialize cipher to decrypt.
        See Also:
        Constant Field Values
      • CHAINING_NONE

        public static final int CHAINING_NONE
        Constant used to initialize cipher using no chaining.
        See Also:
        Constant Field Values
      • CHAINING_ECB

        public static final int CHAINING_ECB
        Constant used to initialize cipher using ECB chaining.
        See Also:
        Constant Field Values
      • CHAINING_CBC

        public static final int CHAINING_CBC
        Constant used to initialize cipher using CBC chaining.
        See Also:
        Constant Field Values
      • PADDING_NONE

        public static final int PADDING_NONE
        Constant used to initialize cipher using no padding.
        See Also:
        Constant Field Values
      • PADDING_PKCS1

        public static final int PADDING_PKCS1
        Constant used to initialize cipher using PKCS #1 padding.
        See Also:
        Constant Field Values
      • PADDING_PKCS5

        public static final int PADDING_PKCS5
        Constant used to initialize cipher using PKCS #5 padding.
        Comment about PKCS#5 padding:

        PKCS#5 padding is a padding scheme that always adds padding bytes even if the input size is a multiple of the block size (i. e. 16 bytes). The value of the padding bytes is the number of bytes added to get a multiple of the block size. Thus, once deciphered, you can immediately find out how many padding bytes have been added by looking the value of the last byte of the output buffer. This is always true since there must always be at least one padding byte.

        See http://tools.ietf.org/html/rfc3852#section-6.3 for more details.

        For instance, if you use PKCS#5 with a 16 bytes input buffer, 16 values of 0x10 will be added resulting in 2 input blocks of 16 bytes each (32 bytes). Once deciphered, the 16 bytes padding block will be removed to return to the initial 16 bytes input buffer.
        See Also:
        Constant Field Values
    • Constructor Detail

      • Cipher

        public Cipher()
    • Method Detail

      • toString

        public final java.lang.String toString()
        Returns the name of the algorithm.
        Overrides:
        toString in class java.lang.Object
        Returns:
        The name of the algorithm whose class heirs from Cipher.
      • getIV

        public final byte[] getIV()
        Returns the initialization vector (IV) in a new buffer. This is useful in the case where a random IV was created.
        Returns:
        The initialization vector in a new buffer, or null if the underlying algorithm does not use an IV, or if the IV has not been set yet.
      • reset

        public final void reset​(int operation,
                                Key key)
                         throws CryptoException
        Initializes this cipher in encryption or decryption mode, without chaining or padding. If this algorithm requires an initialization vector, it will be generated using random values. Calling this method will also reset the input data buffer.
        Parameters:
        operation - The operation mode of this cipher (OPERATION_ENCRYPT or OPERATION_DECRYPT).
        key - The key.
        Throws:
        CryptoException - If one or more initialization parameters are invalid or the cipher fails to initialize with the given parameters.
      • reset

        public final void reset​(int operation,
                                Key key,
                                int chaining)
                         throws CryptoException
        Initializes this cipher in encryption or decryption mode, with the given chaining mode and without a padding. If this algorithm requires an initialization vector, it will be generated using random values. Calling this method will also reset the input data buffer.
        Parameters:
        operation - The operation mode of this cipher (OPERATION_ENCRYPT or OPERATION_DECRYPT).
        key - The key.
        chaining - The chaining mode of this cipher (CHAINING_NONE, CHAINING_ECB, or CHAINING_CBC).
        Throws:
        CryptoException - If one or more initialization parameters are invalid or the cipher fails to initialize with the given parameters.
      • reset

        public final void reset​(int operation,
                                Key key,
                                int chaining,
                                byte[] iv)
                         throws CryptoException
        Initializes this cipher in encryption or decryption mode, with the given chaining mode, initialization vector and without a padding. If this algorithm requires an initialization vector and an invalid value was supplied, it will be generated using random values. Calling this method will also reset the input data buffer.
        Parameters:
        operation - The operation mode of this cipher (OPERATION_ENCRYPT or OPERATION_DECRYPT).
        key - The key.
        chaining - The chaining mode of this cipher (CHAINING_NONE, CHAINING_ECB, or CHAINING_CBC).
        iv - The initialization vector.
        Throws:
        CryptoException - if one or more initialization parameters are invalid or the cipher fails to initialize with the given parameters.
      • reset

        public final void reset​(int operation,
                                Key key,
                                int chaining,
                                byte[] iv,
                                int padding)
                         throws CryptoException
        Initializes this cipher in encryption or decryption mode, with the given chaining mode, initialization vector padding. If this algorithm requires an initialization vector and an invalid value was supplied, it will be generated using random values. Calling this method will also reset the input data buffer.
        Parameters:
        operation - The operation mode of this cipher (OPERATION_ENCRYPT or OPERATION_DECRYPT).
        key - The key.
        chaining - The chaining mode of this cipher (CHAINING_NONE, CHAINING_ECB, or CHAINING_CBC).
        iv - The initialization vector.
        padding - The padding mode of this cipher (PADDING_NONE, PADDING_PKCS1, or PADDING_PKCS5).
        Throws:
        CryptoException - if one or more initialization parameters are invalid or the cipher fails to initialize with the given parameters.
      • update

        public final void update​(int data)
        Updates the input data that will be processed by this cipher algorithm. The data will be accumulated in an input buffer to be processed when getOutput() is finally called.
        Parameters:
        data - The input data.
      • update

        public final void update​(byte[] data)
        Updates the input data that will be processed by this cipher algorithm. The data will be accumulated in an input buffer to be processed when getOutput() is finally called.
        Parameters:
        data - The input data.
      • update

        public final void update​(byte[] data,
                                 int start,
                                 int count)
        Updates the input data that will be processed by this cipher algorithm. The data will be accumulated in an input buffer to be processed when getOutput() is finally called.
        Parameters:
        data - The input data.
        start - The offset in data where the data starts.
        count - The input length.
      • getOutput

        public byte[] getOutput()
                         throws CryptoException
        Finalizes the encryption or decryption operation (depending on how this cipher was initialized) by processing all the accumulated input data and returning the result in a new buffer.
        Returns:
        The operation result in a new buffer.
        Throws:
        CryptoException
      • getAlgorithm

        public abstract java.lang.String getAlgorithm()
        Returns the name of the algorithm.
        Returns:
        The name of the algorithm whose class heirs from Cipher.
      • getBlockLength

        public abstract int getBlockLength()
        Returns the block length.
        Returns:
        The block length (in bytes), or 0 if the underlying algorithm is not a block cipher.
      • isKeySupported

        protected abstract boolean isKeySupported​(Key key,
                                                  int operation)
      • isChainingSupported

        protected abstract boolean isChainingSupported​(int chaining)
      • isPaddingSupported

        protected abstract boolean isPaddingSupported​(int padding)