Reputation: 5645
I want to make public and private keys with KeyPairGenerator in Java it's important to me to use "RSA/ECB/PKCS1Padding" in Cipher, later.
After reading this answer I'm should use this formulate:
maximum bytes = key length in bits / 8 - 11
now:
512 Byte = 4096 bit
1024 Byte = 8192 bit
If I use 8192/8-11 it works well but when I use 4096/8-11 get this error: "Invalid key sizes". Why I can not use 512Byte? Is this a limitation of KeyPairGenerator or what?
KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
kpg.initialize(4096/8-11);
Upvotes: 0
Views: 1708
Reputation: 6414
I never heard about a limited key length in these days but you never know. Below you find an example that creates a RSA KeyPair of 4096 bit and gives out some details about the Java version and operation system in use.
I'm generating a RSAPrivate/PublicKey and not a Private/PublicKey KeyPair to get the bitlength but in your case you would generate the latter.
This is the result when running the program on my Online-compiler, run the example here: https://paiza.io/projects/RGfFI7g3a2WyjjdtU8vsAA:
RSA Key Generation
Java version: main: 13 major: 0 minor: 1+9 update: build:
Operation system: Linux
RSA key generation
PrivateKey: RSA format: PKCS#8 modulus: 4096
PublicKey: RSA format: X.509 modulus: 4096
Here is the code:
import java.security.*;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
public class Main {
public static void main(String[] args) throws NoSuchAlgorithmException {
System.out.println("RSA Key Generation");
System.out.println("Java version: " + getJavaVersion());
System.out.println("Operation system: " + System.getProperty("os.name"));
System.out.println("RSA key generation");
KeyPairGenerator rsaGenerator = KeyPairGenerator.getInstance("RSA");
SecureRandom random = new SecureRandom();
rsaGenerator.initialize(4096, random);
KeyPair rsaKeyPair = rsaGenerator.generateKeyPair();
RSAPrivateKey rsaPrivateKey = (RSAPrivateKey) rsaKeyPair.getPrivate();
RSAPublicKey rsaPublicKey = (RSAPublicKey) rsaKeyPair.getPublic();
System.out.println("PrivateKey: " + rsaPrivateKey.getAlgorithm()
+ " format: " + rsaPrivateKey.getFormat()
+ " modulus: " + rsaPrivateKey.getModulus().bitLength());
System.out.println("PublicKey: " + rsaPublicKey.getAlgorithm()
+ " format: " + rsaPublicKey.getFormat()
+ " modulus: " + rsaPublicKey.getModulus().bitLength());
}
public static String getJavaVersion() {
String[] javaVersionElements = System.getProperty("java.runtime.version").split("\\.|_|-b");
String main = "", major = "", minor = "", update = "", build = "";
int elementsSize = javaVersionElements.length;
if (elementsSize > 0) {main = javaVersionElements[0];}
if (elementsSize > 1) {major = javaVersionElements[1];}
if (elementsSize > 2) {minor = javaVersionElements[2];}
if (elementsSize > 3) {update = javaVersionElements[3];}
if (elementsSize > 4) {build = javaVersionElements[4];}
return "main: " + main + " major: " + major + " minor: " + minor + " update: " + update + " build: " + build;
}
}
Upvotes: 1