【发布时间】:2015-02-05 11:03:52
【问题描述】:
我正在尝试从受密码保护的 PEM 文件中读取数据,以从这些数据中生成公钥和私钥。我有密码,我需要在打开文件时传递该密码。但我在 java 中没有任何选项可以做到这一点。
在这方面需要紧急援助。如果我能得到一个代码示例就更好了。
已编辑:
我当前的代码是 -
public void readPrivateKey(String filePath)
{
File fp = new File(filePath);
FileInputStream fis = new FileInputStream(fp);
DataInputStrean dis = new DataInputStream(fis);
byte [] keyBytes = new byte [(int) fp.length()];
dis.readFully(keyBytes);
dis.close();
String temp = new String(keyBytes);
String privateKeyPem = removeUnnecessaryTags(temp); //this function returns a string after removing String like this "BEGIN......."
byte[] decoded = Base64.decode(privateKeyPem);
ASN1Sequence primitive = (ASN1Sequence) ASN1Sequence.fromByteArray(decoded);
Enumeration <?> e = primitive.getObjects();
BigInteger bigInt = ((DERInteger)e.NextElement()).getValue();
int version = bigInt.intValue();
BigInteger modulus = ((DERInteger)e.NextElement()).getValue();
BigInteger publicExp = ((DERInteger)e.NextElement()).getValue();
//... all the values has been retrieved in this way
}
【问题讨论】:
-
你检查
new JcePEMDecryptorProviderBuilder().build(password);了吗(见stackoverflow.com/questions/14919048/…)