gpt4 book ai didi

java - X500Name 到 java.security.Principal

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:46:24 26 4
gpt4 key购买 nike

我正在尝试寻找实现 java.security.principal 的类来为证书提供主题 DN 值。在搜索时,我发现 X500Name 是通常用于提供主题的类。但是,这并没有实现 Principal 接口(interface)。令我惊讶的是,X500Name 的折旧类 X509Principal 实现了这个接口(interface),这与 X500Name 不同。使用什么类?

谢谢

最佳答案

我建议使用充气城堡类: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()));
}
}

关于java - X500Name 到 java.security.Principal,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23362698/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com