A comprehensive Java application demonstrating various cryptographic concepts including symmetric encryption, key derivation, and Diffie-Hellman key exchange.
- Enter any message (e.g., "Wi-Fi password is: 12345")
- Use a passphrase to encrypt the message
- Get a Base64-encoded encrypted message to share
- Uses AES-256 in GCM (Galois/Counter Mode) for authenticated encryption
- Derives encryption keys from passphrases using SHA-256
- Includes initialization vectors (IV) for security
- Provides authentication to detect tampering
- Paste an encrypted message
- Enter the correct passphrase
- Retrieve the original message
- Simulates secure key exchange between two parties (Alice and Bob)
- Generates public/private key pairs
- Derives shared secrets without transmitting private keys
- Demonstrates end-to-end encryption using the shared secret
- Password strength analysis
- Secure hashing (SHA-256, SHA-512)
- Timing attack resistance
- Secure random number generation
- Java 8 or higher
- No external dependencies required (uses built-in Java cryptography)
- Compile the application:
javac -d . src/main/java/com/example/crypto/*.java
- Run the interactive application:
java com.example.crypto.CryptographyApp
- Run the automated test suite:
java com.example.crypto.CryptographyTest
When you run CryptographyApp
, you'll see a menu:
🔐 Basic Cryptography Demo Application
=====================================
📋 Choose an option:
1. 🔒 Encrypt a note
2. 🔓 Decrypt a note
3. 🤝 Demonstrate Key Exchange (Diffie-Hellman)
4. 🚪 Exit
-
Encrypt a message:
- Choose option 1
- Enter: "Meeting password is: secret123"
- Enter passphrase: "myPassphrase"
- Get encrypted message:
AbCdEf123...
(Base64 encoded)
-
Share the encrypted message:
- Send the Base64 string to the recipient
- Share the passphrase through a separate secure channel
-
Decrypt the message:
- Recipient chooses option 2
- Pastes the encrypted message
- Enters the same passphrase
- Gets the original message back
The key exchange demonstration shows:
- Alice and Bob generate key pairs
- They exchange public keys
- Both derive the same shared secret
- They use the shared secret for encryption
- Algorithm: AES-256-GCM (Advanced Encryption Standard with Galois/Counter Mode)
- Key Size: 256 bits
- Authentication: Built-in with GCM mode
- IV: 96-bit random initialization vector per encryption
- Key Derivation: SHA-256 hash of passphrase
- Algorithm: Diffie-Hellman
- Key Size: 2048 bits
- Security: Provides perfect forward secrecy
- ✅ Cryptographically secure random number generation
- ✅ Constant-time comparisons to prevent timing attacks
- ✅ Proper IV/nonce handling
- ✅ Authenticated encryption (GCM mode)
- ✅ No hardcoded keys or secrets
src/main/java/com/example/crypto/
├── CryptographyApp.java # Main interactive application
├── SymmetricCrypto.java # AES encryption/decryption
├── KeyExchange.java # Diffie-Hellman implementation
├── CryptographyDemo.java # Security concept demonstrations
└── CryptographyTest.java # Automated test suite
This application demonstrates:
- Symmetric Cryptography: How AES works with proper key derivation
- Key Exchange: How two parties can establish a shared secret
- Security Principles: Timing attacks, password strength, secure random generation
- Real-world Applications: Secure messaging, password managers, VPN protocols
This application is designed for learning cryptographic concepts. For production use:
- Use established libraries like Bouncy Castle
- Implement proper key derivation functions (PBKDF2, scrypt, Argon2)
- Add salt to password hashing
- Use certificate-based authentication for key exchange
- Implement proper error handling and logging
- ❌ ECB mode (uses GCM instead)
- ❌ Fixed IVs (generates random IV each time)
- ❌ Unauthenticated encryption (GCM provides authentication)
- ❌ Timing attacks (constant-time comparisons where needed)
You can extend this application by:
- Adding RSA encryption for asymmetric cryptography
- Implementing digital signatures
- Adding file encryption capabilities
- Creating a simple chat application
- Adding elliptic curve cryptography (ECC)
- Java Cryptography Architecture (JCA)
- NIST Cryptographic Standards
- OWASP Cryptographic Storage Cheat Sheet
This project is for educational purposes. Feel free to use and modify for learning about cryptography.