帮我用java实现生成签名证书和加密证书
时间: 2023-05-14 22:05:06 浏览: 117
一个完整可用的证书签名(验签),加密(解密)java源码
5星 · 资源好评率100%
好的,生成签名证书和加密证书可以使用Java的密钥库(KeyStore)和证书(Certificate)类来实现。以下是一个简单的示例代码:
生成签名证书:
```java
import java.io.FileOutputStream;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.cert.Certificate;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;
import java.util.Date;
public class GenerateSignatureCertificate {
public static void main(String[] args) throws Exception {
// 生成密钥对
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
keyPairGenerator.initialize(2048);
KeyPair keyPair = keyPairGenerator.generateKeyPair();
PrivateKey privateKey = keyPair.getPrivate();
PublicKey publicKey = keyPair.getPublic();
// 生成证书
X509Certificate certificate = generateCertificate(privateKey, publicKey);
// 保存证书到文件
FileOutputStream fos = new FileOutputStream("signature.cer");
fos.write(certificate.getEncoded());
fos.close();
}
private static X509Certificate generateCertificate(PrivateKey privateKey, PublicKey publicKey) throws Exception {
// 设置证书信息
X509Certificate certificate = null;
CertificateFactory cf = CertificateFactory.getInstance("X.509");
Date startDate = new Date(System.currentTimeMillis() - 24 * 60 * 60 * 1000);
Date endDate = new Date(System.currentTimeMillis() + 365 * 24 * 60 * 60 * 1000);
String subject = "CN=My Company, OU=My Department, O=My Organization, L=My City, ST=My State, C=My Country";
String issuer = subject;
certificate = (X509Certificate) cf.generateCertificate(new ByteArrayInputStream(generateSelfSignedCertificate(privateKey, publicKey, subject, issuer, startDate, endDate)));
return certificate;
}
private static byte[] generateSelfSignedCertificate(PrivateKey privateKey, PublicKey publicKey, String subject, String issuer, Date startDate, Date endDate) throws Exception {
// 生成自签名证书
X509V3CertificateGenerator certGen = new X509V3CertificateGenerator();
X500Principal dnName = new X500Principal(subject);
certGen.setSerialNumber(BigInteger.valueOf(System.currentTimeMillis()));
certGen.setIssuerDN(dnName);
certGen.setNotBefore(startDate);
certGen.setNotAfter(endDate);
certGen.setSubjectDN(dnName);
certGen.setPublicKey(publicKey);
certGen.setSignatureAlgorithm("SHA256WithRSAEncryption");
return certGen.generate(privateKey, "BC");
}
}
```
生成加密证书:
```java
import java.io.FileOutputStream;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.cert.Certificate;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;
import java.util.Date;
public class GenerateEncryptionCertificate {
public static void main(String[] args) throws Exception {
// 生成密钥对
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
keyPairGenerator.initialize(2048);
KeyPair keyPair = keyPairGenerator.generateKeyPair();
PrivateKey privateKey = keyPair.getPrivate();
PublicKey publicKey = keyPair.getPublic();
// 生成证书
X509Certificate certificate = generateCertificate(privateKey, publicKey);
// 保存证书到文件
FileOutputStream fos = new FileOutputStream("encryption.cer");
fos.write(certificate.getEncoded());
fos.close();
}
private static X509Certificate generateCertificate(PrivateKey privateKey, PublicKey publicKey) throws Exception {
// 设置证书信息
X509Certificate certificate = null;
CertificateFactory cf = CertificateFactory.getInstance("X.509");
Date startDate = new Date(System.currentTimeMillis() - 24 * 60 * 60 * 1000);
Date endDate = new Date(System.currentTimeMillis() + 365 * 24 * 60 * 60 * 1000);
String subject = "CN=My Company, OU=My Department, O=My Organization, L=My City, ST=My State, C=My Country";
String issuer = subject;
certificate = (X509Certificate) cf.generateCertificate(new ByteArrayInputStream(generateSelfSignedCertificate(privateKey, publicKey, subject, issuer, startDate, endDate)));
return certificate;
}
private static byte[] generateSelfSignedCertificate(PrivateKey privateKey, PublicKey publicKey, String subject, String issuer, Date startDate, Date endDate) throws Exception {
// 生成自签名证书
X509V3CertificateGenerator certGen = new X509V3CertificateGenerator();
X500Principal dnName = new X500Principal(subject);
certGen.setSerialNumber(BigInteger.valueOf(System.currentTimeMillis()));
certGen.setIssuerDN(dnName);
certGen.setNotBefore(startDate);
certGen.setNotAfter(endDate);
certGen.setSubjectDN(dnName);
certGen.setPublicKey(publicKey);
certGen.setSignatureAlgorithm("SHA256WithRSAEncryption");
return certGen.generate(privateKey, "BC");
}
}
```
这两个示例代码都使用了Bouncy Castle库来生成证书。如果你没有安装Bouncy Castle库,可以从官网下载并安装。
阅读全文