To encrypt and decrypt data in Java, use the built-in javax.crypto API with AES in GCM mode (AES/GCM/NoPadding) for the data, and RSA with OAEP padding only for small values such as keys. Never use the bare Cipher.getInstance("AES") form seen in older tutorials: it defaults to ECB mode, which leaks patterns and provides no integrity protection.
GCM is an authenticated mode, so decryption fails with an exception if the ciphertext was modified. Every message needs a fresh random IV (nonce), which is stored next to the ciphertext.
A transformation string has the form algorithm/mode/padding. When you pass only "AES", the JDK picks AES/ECB/PKCS5Padding. ECB encrypts identical plaintext blocks into identical ciphertext blocks and has no authentication, so attackers can spot repetition and tamper with data undetected. Also avoid getBytes() without a charset, because it uses the platform default; always pass StandardCharsets.UTF_8.
This complete program encrypts a string with a 256-bit key, prepends the random 12-byte IV to the ciphertext, and Base64-encodes the result:
The output changes on every run because the IV is random. Never reuse an IV with the same key in GCM, since that breaks both confidentiality and authentication.
If the key comes from a user password, do not use the password bytes directly. Derive the key with PBKDF2 and a random salt, and store the salt with the ciphertext:
The iteration count of 600,000 follows current OWASP guidance for PBKDF2-HMAC-SHA256; check the latest recommendation when you deploy.
RSA uses a public key to encrypt and a private key to decrypt. Use at least 2048-bit keys and OAEP padding. The old Cipher.getInstance("RSA") defaults to PKCS#1 v1.5 padding, which is vulnerable to padding oracle attacks.
The ECB in the name is a historical label and has no effect for RSA. RSA can only encrypt data smaller than the key size minus padding overhead, so it is not suitable for files or large messages.
| Aspect | AES-GCM | RSA-OAEP |
|---|---|---|
| Key type | One shared secret key | Public and private key pair |
| Speed | Very fast | Slow |
| Data size | Any size | Smaller than the key |
| Typical use | Encrypting data at rest | Exchanging or wrapping keys, receiving data from strangers |
Real systems combine them in hybrid encryption: generate a random AES key, encrypt the data with it, then encrypt that AES key with the recipient's RSA public key.
AES-256 is a safe default and the JDK supports it without extra setup in current versions. AES-128 is also considered secure; 256 only adds margin.
Not in source code or the same database as the data. Use a key management service, a hardware security module, or a Java KeyStore protected by an externally supplied password.
The key, IV or ciphertext is wrong, or the data was modified after encryption. GCM verifies an authentication tag, so any change is detected.
No. Use standard modes such as AES-GCM, or a vetted library such as Google Tink, which makes safe choices for you.
Use AES/GCM/NoPadding with a random 12-byte IV per message for data, derive keys from passwords with PBKDF2, and use RSA/ECB/OAEPWithSHA-256AndMGF1Padding only for small payloads like keys. Avoid default transformation strings, always specify UTF-8, and keep keys out of your code.