为什么EncryptedPrivateKeyInfo无法读取Java中的PKCS#8加密私钥?

时间:2018-08-16 18:31:20

标签: java encryption cryptography rsa pkcs#8

我正在尝试用Java解密密码加密的私钥,并且遇到过这个previous question,但是当我尝试相同的操作时,会遇到以下异常:

Exception in thread "main" java.io.IOException: ObjectIdentifier() -- data isn't an object ID (tag = 48)
at sun.security.util.ObjectIdentifier.<init>(ObjectIdentifier.java:257)
at sun.security.util.DerInputStream.getOID(DerInputStream.java:314)
at com.sun.crypto.provider.PBES2Parameters.engineInit(PBES2Parameters.java:267)
at java.security.AlgorithmParameters.init(AlgorithmParameters.java:293)
at sun.security.x509.AlgorithmId.decodeParams(AlgorithmId.java:132)
at sun.security.x509.AlgorithmId.<init>(AlgorithmId.java:114)
at sun.security.x509.AlgorithmId.parse(AlgorithmId.java:372)
at javax.crypto.EncryptedPrivateKeyInfo.<init>(EncryptedPrivateKeyInfo.java:95)
at TestDecryptKey.main(TestDecryptKey.java:65)

其中TestDecryptKey.java是我的测试文件。这是我在做什么的摘要:

import javax.crypto.EncryptedPrivateKeyInfo;
import javax.crypto.spec.PBEKeySpec;
import java.util.Base64;
import java.security.GeneralSecurityException;
import java.io.IOException;

public class TestDecryptKey {
  public static void main(String[] args) throws GeneralSecurityException, IOException {
    String encryptedBase64PrivateKey = "MII...";
    String password = "...";
    byte[] encryptedPrivateKey = Base64.getDecoder().decode(encryptedBase64PrivateKey.getBytes("UTF-8"));
    PBEKeySpec pbeSpec = new PBEKeySpec(password.toCharArray());
    // Exception is thrown here
    EncryptedPrivateKeyInfo encryptedPrivateKeyInfo = new EncryptedPrivateKeyInfo(encryptedPrivateKey);
 }
}

我假设它声称我提供的加密密钥数据不是有效的加密密钥,但是openssl不同意。我将Base64字符串的页眉和页脚“ ----- BEGIN加密的私钥-----”和“ ----- END加密的私钥-----”保存为文件“ pem_key”和使用以下命令和上面的密码:

openssl pkcs8 -inform pem -in pem_key -outform der

我能够输出解密的密钥字节。我要去哪里错了?

0 个答案:

没有答案