Generate API Keys in Java
Complete code tutorial with examples and best practices
[ Code Example - Quick Summary ]
Language: Java
What: Generate cryptographically secure API keys in Java using <code>SecureRandom</code> for enterprise applications. Suitable for Spring Boot, microservices, and RESTful APIs.
Try it: Use our interactive Api-keys generator or integrate this code into your Java application.
Generate cryptographically secure API keys in Java using SecureRandom for enterprise applications. Suitable for Spring Boot, microservices, and RESTful APIs.
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 java.security.SecureRandom;
import java.util.Base64;
public class ApiKeyGenerator {
private static final SecureRandom random = new SecureRandom();
public static String generateApiKey(int length) {
byte[] bytes = new byte[length];
random.nextBytes(bytes);
// Convert to hexadecimal
StringBuilder sb = new StringBuilder();
for (byte b : bytes) {
sb.append(String.format("%02x", b));
}
return sb.toString();
}
public static String generateApiKeyBase64(int length) {
byte[] bytes = new byte[length];
random.nextBytes(bytes);
// Base64 URL-safe encoding
return Base64.getUrlEncoder()
.withoutPadding()
.encodeToString(bytes);
}
public static void main(String[] args) {
// Generate 256-bit (32 byte) API key
String apiKey = generateApiKey(32);
System.out.println("Hex: " + apiKey);
// Output: "a7f3e9c2d8b4f1a6e5c9d7b3f8a2e6c4d9b5f7a3e8c6d2b9f4a7e1c5d8b3f6a9"
String apiKeyBase64 = generateApiKeyBase64(32);
System.out.println("Base64: " + apiKeyBase64);
// Output: "p3_pwr2L8RpuXJbT-IouaztX86jG0rmU9KfhxdizNpo"
}
}
[EXPLANATION]
Java's SecureRandom class provides cryptographically strong random number generation. The implementation uses Base64.getUrlEncoder().withoutPadding() for URL-safe encoding without padding characters. This approach is thread-safe and suitable for high-concurrency enterprise applications.
Expected Output
Hexadecimal: a7f3e9c2d8b4f1a6e5c9d7b3f8a2e6c4d9b5f7a3e8c6d2b9f4a7e1c5d8b3f6a9 Base64 URL-safe: p3_pwr2L8RpuXJbT-IouaztX86jG0rmU9KfhxdizNpo With prefix: sk-live-p3_pwr2L8RpuXJbT-IouaztX86jG0rmU9KfhxdizNpo
Common Use Cases
- Spring Boot REST API authentication
- Microservices service-to-service auth
- Android app backend communication
- Enterprise API gateway keys
- OAuth 2.0 client secrets
Important Notes
-
Reuse
SecureRandominstance for performance -
Base64.getUrlEncoder()creates URL-safe encoding -
Use
withoutPadding()to remove trailing = signs - Consider using UUID for shorter keys (128 bits)
- Hash API keys with BCrypt before database storage
Try Our Interactive Generator
Don't want to write code? Use our free web-based Api-keys generator with instant results.
TRY API-KEYS GENERATOR →Other Programming Languages
View Api-keys generation code examples in PHP
View Api-keys generation code examples in JavaScript
View Api-keys generation code examples in Python
View Api-keys generation code examples in C#
View Api-keys generation code examples in C++
View Api-keys generation code examples in Ruby
View Api-keys generation code examples in Go