๐ AES Encryption and Decryption in Java — Step-by-Step Example
In modern applications, data security is crucial — especially when handling sensitive information like passwords, tokens, or financial data.
One of the most widely used symmetric encryption techniques is AES (Advanced Encryption Standard).
This post explains how to encrypt and decrypt data using AES in Java, with a simple working example.
๐งฉ What is AES?
AES (Advanced Encryption Standard) is a symmetric key algorithm, meaning:
-
The same key is used for both encryption and decryption.
-
It provides strong security, fast performance, and is standardized by NIST.
It supports key sizes of:
-
128 bits (16 bytes)
-
192 bits (24 bytes)
-
256 bits (32 bytes)
For most cases, AES-128 offers a strong balance of speed and security.
⚙️ Step 1 — Create the AES Encryption Class
๐งพ Output
๐ก Note: The encrypted output
[B@2fc14f68represents a byte array’s memory address, not the actual encrypted text.
To see the readable Base64-encoded value, you can modify the print statement as shown below.
๐งฑ Step 2 — Display the Encrypted Text in Base64 (Optional)
To view or store the encrypted data safely (for logging or transmission), convert it to Base64:
And when decrypting:
Output:
๐ง How It Works — Step by Step
Here’s what happens under the hood:
๐งฉ Text-Based Flow
๐ Key Points to Remember
| Concept | Description |
|---|---|
| Algorithm | AES (Advanced Encryption Standard) |
| Type | Symmetric key (same key for encryption & decryption) |
| Key Sizes | 128, 192, or 256 bits |
| Cipher Mode Used | "AES" (defaults to ECB mode) |
| Encoding | Use Base64 for readable output |
| Security Note | For real-world security, use "AES/CBC/PKCS5Padding" or "AES/GCM/NoPadding" instead of plain "AES" (ECB) |
No comments:
Post a Comment