如何从java中的pfx文件/pem文件获取RSA公钥的指数和模数值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20897065/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 05:05:46  来源:igfitidea点击:

How to get exponent and modulus value of RSA public key from pfx file/pem file in java

javasecurityopensslkeystorepfx

提问by Ajitha Robert

I want to extract information about RSA Public Key from the pfx file using java.

我想使用 java 从 pfx 文件中提取有关 RSA 公钥的信息。

I have a pfx file and converted to x509 Pem file. From pem file, Using the below command in terminal:

我有一个 pfx 文件并转换为 x509 Pem 文件。从 pem 文件,在终端中使用以下命令:

openssl x509 -in file.pem -text

I am able to view the public key exponent and modulus value

我可以查看公钥指数和模数值



Subject Public Key Info:

主题公钥信息:

Public Key Algorithm: rsaEncryption
    Public-Key: (2048 bit)
    Modulus:
        00:da:7c:e0:3e:c4:62:8d:ce:29:04:2f:93:78:7c:
        :
         6a:e7:c9:7c:8b:6f:09:5c:75:5f:8c:5e:9c:6a:b9:
        7:32:90: a4:4b
    Exponent: 65537 (0x10001)


How to extract the above information in java?

java中如何提取上述信息?

Input:pfxfile and password

输入:pfxfile 和密码

Output:public key exponent and modulus value.

输出:公钥指数和模数值。

I m using the below code to extract the public key exponent and modulus, but i am not getting the value that is extracted using openssl. I doubt whether java.security.cert.Certificate uses some other DER format??

我使用下面的代码来提取公钥指数和模数,但我没有得到使用 openssl 提取的值。我怀疑 java.security.cert.Certificate 是否使用其他 DER 格式??

What is the java equivalent of openssl?

什么是openssl的java等价物?

Code:

代码:

KeyStore ks = KeyStore.getInstance("pkcs12");
ks.load(new FileInputStream(file), password.toCharArray());
Enumeration enumeration = ks.aliases();
while (enumeration.hasMoreElements()) {
    String alias = (String) enumeration.nextElement();
    java.security.cert.Certificate certificate = ks.getCertificate(alias);
    PublicKey publickey = certificate.getPublicKey();
}

采纳答案by erickson

You need to downcast to the transparent java.security.interfaces.RSAPublicKeytype. Then you can access the modulus and public exponents.

您需要向下转换为透明java.security.interfaces.RSAPublicKey类型。然后您可以访问模数和公共指数。

KeyStore ks = KeyStore.getInstance("pkcs12");
ks.load(new FileInputStream(file), password.toCharArray());
Enumeration<String> enumeration = ks.aliases();
while (enumeration.hasMoreElements()) {
  String alias = enumeration.nextElement();
  Certificate certificate = ks.getCertificate(alias);
  RSAPublicKey pub = (RSAPublicKey) certificate.getPublicKey();
  System.out.println(pub.getModulus().toString(16));
  System.out.println(pub.getPublicExponent().toString(16));
}

回答by user728785

I'm adding this answer as I recently tried to get the modulus and exponent from a raw public key string using java (also see https://crypto.stackexchange.com/questions/18031/how-to-find-modulus-from-a-rsa-public-key, from which the following public key was obtained) and thought that others may find the following code helpful:

我正在添加这个答案,因为我最近尝试使用 java 从原始公钥字符串中获取模数和指数(另请参阅https://crypto.stackexchange.com/questions/18031/how-to-find-modulus-from -a-rsa-public-key,从中获得以下公钥)并认为其他人可能会发现以下代码有用:

String publicKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCqGKukO1De7zhZj6+H0qtjTkVxwTCpvKe4eCZ0FPqri0cb2JZfXJ/DgYSF6vUpwmJG8wVQZKjeGcjDOL5UlsuusFncCzWBQ7RKNUSesmQRMSGkVb1/3j+skZ6UtW+5u09lHNsj6tQ51s1SPrCBkedbNf0Tp0GbMJDyR4e9T04ZZwIDAQAB";

try {
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        byte[] keyBytes = Base64.getDecoder().decode(publicKey.getBytes("UTF-8"));
        X509EncodedKeySpec spec = new X509EncodedKeySpec(keyBytes);
        PublicKey fileGeneratedPublicKey = keyFactory.generatePublic(spec);
        RSAPublicKey rsaPub  = (RSAPublicKey)(fileGeneratedPublicKey);
        BigInteger publicKeyModulus = rsaPub.getModulus();
        BigInteger publicKeyExponent  = rsaPub.getPublicExponent();
        System.out.println("publicKeyModulus: " + publicKeyModulus);
        System.out.println("publicKeyExponent: " + publicKeyExponent);
    } catch (NoSuchAlgorithmException | UnsupportedEncodingException | InvalidKeySpecException e) {
        System.out.println(e.getMessage());
        e.printStackTrace();
    }

Giving the expected output:

给出预期的输出:

publicKeyModulus: 119445732379544598056145200053932732877863846799652384989588303737527328743970559883211146487286317168142202446955508902936035124709397221178664495721428029984726868375359168203283442617134197706515425366188396513684446494070223079865755643116690165578452542158755074958452695530623055205290232290667934914919
publicKeyExponent: 65537