英文:
How to calculate keysize of KeyPairGenerator in Java?
问题
我想在Java中使用KeyPairGenerator生成公钥和私钥,对我来说在Cipher中使用"RSA/ECB/PKCS1Padding"很重要,以后会用到。
在阅读了这个回答之后,我应该使用这个公式:
最大字节数 = 密钥长度(以位为单位)/ 8 - 11
现在:
512字节 = 4096位
1024字节 = 8192位
如果我使用8192/8-11,它运行得很好,但是当我使用4096/8-11时,会出现错误:"无效的密钥大小"。为什么我不能使用512字节?这是KeyPairGenerator的限制吗?
KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
kpg.initialize(4096/8-11);
英文:
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);
答案1
得分: 1
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密钥生成");
System.out.println("Java版本:" + getJavaVersion());
System.out.println("操作系统:" + System.getProperty("os.name"));
System.out.println("RSA密钥生成");
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("私钥:" + rsaPrivateKey.getAlgorithm()
+ " 格式:" + rsaPrivateKey.getFormat()
+ " 模数位数:" + rsaPrivateKey.getModulus().bitLength());
System.out.println("公钥:" + rsaPublicKey.getAlgorithm()
+ " 格式:" + rsaPublicKey.getFormat()
+ " 模数位数:" + 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 + " 主版本号:" + major + " 次版本号:" + minor + " 更新号:" + update + " 构建号:" + build;
}
}
英文:
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;
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论