Symmetric Encryption (Criptografia Simétrica)¶
Symmetric encryption uses a single shared secret key for both encryption and decryption. This is the most efficient form of encryption, making it ideal for encrypting large amounts of data at rest or in transit when secure key exchange can be established beforehand.
Mathematical Foundation¶
Basic Operation¶
Given: - Plaintext $M$ (message to encrypt) - Secret key $K$ - Encryption function $E_K(\cdot)$ - Decryption function $D_K(\cdot)$
The encryption process is: $$C = E_K(M)$$
The decryption process is: $$M = D_K(C)$$
Where $C$ is the ciphertext.
Security Requirement¶
For a cipher to be secure, it must satisfy: $$D_K(E_K(M)) = M \quad \text{for all } M$$
And ideally: $$D_{K'}(E_K(M)) \neq M \quad \text{for any } K' \neq K$$
AES (Advanced Encryption Standard)¶
AES is the most widely used symmetric encryption algorithm today, adopted by NIST in 2001. It replaced the older Data Encryption Standard (DES).
Key Sizes¶
| Variant | Block Size | Key Size | Security Level |
|---|---|---|---|
| AES-128 | 128 bits | 128 bits | ~128-bit security |
| AES-192 | 128 bits | 192 bits | ~192-bit security |
| AES-256 | 128 bits | 256 bits | ~256-bit security |
Structure: Substitution-Permutation Network (SPN)¶
AES operates on a 4×4 state matrix of bytes and performs the following operations in each round:
1. Initial Round¶
- AddRoundKey: XOR the state with the round key
2. Main Rounds (9 for AES-128, 11 for AES-256)¶
Each main round consists of: - SubBytes: Non-linear substitution using S-box - ShiftRows: Cyclic shift of rows in the state matrix - MixColumns: Linear mixing of columns - AddRoundKey: XOR with round key
3. Final Round¶
Same as main rounds but without MixColumns
AES State Matrix Representation¶
State (128-bit block):
+--------+--------+--------+--------+
| s00 | s01 | s02 | s03 |
+--------+--------+--------+--------+
| s10 | s11 | s12 | s13 |
+--------+--------+--------+--------+
| s20 | s21 | s22 | s23 |
+--------+--------+--------+--------+
| s30 | s31 | s32 | s33 |
+--------+--------+--------+--------+
Each state byte is 8 bits, total = 4 × 4 × 8 = 128 bits
S-Box (Substitution Box)¶
The S-box provides non-linearity. It's derived from the multiplicative inverse in GF(2^8):
$$S[x] = \text{Inverse}(x)^{-1} \oplus \text{Constant}$$
Where the constant is 0x63 for differential uniformity properties.
Java Implementation¶
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.GCMParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.security.SecureRandom;
import java.util.Base64;
public class SymmetricEncryption {
private static final String ALGORITHM = "AES/GCM/NoPadding";
private static final int IV_LENGTH = 12; // 96 bits for GCM
/**
* Generates a new AES-256 key.
*/
public static SecretKey generateKey() throws Exception {
KeyGenerator keyGen = KeyGenerator.getInstance("AES");
keyGen.init(256); // Key size in bits
return keyGen.generateKey();
}
/**
* Encrypts data using AES-GCM.
* @param plaintext The data to encrypt
* @param key The secret key
* @return Base64-encoded ciphertext with IV prepended
*/
public static String encrypt(String plaintext, SecretKey key) throws Exception {
Cipher cipher = Cipher.getInstance(ALGORITHM);
// Generate random initialization vector (IV)
byte[] iv = new byte[IV_LENGTH];
SecureRandom secureRandom = new SecureRandom();
secureRandom.nextBytes(iv);
GCMParameterSpec parameterSpec = new GCMParameterSpec(128, iv);
cipher.init(Cipher.ENCRYPT_MODE, key, parameterSpec);
byte[] ciphertext = cipher.doFinal(plaintext.getBytes());
// Combine IV and ciphertext, then encode
byte[] combined = new byte[iv.length + ciphertext.length];
System.arraycopy(iv, 0, combined, 0, iv.length);
System.arraycopy(ciphertext, 0, combined, iv.length, ciphertext.length);
return Base64.getEncoder().encodeToString(combined);
}
/**
* Decrypts data encrypted with AES-GCM.
*/
public static String decrypt(String encodedCiphertext, SecretKey key) throws Exception {
// Decode and separate IV from ciphertext
byte[] combined = Base64.getDecoder().decode(encodedCiphertext);
byte[] iv = new byte[IV_LENGTH];
byte[] ciphertextBytes = new byte[combined.length - IV_LENGTH];
System.arraycopy(combined, 0, iv, 0, IV_LENGTH);
System.arraycopy(combined, IV_LENGTH, ciphertextBytes, 0, ciphertextBytes.length);
Cipher cipher = Cipher.getInstance(ALGORITHM);
GCMParameterSpec parameterSpec = new GCMParameterSpec(128, iv);
cipher.init(Cipher.DECRYPT_MODE, key, parameterSpec);
byte[] plaintextBytes = cipher.doFinal(ciphertextBytes);
return new String(plaintextBytes);
}
public static void main(String[] args) throws Exception {
SecretKey key = generateKey();
String message = "Secret message to encrypt";
// Encrypt
String encrypted = encrypt(message, key);
System.out.println("Encrypted: " + encrypted);
// Decrypt
String decrypted = decrypt(encrypted, key);
System.out.println("Decrypted: " + decrypted);
}
}
Using Java's Cipher Class (Simpler Example)¶
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.util.Base64;
public class SimpleAES {
public static String encrypt(String plaintext, byte[] keyBytes) throws Exception {
SecretKeySpec keySpec = new SecretKeySpec(keyBytes, "AES");
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, keySpec);
return Base64.getEncoder().encodeToString(cipher.doFinal(plaintext.getBytes()));
}
public static String decrypt(String encodedCiphertext, byte[] keyBytes) throws Exception {
SecretKeySpec keySpec = new SecretKeySpec(keyBytes, "AES");
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, keySpec);
return new String(cipher.doFinal(Base64.getDecoder().decode(encodedCiphertext)));
}
public static byte[] generateKey() throws Exception {
KeyGenerator keyGen = KeyGenerator.getInstance("AES");
keyGen.init(256);
SecretKey key = keyGen.generateKey();
return key.getEncoded();
}
}
Modes of Operation¶
ECB (Electronic Codebook) - NOT RECOMMENDED¶
Each block is encrypted independently. Identical plaintext blocks produce identical ciphertext blocks, leaking patterns:
CBC (Cipher Block Chaining)¶
Each plaintext block is XORed with the previous ciphertext block before encryption. Requires an initialization vector (IV):
$$C_i = E_K(P_i \oplus C_{i-1})$$
With $C_0 = IV$.
Security: Hides patterns, but errors propagate to next block.
GCM (Galois/Counter Mode) - RECOMMENDED¶
Provides both confidentiality and authentication: - Uses Counter mode for encryption - Provides authenticated encryption via Galois field multiplication - Most secure modern choice
Key Management¶
Key Derivation Functions (KDFs)¶
Convert passwords or low-entropy keys into cryptographic keys:
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.PBEKeySpec;
import java.security.SecureRandom;
public class KeyDerivation {
public static byte[] deriveKey(char[] password, byte[] salt) throws Exception {
SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256");
PBEKeySpec spec = new PBEKeySpec(password, salt, 65536, 256); // 65k iterations
return factory.generateSecret(spec).getEncoded();
}
}
Best Practices for Key Management¶
- Never hardcode keys in source code
- Use secure storage: HashiCorp Vault, AWS KMS, Azure Key Vault
- Rotate keys regularly
- Use different keys for different purposes (encryption vs signing)
- Implement access controls on key storage
Security Analysis¶
Strengths and Weaknesses¶
| Aspect | Assessment |
|---|---|
| Speed | Very fast, suitable for large data |
| Key Size | 128-256 bits (computationally secure) |
| Implementation | Well-vetted standards (AES) |
| Vulnerability | Requires secure key exchange |
Why AES is Secure¶
- Large Key Space: $2^{256}$ possible keys for AES-256
- Resistance to Known Attacks: No practical attacks faster than brute force
- Standardization: Extensive analysis by cryptographers worldwide
- Hardware Acceleration: Available on most modern processors
DES is Deprecated¶
The original Data Encryption Standard (DES) uses only 56-bit keys: - Key space: $2^{56} \approx 7.2 \times 10^{16}$ combinations - Broken since the 1990s - Never use DES for new applications
Comparison with Asymmetric Encryption¶
| Feature | Symmetric (AES) | Asymmetric (RSA/ECC) |
|---|---|---|
| Key Size | 128-256 bits | 2048-4096 bits |
| Speed | Very fast (~$10^3\times$ faster) | Slower |
| Use Case | Encrypting data | Key exchange, signatures |
| Key Exchange | Requires pre-shared secret | Built-in secure exchange |
Hybrid Encryption (TLS/SSL)¶
Modern protocols combine both approaches:
1. Client and Server perform Diffie-Hellman key exchange
2. Establish shared symmetric key
3. Use AES to encrypt the actual data transfer
4. Sign with RSA/ECC for authentication
This provides the efficiency of symmetric encryption with the secure key exchange of asymmetric cryptography.
References¶
- NIST FIPS 197: Advanced Encryption Standard (AES)
- Daemen, J., & Rijmen, V. (2002). The Design of Rijndael. Springer.
- Ferguson, N., Schneier, B., & Kohno, T. (2010). Cryptographic Engineering. MIT Press.
- Stinson, D. R. (2005). Cryptography: Theory and Practice. CRC Press.