繁体   English   中英

如何读取受密码保护的文件的内容?

[英]How to read contents of a password protected file?

我正在尝试从受密码保护的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
}

我相信您正在考虑使用RSA密钥对。

您无需使用Java就可以做到这一点,

openssl rsa -in MYFILE.pem -pubout > MYFILE.pub
ssh-keygen -f MYFILE.pub -i -m PKCS8

如果需要使用Java进行此操作,请在注释中引用@ m0skit0给出的链接。


暂无
暂无

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

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