Lab 06: Cryptography — JCA/JCE

Objective

Use Java's cryptography architecture (javax.crypto, java.security) to implement SHA-256 hashing, HMAC-SHA256 message authentication, AES-256-GCM authenticated encryption/decryption, RSA-2048 digital signing and verification, and secure random token generation.

Background

The Java Cryptography Architecture (JCA) provides a provider-based, algorithm-agnostic API. You never implement crypto algorithms — you call MessageDigest.getInstance("SHA-256"), Cipher.getInstance("AES/GCM/NoPadding"), etc., and the provider (Sun's JCE by default) handles the implementation. This makes algorithm migration easy and correct.

Time

30 minutes

Prerequisites

  • Practitioner Labs (any)

Tools

  • Docker: zchencow/innozverse-java:latest


Lab Instructions

Steps 1–8: SHA-256, HMAC-SHA256, AES-GCM encrypt/decrypt, RSA sign/verify, tamper detection, secure token, password hashing, Capstone audit log

💡 AES-GCM provides both confidentiality AND integrity. The 16-byte "GCM tag" appended to the ciphertext is a MAC over the ciphertext — if anyone modifies even a single bit, decryption throws AEADBadTagException. This is why AES-GCM is preferred over AES-CBC: CBC encrypts but doesn't authenticate, so a bit-flip attack can modify ciphertext without detection. Never use CBC for new code.

📸 Verified Output:


Summary

Use case
Algorithm
API

Integrity check

SHA-256

MessageDigest.getInstance("SHA-256")

Message auth

HMAC-SHA256

Mac.getInstance("HmacSHA256")

Encryption

AES-256-GCM

Cipher.getInstance("AES/GCM/NoPadding")

Digital signing

RSA-2048

Signature.getInstance("SHA256withRSA")

Secure token

CSPRNG

new SecureRandom() + Base64

Further Reading

Last updated