Generate Encryption Keys in Java - KeyGenerator & RSA Examples
Complete code tutorial with examples and best practices
[ Code Example - Quick Summary ]
Language: Java
What: Generate AES and RSA encryption keys in Java using KeyGenerator and KeyPairGenerator from the javax.crypto package.
Try it: Use our interactive Encryption-keys generator or integrate this code into your Java application.
Generate AES and RSA encryption keys in Java using KeyGenerator and KeyPairGenerator from the javax.crypto package. Looking for other languages? Check our code examples in PHP , JavaScript , Python , C# , C++ , Ruby and Go or use our interactive web generator.
Java Code Example
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import java.security.*;
import java.util.Base64;
public class EncryptionKeyGenerator {
public static void main(String[] args) throws Exception {
// AES-256 Key Generation
KeyGenerator aesKeyGen = KeyGenerator.getInstance("AES");
aesKeyGen.init(256, new SecureRandom());
SecretKey aesKey = aesKeyGen.generateKey();
byte[] aesKeyBytes = aesKey.getEncoded();
String aesKeyHex = bytesToHex(aesKeyBytes);
String aesKeyBase64 = Base64.getEncoder().encodeToString(aesKeyBytes);
System.out.println("AES-256 Key (Hex): " + aesKeyHex);
System.out.println("AES-256 Key (Base64): " + aesKeyBase64);
// AES-128 Key
aesKeyGen.init(128);
SecretKey aes128Key = aesKeyGen.generateKey();
System.out.println("AES-128 Key (Hex): " + bytesToHex(aes128Key.getEncoded()));
// RSA Key Pair Generation (2048-bit)
KeyPairGenerator rsaKeyGen = KeyPairGenerator.getInstance("RSA");
rsaKeyGen.initialize(2048, new SecureRandom());
KeyPair keyPair = rsaKeyGen.generateKeyPair();
PrivateKey privateKey = keyPair.getPrivate();
PublicKey publicKey = keyPair.getPublic();
System.out.println("Private Key (Base64): " +
Base64.getEncoder().encodeToString(privateKey.getEncoded()));
System.out.println("Public Key (Base64): " +
Base64.getEncoder().encodeToString(publicKey.getEncoded()));
}
private static String bytesToHex(byte[] bytes) {
StringBuilder hex = new StringBuilder();
for (byte b : bytes) {
hex.append(String.format("%02x", b));
}
return hex.toString();
}
}
[EXPLANATION]
Java's KeyGenerator class generates symmetric keys (AES). Call getInstance("AES") to get an AES key generator, then init() with key size (128, 192, or 256 bits) and a SecureRandom instance for cryptographic randomness. generateKey() returns a SecretKey object, and getEncoded() retrieves the raw bytes. For asymmetric encryption, KeyPairGenerator.getInstance("RSA") creates RSA key pairs. initialize() sets the modulus length (2048 or 4096 bits), and generateKeyPair() returns a KeyPair containing private and public keys. Base64 encoding is standard for key storage and transmission.
Expected Output
AES-256 Key (Hex): a7d2e5c1b8f4a9d6c3e7b2f5a8c1d4e7b9f2a5c8d1e4b7f3a6d2e5c1b8f4a9d6 AES-256 Key (Base64): p9LlwbjGnWw+ey9ajB1Oe58qXI0eS389ptLlwbj0qtY= AES-128 Key (Hex): c2f5a8d1e4b7f3a9d6c2e5b8f1a4c7d2 Private Key (Base64): MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDN... Public Key (Base64): MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAzf...
Common Use Cases
- Encrypt sensitive data in enterprise applications
- Secure file storage with AES encryption
- Digital signatures for document verification
- TLS/SSL certificate generation
- Encrypt database connections
Important Notes
-
Use
SecureRandomfor cryptographic key generation - AES-256 may require Java Cryptography Extension (JCE) Unlimited Strength
- For PEM format keys, use Bouncy Castle library
- Store keys in Java KeyStore (JKS) or PKCS12 format
-
Consider using
SecretKeyFactoryfor key derivation (PBKDF2)
Try Our Interactive Generator
Don't want to write code? Use our free web-based Encryption-keys generator with instant results.
TRY ENCRYPTION-KEYS GENERATOR →Other Programming Languages
View Encryption-keys generation code examples in PHP
View Encryption-keys generation code examples in JavaScript
View Encryption-keys generation code examples in Python
View Encryption-keys generation code examples in C#
View Encryption-keys generation code examples in C++
View Encryption-keys generation code examples in Ruby
View Encryption-keys generation code examples in Go