简体   繁体   English

如何从Java中的* pem字符串生成RSA私钥

[英]How to generate RSA Private key from *pem string in Java

I want to generate the private key from a string(a .pem file) in Java. 我想从Java中的字符串( .pem文件)生成私钥。

private static final String test = "-----BEGIN RSA PRIVATE KEY-----\n" +
         "MIIEpAIBAAKCAQEAvcCH8WsT1xyrZqq684VPJzOF3hN5DNbowZ96Ie//PN0BtRW2\n" +
// and so on
         "-----END RSA PRIVATE KEY-----";

try {
    String privKeyPEM = test.replace("-----BEGIN RSA PRIVATE KEY-----\n", "");
    privKeyPEM = privKeyPEM.replace("-----END RSA PRIVATE KEY-----", "");

    byte [] encoded = Base64.decode(privKeyPEM);

    PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(encoded);
    KeyFactory kf = KeyFactory.getInstance("RSA");
    PrivateKey privKey = kf.generatePrivate(keySpec);
}
catch (Exception e) {
    e.printStackTrace();
}

The last line(generatePrivate function) is throwing this exception: 最后一行(generatePrivate函数)抛出此异常:

java.security.spec.InvalidKeySpecException: java.security.InvalidKeyException: IOException : algid parse error, not a sequence
    at sun.security.rsa.RSAKeyFactory.engineGeneratePrivate(Unknown Source)
    at java.security.KeyFactory.generatePrivate(Unknown Source)
    at Test.main(Test.java:52)
Caused by: java.security.InvalidKeyException: IOException : algid parse error, not a sequence
    at sun.security.pkcs.PKCS8Key.decode(Unknown Source)
    at sun.security.pkcs.PKCS8Key.decode(Unknown Source)
    at sun.security.rsa.RSAPrivateCrtKeyImpl.<init>(Unknown Source)
    at sun.security.rsa.RSAPrivateCrtKeyImpl.newKey(Unknown Source)
    at sun.security.rsa.RSAKeyFactory.generatePrivate(Unknown Source)
    ... 3 more

If I change the private key to the value from a .der file it works properly, but I need to generate the private key file from a .pem file. 如果我将私钥更改为.der文件中的值,它可以正常工作,但我需要从.pem文件生成私钥文件。

I attached a screenshot of the bytes printed as string(once hard-coded with \\n and once hard-coded without \\n) and once from the file. 我附上了以字符串形式打印的字节的屏幕截图(一次用\\ n硬编码,一次硬编码,没有\\ n),一次从文件中截取。

Bigger Image 更大的形象

产量

The weird thing is that the output from the file is different to the output from the strings. 奇怪的是文件的输出与字符串的输出不同。

If I try to encode a .der file with Base64, the result is different than the string in the .pem file. 如果我尝试使用Base64编码.der文件,则结果与.pem文件中的字符串不同。 Why is that so? 为什么会这样?

You say that the last line is throwing exception, ie 你说最后一行是抛出异常,即

PrivateKey privKey = kf.generatePrivate(keySpec);

Above line works on keyspecs being set correct, ie 上面的行适用于设置正确的keyspecs,即

PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(encoded);

So actual problem is with encoded bytes array. 所以实际问题是编码字节数组。 Have you done System.out after byte [] encoded = Base64.decode(privKeyPEM); byte [] encoded = Base64.decode(privKeyPEM);之后你做过System.out byte [] encoded = Base64.decode(privKeyPEM); and see what the output is. 并看看输出是什么。

I understand that if the message is in MIME format, then after certain characters it appends a combination of carriage return and newline so the strings are not too long for e-mail system or wherever you are using it. 我理解,如果邮件是MIME格式,那么在某些字符之后它会附加回车符和换行符的组合,因此对于电子邮件系统或您使用它的任何地方,字符串都不会太长。

The final String test has some '\\n' in the original text which you use. 最终的String测试在您使用的原始文本中有一些'\\ n'。 You did get rid of other text in below line, 你确实摆脱了下面的其他文字,

String privKeyPEM = test.replace("-----BEGIN RSA PRIVATE KEY-----\n", "");
    privKeyPEM = privKeyPEM.replace("-----END RSA PRIVATE KEY-----", "");

But, look at the string, 但是,看看字符串,

"MIIEpAIBAAKCAQEAvcCH8WsT1xyrZqq684VPJzOF3hN5DNbowZ96Ie//PN0BtRW2\n" +
// and so on
         "-----END RSA PRIVATE KEY-----";

it may have some more '\\n' left which may result in some unwanted characters when you generate keyspec. 当你生成keyspec时,它可能会有更多的'\\ n',这可能会导致一些不需要的字符。 Try more System.out and see what the encoded byte array is like and also before that check String privKeyPEM and see if any extra character is not left in it. 尝试更多的System.out并查看编码的字节数组是什么样的,并在此之前检查String privKeyPEM并查看是否还有任何额外的字符。

Hope this hepls. 希望这个肝脏。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM