A simple Java implementation of RSA encryption and decryption using the Java Cryptography Architecture (JCA).
This project provides a straightforward RSA encryption/decryption utility that demonstrates:
- RSA key pair generation
- Encrypting plaintext using a public key
- Decrypting ciphertext using a private key
- Base64 encoding/decoding for ciphertext handling
- Key Generation: Generates 1024-bit RSA key pairs using
SecureRandom - Encryption: Encrypts byte arrays using a public key and returns Base64-encoded ciphertext
- Decryption: Decrypts Base64-encoded ciphertext using a private key
- Java 8 or higher
Compile and run the RSA.java file:
cd src
javac RSA.java
java RSAimport java.security.KeyPair;
// Generate a key pair
KeyPair keys = RSA.generateRSAKeyPair();
// Encrypt a message
String encrypted = RSA.encrypt(keys.getPublic(), "Hello, World!".getBytes());
// Decrypt the message
String decrypted = RSA.decrypt(keys.getPrivate(), encrypted);Generates a new 1024-bit RSA key pair.
Returns: KeyPair - The generated public/private key pair
Encrypts plaintext bytes using the provided public key.
Parameters:
publicKey- The RSA public key for encryptionplainText- The bytes to encrypt
Returns: String - Base64-encoded ciphertext
Decrypts Base64-encoded ciphertext using the provided private key.
Parameters:
privateKey- The RSA private key for decryptioncipherText- Base64-encoded ciphertext
Returns: String - The decrypted plaintext
This project is open source and available for use and modification.