Skip to content

AES+RSA hybrid encryption with WebSocket binary frames and Google Protobuf. Secure real-time communication library featuring digital signatures, encrypted key exchange, and efficient binary message serialization.

License

Notifications You must be signed in to change notification settings

streamnz/aes-rsa-java

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

AES + RSA Encryption Java Example

A comprehensive Java implementation demonstrating the combination of AES (Advanced Encryption Standard) and RSA encryption algorithms for secure data transmission and digital signatures.

Overview

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

Features

  • 🔐 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

Architecture

The project implements a hybrid encryption scheme:

  1. 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
  2. Server Side:

    • Decrypt AES key with RSA private key
    • Decrypt data with AES key
    • Verify signature with client's RSA public key

Project Structure

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

Dependencies

  • 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)

Quick Start

1. Clone the Repository

git clone <repository-url>
cd aes-rsa-java

2. Build the Project

mvn clean compile

3. Run the Example

mvn exec:java -Dexec.mainClass="com.wustrive.Main"

Usage Example

// 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...
}

Key Features

AES Encryption

  • Algorithm: AES-128
  • Mode: Default Java Cipher mode
  • Key Length: 128 bits (16 bytes)
  • Encoding: UTF-8

RSA Encryption

  • Key Size: 1024 bits
  • Algorithm: RSA
  • Padding: Default Java RSA padding
  • Signature: SHA1WithRSA

Security Considerations

  1. Key Management: Store private keys securely
  2. Random Generation: Uses SecureRandom for key generation
  3. Input Validation: Comprehensive parameter checking
  4. Error Handling: Proper exception handling and logging

API Reference

AES Class

  • encrypt(byte[] data, byte[] key): Encrypt data with AES
  • decrypt(byte[] data, byte[] key): Decrypt data with AES
  • encryptToBase64(String data, String key): Encrypt string to Base64
  • decryptFromBase64(String data, String key): Decrypt Base64 string

RSA Class

  • generateKeyPair(): Generate RSA key pair
  • encrypt(String data, String publicKey): Encrypt with RSA
  • decrypt(String data, String privateKey): Decrypt with RSA
  • sign(String content, String privateKey): Create digital signature
  • checkSign(String content, String sign, String publicKey): Verify signature

EncryUtil Class

  • handleRSA(TreeMap<String, Object> map, String privateKey): Generate signature
  • checkDecryptAndSign(...): Complete decryption and verification process

Testing

Run the test suite:

mvn test

License

This project is licensed under the MIT License - see the LICENSE file for details.

Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Add tests if applicable
  5. Submit a pull request

Security Notice

⚠️ Important: This is an educational example. For production use:

  • 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

Author

Created by wustrive - demonstrating AES+RSA hybrid encryption in Java.

About

AES+RSA hybrid encryption with WebSocket binary frames and Google Protobuf. Secure real-time communication library featuring digital signatures, encrypted key exchange, and efficient binary message serialization.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •  

Languages