X500Name到java.security.Principal

时间:2014-04-29 10:57:30

标签: java bouncycastle

我正在尝试查找实现java.security.principal的类,以便为证书提供Subject DN值。在搜索时,我遇到的X500Name是通常用于提供主题的类。但是,这不实现Principal接口。让我感到惊讶的是X500Name的折旧类X509Principal与X500Name不同,实现了这个界面。使用什么课程?

由于

2 个答案:

答案 0 :(得分:6)

我建议使用一个充满活力的城堡类:org.bouncycastle.jce.X509Principal来实现java.security.Principal。要获得org.bouncycastle.jce.X509Principal实例,您可以使用以下方法:

public static org.bouncycastle.jce.X509Principal getSubjectX509Principal(
    java.security.cert.X509Certificate cert) throws CertificateEncodingException
课程org.bouncycastle.jce.PrincipalUtil

我给你一个样本:

import java.io.FileInputStream;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;

public class X509PrincipalSample {

    public static void main(String[] args) throws Exception {

            CertificateFactory cf = CertificateFactory.getInstance("X509");
            // certificate file must be encoded in DER binary format
            FileInputStream certificateFile = new FileInputStream("/tmp/cer.cer");
            X509Certificate certificate = (X509Certificate) cf.generateCertificate(certificateFile);
            X509Principal x509Principal = PrincipalUtil.getSubjectX509Principal(certificate);
            System.out.println(x509Principal.getName());
    }
}

希望这有帮助,

编辑:

正如你所说,org.bouncycastle.jce.X509Principal在最后一个充气城堡发布中被弃用了。因此,您可以使用org.bouncycastle.asn1.x500.X500Name来处理subjectDN字段,但是如果您想使用实现java.security.Principal的类使用javax.security.auth.x500.X500Principal,我会举一个例子:

import java.io.FileInputStream;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;

import javax.security.auth.x500.X500Principal;

import org.bouncycastle.asn1.x500.RDN;
import org.bouncycastle.asn1.x500.X500Name;
import org.bouncycastle.asn1.x500.style.BCStyle;
import org.bouncycastle.asn1.x500.style.IETFUtils;


public class X500NameSample {

    public static void main(String[] args) throws Exception {

         CertificateFactory cf = CertificateFactory.getInstance("X509");
         // certificate file must be encoded in DER binary format
         FileInputStream certificateFile = new FileInputStream("C:/Documents and Settings/aciffone/Escritorio/cer.cer");
         X509Certificate certificate = (X509Certificate) cf.generateCertificate(certificateFile);

         // using X500Principal
         X500Principal subjectX500Principal = certificate.getSubjectX500Principal();
         System.out.println(subjectX500Principal.getName());
         System.out.println(subjectX500Principal.getName(X500Principal.RFC1779));
         System.out.println(subjectX500Principal.getName(X500Principal.CANONICAL));

         // using X500Name
         X500Name x500name = new X500Name( subjectX500Principal.getName(X500Principal.RFC1779) );
         // you can get the different subject DN values with BCStyle constants
         RDN cn = x500name.getRDNs(BCStyle.CN)[0];
         System.out.println(IETFUtils.valueToString(cn.getFirst().getValue()));

         x500name = new X500Name( subjectX500Principal.getName() );
         // you can get the different subject DN values with BCStyle constants
         cn = x500name.getRDNs(BCStyle.CN)[0];
         System.out.println(IETFUtils.valueToString(cn.getFirst().getValue()));
    }
}

答案 1 :(得分:1)