BouncyCastle AttributeCertificate到文件,反之亦然

时间:2016-03-30 19:22:41

标签: java bouncycastle

是否有人知道如何将属性证书写入文件。除了读取byte []并将其转换为属性证书?

2 个答案:

答案 0 :(得分:0)

可以使用证书的编码:

 if ($string =~ qr{^.*perl\/(\S+)\S+}) {
    print Dumper $1;
}

还有另一种选择来读取字节数组并将其解码为证书:

X509V2AttributeCertificate certitificate = new X509V2AttributeCertificate(attributeCertificateHolder.getEncoded());

// Store to file
String fileName = "test.txt";
FileOutputStream fos = new FileOutputStream(fileName);
fos.write(certitificate.getEncoded());
fos.close();

// Read from file
byte[] readCertificate = Files.readAllBytes(new File(fileName).toPath());
X509V2AttributeCertificate decodedCertificate = new X509V2AttributeCertificate(readCertificate);

毕竟X509AttrCertParser parser = new X509AttrCertParser(); parser.engineInit(new FileInputStream(fileName)); X509AttributeCertificate certificate = (X509AttributeCertificate) parser.engineRead(); 存在问题,因为它已被弃用。到目前为止我还没有找到任何解决方案。此解决方案的提示取自Bouncycastle: X509CertificateHolder to X509Certificate?

答案 1 :(得分:0)

如果您要使用弹性城堡来生成属性证书,则很可能使用X509AttributeCertificateHolder。在这种情况下,您可以使用JcaPEMWriter以PEM格式编写属性证书。

// create the attrCertHolder
X509AttributeCertificateHolder attrCertHolder = ...

// write as pem format
JcaPEMWriter pemWriter = new JcaPEMWriter(new FileWriter("<directory-path>/attribute_cert.pem"));
pemWriter.writeObject(attrCertHolder);
pemWriter.flush();

要从文件中检索证书,请使用PEMParser

PEMParser pemParser = new PEMParser(new FileReader("<directory-path>/attribute_cert.pem"));
X509AttributeCertificateHolder attrCertHolderRetrived = (X509AttributeCertificateHolder) pemParser.readObject();

比较它们是否相等:

// Compare the encoded byte arrays and not with equals() method
boolean equals = Arrays.equals(attrCertHolder.getEncoded(), attrCertHolderRetrived.getEncoded());
Preconditions.checkArgument(equals, "original and retrived attribute certs do not match");