版权声明:转载请注明出处 https://blog.csdn.net/laozhaishaozuo/article/details/82285828
- 准备测试数据
- 测试代码
准备测试数据
1)准备密钥
keytool -genkey -keystore "shaozuo.keystore" -alias shaozuoKey -keyalg RSA -validity 365 -dname "CN=shaozuo, OU=shaozuo, O=shaozuo, L=Beijing, ST=Beijing, C=CN" -keypass shaozuo -storepass shaozuo
2)导出证书
keytool -alias shaozuoKey -exportcert -keystore shaozuo.keystore -file shaozuo_pub.cer -storepass shaozuo
测试代码
SecurityCertTest
mport static org.junit.Assert.assertEquals;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.security.GeneralSecurityException;
import java.security.KeyStore;
import java.security.PrivateKey;
import java.security.Security;
import java.security.cert.CRL;
import java.security.cert.CertPath;
import java.security.cert.Certificate;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;
import java.util.Collection;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.junit.Ignore;
import org.junit.Test;
import com.google.common.collect.Lists;
/**
* 证书测试
*
* @author shaozuo
*
*/
public class SecurityCertTest {
private static final String X_509 = "X.509";
static {
Security.insertProviderAt(new BouncyCastleProvider(), 1);
}
@Test
public void testKeyStore() throws GeneralSecurityException, IOException {
File file = new File("src/test/resources/shaozuo.keystore");
if (file.exists()) {
FileInputStream is = new FileInputStream(file);
KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
keyStore.load(is, "shaozuo".toCharArray());
is.close();
PrivateKey key = (PrivateKey) keyStore.getKey("shaozuoKey", "shaozuo".toCharArray());
System.out.println(key.getAlgorithm());
}
}
/**
* @throws GeneralSecurityException
* @throws IOException
*/
@Test
public void testCertificateFactory() throws GeneralSecurityException, IOException {
CertificateFactory certificateFactory = CertificateFactory.getInstance(X_509);
File file = new File("src/test/resources/shaozuo_pub.cer");
if (file.exists()) {
FileInputStream is = new FileInputStream(file);
Certificate certificate = certificateFactory.generateCertificate(is);
is.close();
// System.out.println(certificate.getPublicKey().getAlgorithm());
if (StringUtils.equals(X_509, certificate.getType())) {
X509Certificate cX509Certificate = (X509Certificate) certificate;
// System.out.println(cX509Certificate.toString());
assertEquals(CryptoAlgorithm.RSA.name(),
cX509Certificate.getPublicKey().getAlgorithm());
}
}
}
/**
* 测试证书吊销列表
*
*
* @throws GeneralSecurityException
* @throws IOException
*/
@Test
@Ignore
public void testCRL() throws GeneralSecurityException, IOException {
// TODO 找到生成吊销列表的方法
CertificateFactory certificateFactory = CertificateFactory.getInstance(X_509);
File file = new File("src/test/resources/shaozuo.keystore");
if (file.exists()) {
FileInputStream is = new FileInputStream(file);
CRL crl = certificateFactory.generateCRL(is);
is.close();
System.out.println(crl.toString());
}
}
/**
* 测试CertPath
*
*
* @throws GeneralSecurityException
* @throws IOException
*/
@Test
public void testGenCertPathByCerts() throws GeneralSecurityException, IOException {
CertificateFactory certificateFactory = CertificateFactory.getInstance(X_509);
File file = new File("src/test/resources/laozhai_pub.p7b");
if (file.exists()) {
FileInputStream is = new FileInputStream(file);
Collection<? extends Certificate> certificates = certificateFactory
.generateCertificates(is);
// the List of Certificates
List<? extends Certificate> certs = Lists.newArrayList(certificates);
System.out.println(certs.size());
CertPath certPath = certificateFactory.generateCertPath(certs);
}
}
/**
* 测试CertPath
*
*
* @throws GeneralSecurityException
* @throws IOException
*/
@Test
public void testGenCertPathByInput() throws GeneralSecurityException, IOException {
CertificateFactory certificateFactory = CertificateFactory.getInstance(X_509);
File file = new File("src/test/resources/laozhai_pub.p7b");
if (file.exists()) {
FileInputStream is = new FileInputStream(file);
CertPath certPath = certificateFactory.generateCertPath(is, "PKCS7");
is.close();
}
}
/**
* 测试CertPath
*
*
* @throws GeneralSecurityException
* @throws IOException
*/
@Test
public void testGenCertPathByInputCer() throws GeneralSecurityException, IOException {
CertificateFactory certificateFactory = CertificateFactory.getInstance(X_509);
File file = new File("src/test/resources/laozhai_pub.cer");
if (file.exists()) {
FileInputStream is = new FileInputStream(file);
CertPath certPath = certificateFactory.generateCertPath(is, "PKCS7");
is.close();
}
}
/**
* 测试Certificates
*
*
* @throws GeneralSecurityException
* @throws IOException
*/
@Test
public void testCertificates() throws GeneralSecurityException, IOException {
CertificateFactory certificateFactory = CertificateFactory.getInstance(X_509);
File file = new File("D:\\program_language\\Java\\jre1.8.0_171\\lib\\security\\cacerts");
if (file.exists()) {
FileInputStream is = new FileInputStream(file);
KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
keyStore.load(is, "changeit".toCharArray());
is.close();
}
}
}