Java中提供了keytool生成密钥文件的方式,用于一定程度的隐私保护。Android中就是通过此证书方式,用于apk的安装验证,在相关项目中使用对应的jks验证文件,可以保证不同开发者对同一工程进行调试时,无需卸载安装。
##keytool简介
keytool是java的数据证书管理工具,keytool将密钥(key)和证书(certificates)存在keystore文件中,该文件中包含两种数据:
- 密钥实体(key entity)-密钥(sercet key),又或者是私钥和配对公钥(采用非对称加密)
- 可信任的证书实体(trusted certificate entities)-只包含公钥
每个keystore都关联一个不区分大小写的别名alias。
###keytool参数介绍
JDK1.8中keytool参数介绍
-J -- specify java option 指定java选项,通常为环境或内存选项
-alias -- alias 别名
-certreq -- command to generate certificate signing request 生成证书签发请求CSR用于发送给CA替换keystore中已经存在的cetrificate chain(可以指定csr文件,否则在stdout输出 示例:keytool –certReq –keystore keystore.jks –file mycsr.csr)
-delete -- command to delete entry 删除密钥库
-dest -- destination alias 目标别名
-dname -- X.500 distinguish name 指定证书拥有者信息 例如: "CN=名字与姓氏,OU=组织单位名称,O=组织名称,L=城市或区域名称,ST=州或省份名称,C=单位的两字母国家代码" (格式:CN=cName, OU=orgUnit, O=org, L=city, S=state, C=countryCode)
-export -- command to store certificate 将别名指定的证书导出到文件 keytool -export -alias 需要导出的别名 -keystore 指定keystore -file 指定导出的证书位置及证书名称 -storepass 密码
-file -- specify certificate file 参数指定导出到文件的文件名
-file -- specify certificate signing request file
-file -- specify identity database file
-genkey -- command to generate a key pair 在用户主目录中创建一个默认文件".keystore",还会产生一个mykey的别名,mykey中包含用户的公钥、私钥和证书
-help -- command to print help message
-import -- command to import certificate or certificate chain 将已签名数字证书导入密钥库 keytool -import -alias 指定导入条目的别名
-keystore 指定keystore -file 需导入的证书
-keyalg -- key algorithm 指定密钥的算法 (如 RSA DSA(如果不指定默认采用DSA))
-keyclone -- command to create new keystore entry
-keypass -- password for private key 指定别名条目的密码(私钥的密码)
-keypasswd -- command to change password for private key 修改密钥库中指定条目口令 keytool -keypasswd -alias 需修改的别名 -keypass 旧密码 -new 新密码 -storepass keystore密码 -keystore sage
-keysize -- key size 指定密钥长度
-keystore -- keystore location 指定密钥库的名称(产生的各类信息将不在.keystore文件中)
-list -- command to print keystore entry 显示密钥库中的证书信息 keytool -list -v -keystore 指定keystore -storepass 密码
-new -- new password
-new -- nwe password for private key
-new -- password for private key of new entry
-noprompt -- disable interaction with the user (If the -noprompt option is provided, then the user is not prompted for a new destination alias. Existing entries are overwritten with the destination alias name. Entries that cannot be imported are skipped and a warning is displayed.)
-printcert -- command to print certificate in a human-readable format 查看导出的证书信息 keytool -printcert -file yushan.crt
-rfc -- make certificate format printable as RFC 1421 如果指定了 -rfc 选项,将以可打印的编码格式输出证书。
keytool -list -rfc -keystore e:/yushan.keystore -storepass 123456
-selfcert -- command to generate X.509 v1 self-signed certificate
-sigalg -- signature algorithm (The sigalg value specifies the algorithm that should be used to sign the CSR.)
-storepass -- password for keystore 指定密钥库的密码(获取keystore信息所需的密码)
-storepasswd -- command to change password for keystore 修改keystore口令 keytool -storepasswd -keystore e:/yushan.keystore(需修改口令的keystore) -storepass 123456(原始密码) -new yushan(新密码)
-storetype -- keystore type (For keytool and jarsigner, you can specify a keystore type at the command line, with the -storetype option. For Policy Tool, you can specify a keystore type with the Keystore menu.)
-trustcacerts -- use cacerts (If the -trustcacerts option was specified, then additional certificates are considered for the chain of trust, namely the certificates in a file named cacerts.)
-v -- make certificate format human-readable
-v -- verbose mode 显示密钥库中的证书详细信息
-validity -- valid days 指定创建的证书有效期多少天
上述主要参数参考网上说明,部分直接引用了官方文档中描述,实际使用过程应该根据需求调整,此处不做过多描述。