Package totalcross.crypto.cipher
Class Cipher
- java.lang.Object
-
- totalcross.crypto.cipher.Cipher
-
public abstract class Cipher extends java.lang.ObjectThis class provides the functionality of a cryptographic cipher for encryptionIf 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 intchainingstatic intCHAINING_CBCConstant used to initialize cipher using CBC chaining.static intCHAINING_ECBConstant used to initialize cipher using ECB chaining.static intCHAINING_NONEConstant used to initialize cipher using no chaining.protected byte[]ivprotected Keykeyprotected java.lang.ObjectnativeHeapprotected intoperationstatic intOPERATION_DECRYPTConstant used to initialize cipher to decrypt.static intOPERATION_ENCRYPTConstant used to initialize cipher to encrypt.protected intpaddingstatic intPADDING_NONEConstant used to initialize cipher using no padding.static intPADDING_PKCS1Constant used to initialize cipher using PKCS #1 padding.static intPADDING_PKCS5Constant 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 voiddoReset()abstract java.lang.StringgetAlgorithm()Returns the name of the algorithm.abstract intgetBlockLength()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 booleanisChainingSupported(int chaining)protected abstract booleanisKeySupported(Key key, int operation)protected abstract booleanisPaddingSupported(int padding)protected abstract byte[]process(byte[] data)voidreset(int operation, Key key)Initializes this cipher in encryption or decryption mode, without chaining or padding.voidreset(int operation, Key key, int chaining)Initializes this cipher in encryption or decryption mode, with the given chaining mode and without a padding.voidreset(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.voidreset(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.StringtoString()Returns the name of the algorithm.voidupdate(byte[] data)Updates the input data that will be processed by this cipher algorithm.voidupdate(byte[] data, int start, int count)Updates the input data that will be processed by this cipher algorithm.voidupdate(int data)Updates the input data that will be processed by this cipher algorithm.
-
-
-
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
-
-
Method Detail
-
toString
public final java.lang.String toString()
Returns the name of the algorithm.- Overrides:
toStringin classjava.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
nullif 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 CryptoExceptionInitializes 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_ENCRYPTorOPERATION_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 CryptoExceptionInitializes 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_ENCRYPTorOPERATION_DECRYPT).key- The key.chaining- The chaining mode of this cipher (CHAINING_NONE,CHAINING_ECB, orCHAINING_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 CryptoExceptionInitializes 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_ENCRYPTorOPERATION_DECRYPT).key- The key.chaining- The chaining mode of this cipher (CHAINING_NONE,CHAINING_ECB, orCHAINING_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 CryptoExceptionInitializes 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_ENCRYPTorOPERATION_DECRYPT).key- The key.chaining- The chaining mode of this cipher (CHAINING_NONE,CHAINING_ECB, orCHAINING_CBC).iv- The initialization vector.padding- The padding mode of this cipher (PADDING_NONE,PADDING_PKCS1, orPADDING_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 whengetOutput()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 whengetOutput()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 whengetOutput()is finally called.- Parameters:
data- The input data.start- The offset indatawhere the data starts.count- The input length.
-
getOutput
public byte[] getOutput() throws CryptoExceptionFinalizes 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.
-
doReset
protected abstract void doReset() throws NoSuchAlgorithmException, CryptoException
-
process
protected abstract byte[] process(byte[] data) throws CryptoException- Throws:
CryptoException
-
isKeySupported
protected abstract boolean isKeySupported(Key key, int operation)
-
isChainingSupported
protected abstract boolean isChainingSupported(int chaining)
-
isPaddingSupported
protected abstract boolean isPaddingSupported(int padding)
-
-