A comprehensive Java implementation demonstrating the combination of AES (Advanced Encryption Standard) and RSA encryption algorithms for secure data transmission and digital signatures.
This project provides a complete example of how to combine AES and RSA encryption for secure communication between client and server applications. The implementation includes:
- AES Encryption: Fast symmetric encryption for data payload
- RSA Encryption: Asymmetric encryption for AES key exchange and digital signatures
- Digital Signatures: RSA-based signature generation and verification
- Base64 Encoding: Safe data transmission format
- 🔐 AES-128 Encryption: Fast symmetric encryption for sensitive data
- 🔑 RSA Key Management: Public/private key pair generation and management
- ✍️ Digital Signatures: RSA-based signature generation and verification
- 🔄 Hybrid Encryption: Combines AES speed with RSA security
- 📦 Base64 Encoding: Safe data transmission over text-based protocols
- 🧪 Complete Example: Ready-to-run client-server simulation
The project implements a hybrid encryption scheme:
-
Client Side:
- Generate random AES key
- Encrypt data with AES
- Sign data with RSA private key
- Encrypt AES key with server's RSA public key
-
Server Side:
- Decrypt AES key with RSA private key
- Decrypt data with AES key
- Verify signature with client's RSA public key
src/main/java/com/wustrive/aesrsa/util/
├── AES.java # AES encryption/decryption utilities
├── RSA.java # RSA encryption/decryption and signatures
├── EncryUtil.java # High-level encryption utilities
├── Base64.java # Base64 encoding/decoding
├── RandomUtil.java # Random key generation
├── Digest.java # Hash and HMAC utilities
├── CheckUtils.java # Input validation utilities
├── ConvertUtils.java # Data conversion utilities
└── ConfigureEncryptAndDecrypt.java # Configuration constants
src/main/java/com/wustrive/
└── Main.java # Example client-server implementation
- Java 8+
- JUnit 3.8.1 (for testing)
- Log4j 1.2.16 (for logging)
- Apache Commons Lang 2.5 (utility functions)
- FastJSON 1.1.15 (JSON processing)
git clone <repository-url>
cd aes-rsa-javamvn clean compilemvn exec:java -Dexec.mainClass="com.wustrive.Main"// Client side - encrypt and sign data
TreeMap<String, Object> params = new TreeMap<>();
params.put("userid", "152255855");
params.put("phone", "18965621420");
// Generate RSA signature
String sign = EncryUtil.handleRSA(params, clientPrivateKey);
params.put("sign", sign);
// Convert to JSON and encrypt with AES
String info = JSON.toJSONString(params);
String aesKey = RandomUtil.getRandom(16);
String encryptedData = AES.encryptToBase64(info, aesKey);
// Encrypt AES key with server's public key
String encryptedKey = RSA.encrypt(aesKey, serverPublicKey);
// Server side - decrypt and verify
boolean isValid = EncryUtil.checkDecryptAndSign(
encryptedData, encryptedKey, clientPublicKey, serverPrivateKey);
if (isValid) {
// Decrypt and process data
String aesKey = RSA.decrypt(encryptedKey, serverPrivateKey);
String decryptedData = AES.decryptFromBase64(encryptedData, aesKey);
// Process decrypted data...
}- Algorithm: AES-128
- Mode: Default Java Cipher mode
- Key Length: 128 bits (16 bytes)
- Encoding: UTF-8
- Key Size: 1024 bits
- Algorithm: RSA
- Padding: Default Java RSA padding
- Signature: SHA1WithRSA
- Key Management: Store private keys securely
- Random Generation: Uses
SecureRandomfor key generation - Input Validation: Comprehensive parameter checking
- Error Handling: Proper exception handling and logging
encrypt(byte[] data, byte[] key): Encrypt data with AESdecrypt(byte[] data, byte[] key): Decrypt data with AESencryptToBase64(String data, String key): Encrypt string to Base64decryptFromBase64(String data, String key): Decrypt Base64 string
generateKeyPair(): Generate RSA key pairencrypt(String data, String publicKey): Encrypt with RSAdecrypt(String data, String privateKey): Decrypt with RSAsign(String content, String privateKey): Create digital signaturecheckSign(String content, String sign, String publicKey): Verify signature
handleRSA(TreeMap<String, Object> map, String privateKey): Generate signaturecheckDecryptAndSign(...): Complete decryption and verification process
Run the test suite:
mvn testThis project is licensed under the MIT License - see the LICENSE file for details.
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests if applicable
- Submit a pull request
- Use longer RSA key sizes (2048+ bits)
- Implement proper key rotation
- Use secure random number generation
- Follow security best practices for key storage
- Consider using established cryptographic libraries
Created by wustrive - demonstrating AES+RSA hybrid encryption in Java.