KeyTool证书管理
KeyTool是Java中的数字证书管理工具,用于数字证书的申请、导入、导出和撤销等操作。KeyTool与本地密钥库相关联,将私钥存于密钥库,公钥则以数字证书输出。KeyTool位于%JDK_HOME%\bin目录中,需要通过命令行进行相应的操作。
1. 构建自签名证书
keytool -genkeypair -keyalg RSA -keysize 2048 -sigalg SHA1withRSA -validity
36000 -alias www.zlex.org -keystore zlex.keystore
各参数的含义如下所示:
-genkeypair 表示生成密钥。
-keyalg 指定密钥算法,这里指定为RSA算法。
-keysize 指定密钥长度,默认1024位,这里指定为2048位。
-sigalg 指定数字签名算法,这里指定为SHA1withRSA算法。
-validity 指定证书有效期,这里指定为36000天。
-alias 指定别名,这里是www.zlex.org。
-keystore 指定密钥库存储位置,这里是zlex.keystore。
KeyTool工具支持RSA和DSA共2种算法,且DSA算法为默认算法。
导出数字证书
keytool -exportcert -alias www.zlex.org -keystore zlex.keystore -file zlex.cer -rfc
各参数的含义如下所示:
-exportcert 表示证书导出操作。
-alias 指定导别名,这里为www.zlex.org。
-keystore 指定密钥库文件,这里为zlex.keystore。
-file 指定导出文件路径,这里为zlex.cer。
-rfc 指定以Base64编码格式输出。
这里通过KeyTool工具直接导出的证书,是一个自签名的X.509第三版类型的根证书,并以
Base64编码保存。自签名证书虽然可以使用,但未经过CA机构认证,几乎没有任何法律效力。
构建CA签发证书
如果要获取CA机构认证的数字证书,需要将数字证书签发申请(CSR)导出,经由CA机
构认证并颁发,同时将认证后的证书导入本地密钥库和信任库。
导出数字证书签发申请
keytool -certreq -alias www.zlex.org -keystore zlex.keystore -file zlex.csr -v
-certreq 表示数字证书申请操作。
-alias 指定别名,这里为www.zlex.org。
-keystore 指定密钥库文件,这里为zlex.keystore。
-file 指定导出文件路径,这里为zlex.csr。
-v 详细信息。
获得签发后的数字证书后,需要将其导入信任库。导入数字证书操作
导入数字证书
keytool -importcert -trustcacerts -alias www.zlex.org -file zlex.cer -keystore zlex.keystore
导入证书后,我们可以通过相关命令查看该证书,命令如下:
keytool -list -alias www.zlex.org -keystore zlex.keystore
各参数的含义如下所示:
-list 表示导入数字证书。
-alias 指定别名,这里为www.zlex.org。
-keystore 指定密钥库文件,这里为zlex.keystore。
证书使用:
Java 6提供了完善的数字证书管理实现,仅通过操作密钥库和数字证书就可完成相应的加密/解密和签名/验证操作。密钥库管理私钥,数字证书管理公钥,私钥和密钥分属消息传递两方,进行加密消息传递
public static PrivateKey getPrivateKeyByKeyStore(String keyStorePath,String alias,String password)
throws Exception
{
//获取密钥库
KeyStore ks=getKeyStore(keyStorePath, password);
//获得私钥
return (PrivateKey)ks.getKey(alias, password.toCharArray());
}
// 由Certificate获得公钥
private static PublicKey getPublicKeyByCertificate(String certificatePath)
throws Exception
{
//获得证书
Certificate certificate=getCertificate(certificatePath);
//获得公钥
return certificate.getPublicKey();
}
//获得Certificate
private static Certificate getCertificate(String certificatePath)
throws Exception
{
CertificateFactory certificateFactory=CertificateFactory.getInstance(CERT_TYPE);
FileInputStream in=new FileInputStream(certificatePath);
Certificate certificate=certificateFactory.generateCertificate(in);
in.close();
return certificate;
}
private static KeyStore getKeyStore(String keyStorePath,String password)
throws Exception{
//实例化密钥库
KeyStore ks=KeyStore.getInstance(KeyStore.getDefaultType());
//获得密钥库文件流
FileInputStream is=new FileInputStream(keyStorePath);
//加载密钥库
ks.load(is,password.toCharArray());
//关闭密钥库文件流
is.close();
return ks;
}
私钥加密:
public static byte[] encryptByPrivateKey(byte[] data,String keyStorePath,String alias,String password)
throws Exception
{
//获取私钥
PrivateKey privateKey=getPrivateKeyByKeyStore(keyStorePath, alias, password);
//对数据加密
Cipher cipher=Cipher.getInstance(privateKey.getAlgorithm());
cipher.init(Cipher.ENCRYPT_MODE, privateKey);
return cipher.doFinal(data);
}
//私钥解密
public static byte[] decryptByPrivateKey(byte[] data,String keyStorePath,String alias,String password)
throws Exception
{
//获取私钥
PrivateKey privateKey=getPrivateKeyByKeyStore(keyStorePath, alias, password);
//对数据解密
Cipher cipher=Cipher.getInstance(privateKey.getAlgorithm());
cipher.init(Cipher.DECRYPT_MODE, privateKey);
return cipher.doFinal(data);
}
//公钥加密
public static byte[] encryptByPublicKey(byte[] data,String certificatePath)
throws Exception
{
//取得公钥
PublicKey publicKey=getPublicKeyByCertificate(certificatePath);
//对数据加密
Cipher cipher=Cipher.getInstance(publicKey.getAlgorithm());
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
return cipher.doFinal(data);
}
//公钥解密
public static byte[] decryptByPublicKey(byte[] data,String certificatePath)
throws Exception
{
//取得公钥
PublicKey publicKey=getPublicKeyByCertificate(certificatePath);
Cipher cipher=Cipher.getInstance(publicKey.getAlgorithm());
cipher.init(Cipher.DECRYPT_MODE, publicKey);
return cipher.doFinal(data);
}
//签名
public static byte[] sign(byte[] sign,String keyStorePath,String alias,String password)
throws Exception
{
//获得证书
X509Certificate x509Certificate=(X509Certificate)getCertificate(keyStorePath, alias, password);
//构建签名,由证书指定签名算法
Signature signature=Signature.getInstance(x509Certificate.getSigAlgName());
//获取私钥
PrivateKey privateKey=getPrivateKeyByKeyStore(keyStorePath, alias, password);
//初始化签名,由私钥构建
signature.initSign(privateKey);
signature.update(sign);
return signature.sign();
}
//验证签名
public static boolean verify(byte[] data,byte[] sign,String certificatePath)
throws Exception
{
//获得证书
X509Certificate x509Certificate=(X509Certificate)getCertificate(certificatePath);
//由证书构建签名
Signature signature=Signature.getInstance(x509Certificate.getSigAlgName());
//由证书初始化签名,实际上用到了证书中的公钥
signature.initVerify(x509Certificate);
signature.update(data);
return signature.verify(sign);
}
测试用例如下:
public class CertificateCoderTest {
private String password = "100889";
private String alias = "www.zlex.org";
private String certificatePath = "E:/jdk1.6/bin/zlex.cer";
private String keyStorePath = "E:/jdk1.6/bin/zlex,keystore";
@Test
public void test1() throws Exception
{
System.err.println("公钥加密-----私钥解密");
String inputStr="数字签名";
byte[] data=inputStr.getBytes();
//公钥加密
byte[] encrypt=CertificateCoder.encryptByPublicKey(data, certificatePath);
//私钥解密
byte[] decrypt=CertificateCoder.decryptByPrivateKey(encrypt, keyStorePath, alias, password);
String outputStr=new String(decrypt);
System.out.println("--"+outputStr);
System.err.println("加密前:\n"+inputStr);
System.err.println("解密后:\n"+outputStr);
assertArrayEquals(data, decrypt);
}
/**
* 私钥加密—公钥解密
* @throws Exception
*/
@Test
public void test2() throws Exception {
System.err.println("私钥加密—公钥解密");
String inputStr = "数字签名";
byte[] data = inputStr.getBytes();
// 私钥加密
byte[] encodedData = CertificateCoder.encryptByPrivateKey(data,
keyStorePath, alias, password);
// 公钥加密
byte[] decodedData = CertificateCoder.decryptByPublicKey(encodedData,
certificatePath);
String outputStr = new String(decodedData);
System.err.println("加密前:\n" + inputStr);
System.err.println("解密后:\n" + outputStr);
assertEquals(inputStr, outputStr);
}
/**
* 签名验证
* @throws Exception
*/
@Test
public void testSign() throws Exception
{
String inputStr = "签名";
byte[] data = inputStr.getBytes();
System.err.println("私钥签名—公钥验证");
// 产生签名
byte[] sign = CertificateCoder.sign(data, keyStorePath, alias, password);
System.err.println("签名:\n" + Hex.encodeHexString(sign));
// 验证签名
boolean status = CertificateCoder.verify(data, sign, certificatePath);
System.err.println("状态:\n" + status);
// 校验
assertTrue(status);
}
}
分享到:
相关推荐
SM2数字证书,充分借鉴与吸收了X509数字证书的格式与优点,在考虑RSA算法的优缺点之后,推出的基于商密公钥算法的SM2证书规范,至此,商密算法的数字证书在商用密码应用安全性评估中成为了必须要检测的一个重要环节...
本规范规定了税务系统数字证书应用接口,包括客户端调用用户密码设备(如USBKEY)接口和应用服务器端调用签名及认证服务器接口,为完整实现税务系统数字证书身份认证、签名验签、数字信封加解密等应用功能提供接口。...
1:调用.NET2.0的MAKECERT创建含有私钥的数字证书,并存储到个人证书区; 2:将该证书导出为pfx文件,并为其指定一个用来打开pfx文件的password; 3:读取pfx文件,导出pfx中公钥和私钥; 4:用pfx证书中的公钥进行...
本标准是为我国信息安全基础设施建设中关于数字证书认证系统密码协议...实体对数字证书认证系统的真实性、保密性、完整性、可认证性和不可否认性等安全需求。 本标准凡涉及密码算法相关内容,按照国家有关法规实施。
实验二 数字证书应用 数字证书实验详细步骤原理 利用数字证书建立安全Web通信 利用数字证书实现电子邮件安全
支付宝数字证书助手支付宝数字证书助手支付宝数字证书助手支付宝数字证书助手支付宝数字证书助手支付宝数字证书助手支付宝数字证书助手支付宝数字证书助手支付宝数字证书助手
所有与数字证书相关的各种概念和技术,统称为PKI( Public Key Infrastructure 公钥基础设施),为解决公钥与用户映射关系问题,PKI引入了数字证书。数字证书里包含了用户身份信息,用户公钥信息(两者用于确定用户...
使用TOMCAT和JDK搭建SSL服务, JAVA获取数字证书项, 如何用OPENSSL签发证书, 支持第三方CA
vs2017数字证书certificates安装
用图文的方式对数字签名和数字证书之间的关系进行一个详细说明、希望大家看后恍然大悟。我目前正在用C#做证书和签名这块,但进展十分不畅,如果哪位朋友有好的方法或者示例,请私信联系
标准内部OA系统数字证书应用平台方案
ca 数字证书 粤港跨境研究资料 针对粤港两地政策法规未完全规范,以及两地数字证书认证机构(CA)在认证业务声明和证书策略两方面存在差异 的问题,分析目前跨域公钥基础设施信任架构,提出一种针对当前两地政策法规未...
官方网站上的下载没有该证书,是找服务人员要到的,可解决64位操作系统不能识别数字证书的问题。温馨提示,此驱动是明华Ukey版本
制作符合国际标准的数字证书和应用数字证书进行文件保密的工具集合,还有用于代码签名的辅助工具,适用于个人或小企业、小团体内部使用。短小精简,绿色免安装,是学习、掌握数字证书应用的最佳实践工具。
信息安全大作业_CA系统的设计和实现源码(电子认证服务系统_数字证书数字签名python语言)+项目详细说明.7z CA代表Certificate Authority。也就是电子认证服务或机构,为电子签名相关各方提供真实性和可靠性验证,是...
为了规范电子政务数字证书应用,提升数字证书的应用价值,本规范定义了证书应用的环境函数、 证书解析函数、随机数函数、签名验证函数、加解密函数、时间戳函数以及数据服务函数u通过以上 函数的定义,可以提供完菩...
golang解析数字证书 PKCS#1 PKCS#8格式的私钥
从网络上找到这PKI/CA与数字证书技术大全和PKI原理与技术两部分内容,汇总一下,供大家一起学习PKI相关知识
在WEB应用中使用基于数字证书的登录验证
国密数字证书验证、SM2椭圆曲线公钥密码算法、SM2椭圆曲线公钥密码算法推荐曲线参数、SM3密码杂凑算法、SM4分组密码算法、国密数字证书验证方法、使用国密算法的数字证书的验证签名的详细例子