This project demonstrates secure cryptographic practices using AES-256-GCM for symmetric encryption and ECC (Elliptic Curve Cryptography) for key exchange. It consists of two programs:
- crypto_core.cpp - Core encryption concepts demonstration
- secure_transfer.cpp - Network-based secure data transfer simulation
- Type: Symmetric encryption (same key for encryption/decryption)
- Key Size: 256 bits (extremely secure)
- Mode: GCM (Galois/Counter Mode)
- Provides encryption AND authentication
- Detects if data has been tampered with
- Prevents replay attacks using unique IVs
- Components:
- Key: Secret 256-bit key (must be kept secure)
- IV (Initialization Vector): Random 96-bit value (unique per encryption)
- Tag: 128-bit authentication code (proves data integrity)
Why AES?
- Industry standard, used by governments worldwide
- Fast in both hardware and software
- No known practical attacks on AES-256
- Type: Asymmetric encryption (public/private key pairs)
- Curve: P-256 (also known as secp256r1 or prime256v1)
- NIST standard curve
- 256-bit security level β 3072-bit RSA
- Advantages over RSA:
- Much smaller keys (256 bits vs 3072 bits)
- Faster computation
- Less bandwidth required
- Purpose: Secure key exchange over insecure channel
- How it works:
- Alice generates key pair (privateA, publicA)
- Bob generates key pair (privateB, publicB)
- They exchange public keys (safe to send openly!)
- Alice computes: sharedSecret = ECDH(privateA, publicB)
- Bob computes: sharedSecret = ECDH(privateB, publicA)
- Both get the SAME secret without ever transmitting it!
- Magic: Even if an attacker intercepts both public keys, they cannot derive the shared secret
- Linux (Ubuntu 20.04+, Debian 10+, or similar)
- g++ compiler with C++11 support
- OpenSSL development libraries (version 1.1.0 or higher)
sudo apt-get update
sudo apt-get install build-essential libssl-devsudo dnf install gcc-c++ openssl-devel
# Or for older systems:
sudo yum install gcc-c++ openssl-develsudo pacman -S base-devel openssl# Check g++ version (should be 4.8.1 or higher)
g++ --version
# Check OpenSSL version (should be 1.1.0 or higher)
openssl version
# Verify OpenSSL development files
pkg-config --modversion opensslCreate a Makefile:
# Compiler settings
CXX = g++
CXXFLAGS = -std=c++11 -Wall -Wextra -O3 -pthread
LDFLAGS = -lssl -lcrypto
# Targets
all: crypto_core secure_transfer
crypto_core: crypto_core.cpp
$(CXX) $(CXXFLAGS) -o crypto_core crypto_core.cpp $(LDFLAGS)
secure_transfer: secure_transfer.cpp
$(CXX) $(CXXFLAGS) -o secure_transfer secure_transfer.cpp $(LDFLAGS)
clean:
rm -f crypto_core secure_transfer
rm -f /tmp/secure_transfer.sock
.PHONY: all cleanThen compile:
makeCompile each program individually:
# Compile crypto_core
g++ -std=c++11 -Wall -Wextra -O3 \
-o crypto_core crypto_core.cpp \
-lssl -lcrypto
# Compile secure_transfer
g++ -std=c++11 -Wall -Wextra -O3 -pthread \
-o secure_transfer secure_transfer.cpp \
-lssl -lcrypto-std=c++11: Use C++11 standard (required for modern features)-Wall -Wextra: Enable all warnings (good practice)-O3: Optimize for maximum performance-pthread: Enable POSIX threads support (for secure_transfer)-lssl -lcrypto: Link against OpenSSL libraries
This program demonstrates the core encryption concepts in a single process.
./crypto_coreWhat it does:
- Generates a random AES-256 key
- Encrypts a message using AES-256-GCM
- Generates ECC key pairs for Alice and Bob
- Exchanges public keys
- Derives shared secret using ECDH
- Decrypts the message
- Verifies message integrity
Expected Output:
======================================
CRYPTOGRAPHY DEMONSTRATION
AES-256-GCM + ECC (P-256)
======================================
[STEP 1] Alice generates AES key for data encryption
=== AES KEY GENERATION ===
Generated 256-bit AES key
AES Key: 3f9a8b7c2d1e0f4a5b6c7d8e9f0a1b2c3d4e5f6a7b8c9d0e1f2a3b4c5d6e7f8
...
[β] Message integrity verified: YES β
This program simulates secure network communication between two parties.
You need TWO terminal windows:
./secure_transfer receive./secure_transfer send "Your secret message here"Example:
# Terminal 1
./secure_transfer receive
# Terminal 2
./secure_transfer send "Attack at dawn! π"What happens:
- Receiver starts listening on Unix socket
- Sender connects to receiver
- They exchange ECC public keys
- Both derive the same AES session key via ECDH
- Sender encrypts message with AES-256-GCM
- Sender transmits encrypted data
- Receiver decrypts and verifies message
-
AES Key - 256-bit (32 bytes) hex string
AES Key: 3f9a8b7c2d1e0f4a5b6c7d8e9f0a1b2c... -
IV (Initialization Vector) - 96-bit (12 bytes) random value
IV: a1b2c3d4e5f6a7b8c9d0e1f2 -
Authentication Tag - 128-bit (16 bytes) integrity proof
Tag: 4a5b6c7d8e9f0a1b2c3d4e5f6a7b8c9d -
Shared Secret - Derived via ECDH (32 bytes)
Shared Secret: 7f8e9d0a1b2c3d4e5f6a7b8c9d0e1f2a...
[*]- Operation in progress[β]- Success[β]- Failure
- Uses RAII (Resource Acquisition Is Initialization) for automatic cleanup
- Smart wrappers prevent OpenSSL memory leaks
- Automatic cleanup of sockets and file descriptors
- Authenticated Encryption: GCM mode prevents tampering
- Random IVs: Each encryption uses unique IV
- Forward Secrecy: New ECDH keys for each session (in secure_transfer)
- Length-Prefixed Protocol: Prevents injection attacks
- -O3 Optimization: Compiler optimizations for speed
- Buffer Reuse: Minimize allocations
- Efficient Crypto: Hardware-accelerated AES when available
./crypto_core
# Verify "Message integrity verified: YES β" appears# Terminal 1
./secure_transfer receive
# Terminal 2
./secure_transfer send "Test message 123"
# Verify both show "Secure transfer complete!"Modify the encrypted data to see authentication failure:
- The program will detect tampering via GCM authentication tag
- Uses industry-standard algorithms (AES-256, P-256)
- Authenticated encryption (GCM mode)
- Cryptographically secure randomness (RAND_bytes)
- Forward secrecy via ECDH
- No hardcoded keys or secrets
For production use, you should also implement:
-
Key Derivation Function (KDF)
// Use HKDF to derive AES key from ECDH secret // Don't use raw ECDH output directly
-
Certificate Validation
- Use X.509 certificates for identity verification
- Implement proper PKI (Public Key Infrastructure)
-
Perfect Forward Secrecy
- Generate new ECDH keys for each session
- Don't reuse long-term keys for encryption
-
Additional Authentication
- Add HMAC for extra layer
- Consider using AAD (Additional Authenticated Data) in GCM
-
Key Storage
- Use hardware security modules (HSM) for keys
- Implement secure key rotation policies
-
Network Security
- Use TLS 1.3 for production network communication
- Implement proper timeout and retry logic
- Add rate limiting to prevent DoS
-
Error Handling
- Don't leak information in error messages
- Log securely without exposing sensitive data
Solution: Ensure OpenSSL is properly installed
sudo apt-get install --reinstall libssl-devSolution: Make sure receiver is started first and socket path is accessible
# Check if socket exists
ls -la /tmp/secure_transfer.sock
# Clean up old socket
rm /tmp/secure_transfer.sockSolution: Specify OpenSSL path explicitly
g++ -std=c++11 -I/usr/include/openssl \
-o crypto_core crypto_core.cpp \
-L/usr/lib/x86_64-linux-gnu -lssl -lcryptoSolution: This is expected if data is modified! The GCM tag detects tampering.
- "Cryptography Engineering" by Ferguson, Schneier, and Kohno
- "Applied Cryptography" by Bruce Schneier
- "Serious Cryptography" by Jean-Philippe Aumasson
- FIPS 197 - AES Standard
- FIPS 186-4 - Digital Signature Standard (includes ECC)
- RFC 8446 - TLS 1.3
βββ CryptoUtils class - Utility functions (hex printing, error handling)
βββ AESCrypto class - AES-256-GCM encryption/decryption
βββ ECCCrypto class - ECC key generation and ECDH
βββ main() - Demonstration flow
βββ Utils namespace - Socket I/O and utilities
βββ AESCrypto class - AES-256-GCM encryption/decryption
βββ ECCCrypto class - ECC key generation and ECDH
βββ SecureReceiver class - Server-side logic
βββ SecureSender class - Client-side logic
βββ main() - Mode selection and execution
This code is for educational purposes only. While it uses production-grade cryptography libraries and follows best practices, additional security measures are needed for production deployment.
Important:
- Never roll your own crypto for production systems
- Use established libraries like OpenSSL, libsodium, or BoringSSL
- Get security audits for production cryptographic code
- Stay updated with latest security advisories
Found a bug or have a suggestion? This is educational code, but improvements are always welcome!
If you encounter issues:
- Check the "Common Issues" section above
- Verify OpenSSL installation:
openssl version - Ensure g++ supports C++11:
g++ --version - Check system logs for detailed errors