X509AttributeCertificateHolder编码和解码

时间:2017-06-20 08:56:52

标签: java decode encode bouncycastle attributecertificate

有人知道如何编码/解码X509AttributeCertificateHolder吗?

我尝试了以下代码(att是创建的X509AttributeCertificateHolder):

byte[] arr = att.getEncoded();
X509AttributeCertificateHolder holder = new X509AttributeCertificateHolder(arr);

问题是:holderatt的属性不一样。

我使用了以下简单测试:

for (int i = 0; i < holder.getAttributes().length; i++) {
    Attribute attr1 = holder.getAttributes()[i];
    Attribute attr2 = att.getAttributes()[i];

    System.out.println("Holder value after decode: " + attr1.getAttrValues());
    System.out.println("Holder value before encode: " + attr2.getAttrValues());
}

测试结果如下:

  

解码后的持有人价值:   [[[1] [6]#69643a2f2f444155313233343536373839]

     

编码前的持有人值:[姓名:id:// DAU123456789 - 身份验证:N / A]

这是base64中的结构:

MIIBvzCCASgCAQEwZ6BlMGCkXjBcMQswCQYDVQQGEwJBVTEoMCYGA1UECgwfVGhl
IExlZ2lvbiBvZiB0aGUgQm91bmN5IENhc3RsZTEjMCEGA1UECwwaQm91bmN5IFBy
aW1hcnkgQ2VydGlmaWNhdGUCAQKgYjBgpF4wXDELMAkGA1UEBhMCQVUxKDAmBgNV
BAoMH1RoZSBMZWdpb24gb2YgdGhlIEJvdW5jeSBDYXN0bGUxIzAhBgNVBAsMGkJv
dW5jeSBQcmltYXJ5IENlcnRpZmljYXRlMA0GCSqGSIb3DQEBBQUAAgEBMCIYDzIw
MTcwNjIwMTQ1MDIyWhgPMjAxNzA2MjAxNDUyMDJaMCAwHgYDVQRIMRcwFaEThhFp
ZDovL0RBVTEyMzQ1Njc4OTANBgkqhkiG9w0BAQUFAAOBgQBJ3qTRoIugVaP0KSyd
jcMV3crYjuVGapxe6TTJtDqHc8xXreGhoqvSZv/r6hc6D0Fkjc45fZN4iDml3aLy
E7EsGsRFEm+6cLP4/8s8kgkbPk8ZjslxuQz+IScTXHQABv/5gVzjCC+4cTZ/BccM
KtbQwhNz+aIiJM60uVcW+hfC0w==

1 个答案:

答案 0 :(得分:2)

要查看正在进行的操作,我已使用您的代码并执行以下操作:

Attribute attr1 = holder.getAttributes()[i];
Attribute attr2 = att.getAttributes()[i];

ASN1Set values1 = attr1.getAttrValues();
System.out.println(values1.getObjectAt(0).getClass());
ASN1Set values2 = attr2.getAttrValues();
System.out.println(values2.getObjectAt(0).getClass());

输出结果为:

  

class org.bouncycastle.asn1.DLSequence
  class org.bouncycastle.asn1.x509.RoleSyntax

因此,在编码之前(在att变量中),属性值为RoleSyntax。如果您查看其toString()方法,它会以Name: [value] - Auth: [value]格式打印值。

但是在编码后(在holder变量中),由于某种原因,BouncyCastle丢失了这些信息,属性值变为DLSequence

要解决此问题,您需要使用此序列创建RoleSyntax对象:

Attribute attr1 = holder.getAttributes()[i];

ASN1Set values1 = attr1.getAttrValues();

RoleSyntax rl = RoleSyntax.getInstance(attr1.getAttrValues().getObjectAt(0));
System.out.println(rl);

输出将是:

  

姓名:id:// DAU123456789 - 身份验证:N / A