[英]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.