【发布时间】:2013-05-15 13:18:00
【问题描述】:
我很难理解如何使用给我的私钥和公钥文件。
我有 2 个文件,public.pem 和 private.pem。我正在尝试使用 BouncyCastle 来获取公钥和私钥。我编写了这个类来尝试提取密钥:
public class KeyReaders {
public static class PublicKeyReader {
public static byte[] get(String filename)
throws Exception {
FileReader f = new FileReader(filename);
PEMParser pp = new PEMParser(f);
SubjectPublicKeyInfo o = (SubjectPublicKeyInfo )pp.readObject();
return o.parsePublicKey().getEncoded();
}
}
public static class PrivateKeyReader {
public static byte[] get(String filename)
throws Exception {
FileReader f = new FileReader(filename);
PEMParser pp = new PEMParser(f);
PEMKeyPair o = (PEMKeyPair)pp.readObject();
return o.getPrivateKeyInfo().getEncoded();
}
}
}
我似乎不知道如何使用这些键来解码文件。我有一个文件test.txt,我无法使用私钥对其进行解码。我不能 100% 确定这是否是读取 .PEM 文件的适当方式。
那么,在给定私钥文件的情况下,如何使用 BouncyCastle 解码文本文件?
【问题讨论】:
-
立即将它们重新编码为
byte[]似乎是个错误。任何加密 API 都可能需要您已解码的PublicKey或PrivateKey。 -
我想我在弄清楚如何获取基本 PublicKey 和 PrivateKey 时遇到了麻烦。
标签: java cryptography key bouncycastle decoding