Overview
Thejavax.crypto package provides the Java Cryptographic Extension (JCE) framework for:
- Symmetric Encryption: AES, ChaCha20, DES algorithms
- Key Generation: Secret key generation for symmetric algorithms
- Key Agreement: Diffie-Hellman and ECDH key agreement
- Message Authentication: MAC (Message Authentication Code) generation
- Key Derivation: Password-based key derivation (PBKDF2)
- Key Wrapping: Secure key transport
Core Classes
Cipher
Provides the functionality of a cryptographic cipher for encryption and decryption.Returns a Cipher object that implements the specified transformation.Transformation Format:
algorithm/mode/padding or just algorithmRequired transformations (all Java platforms):AES/CBC/NoPadding(128)AES/CBC/PKCS5Padding(128)AES/ECB/NoPadding(128)AES/ECB/PKCS5Padding(128)AES/GCM/NoPadding(128, 256)ChaCha20-Poly1305RSA/ECB/OAEPWithSHA-1AndMGF1Padding(1024, 2048)RSA/ECB/OAEPWithSHA-256AndMGF1Padding(1024, 2048)
NoSuchAlgorithmException- if transformation is null, empty, or invalidNoSuchPaddingException- if padding scheme is not available
Initializes this cipher with a key.Operation modes:
Cipher.ENCRYPT_MODE- Encrypt modeCipher.DECRYPT_MODE- Decrypt modeCipher.WRAP_MODE- Key wrapping modeCipher.UNWRAP_MODE- Key unwrapping mode
InvalidKeyException- if the key is inappropriate for initializing this cipher
Continues a multiple-part encryption or decryption operation. Returns the next chunk of processed data, or null if no output is produced.
Finishes a multiple-part encryption or decryption operation. Returns the last chunk of processed data.Throws:
IllegalBlockSizeException- if total input length is not multiple of block size (for encryption)BadPaddingException- if decryption detects incorrect padding
Encrypts or decrypts data in a single-part operation. Equivalent to calling update(input) followed by doFinal().
Returns the initialization vector (IV) in a new buffer. Useful when a random IV was created.
Returns the block size (in bytes). Returns 0 if this cipher is not a block cipher.
For AEAD modes like GCM, call
doFinal() to verify the authentication tag. An AEADBadTagException will be thrown if verification fails.KeyGenerator
Provides the functionality of a secret (symmetric) key generator.Returns a KeyGenerator object that generates secret keys for the specified algorithm.Required algorithms (all Java platforms):
AES(128, 256)ChaCha20HmacSHA1HmacSHA256
NoSuchAlgorithmException- if no provider supports the algorithm
Initializes this key generator for a certain keysize (in bits).Throws:
InvalidParameterException- if keysize is wrong or not supported
Initializes this key generator for a certain keysize, using a user-provided source of randomness.
Generates a secret key.
SecretKey
Interface for secret (symmetric) keys. This interface contains no methods; it serves to group and provide type safety for secret keys.SecretKey Usage
Mac
Provides the functionality of a Message Authentication Code (MAC) algorithm.Returns a Mac object that implements the specified MAC algorithm.Common algorithms:
HmacSHA1HmacSHA256HmacSHA384HmacSHA512
Initializes this Mac object with the given key.Throws:
InvalidKeyException- if the given key is inappropriate
Processes the given array of bytes.
Finishes the MAC operation and returns the MAC result. Resets the Mac object.
Advanced Features
Password-Based Encryption
Key Agreement
Diffie-Hellman
Sealed Objects
SealedObject
Exception Handling
- BadPaddingException
- IllegalBlockSizeException
- InvalidKeyException
Thrown when decryption detects incorrect padding. Usually indicates:
- Wrong key used for decryption
- Corrupted ciphertext
- Tampering detected
Best Practices
Thread Safety
Most classes in javax.crypto are NOT thread-safe:Cipher- Not thread-safeKeyGenerator- Not thread-safe after initializationMac- Not thread-safe
See Also
- java.security Package - Core security framework
- JGSS API - Authentication and secure communication