本文内容只是一个简单的笔记,内容主要来自于
man keytool
命令
Android 开发中,需要对应用进行签名,一般签名信息存储在一个 keystore 文件中,这个文件可以用 keytool 进行读写。以下介绍 keystore 的常见用法。
keytool 用于管理 keystore 文件,keystore 文件可以理解为是一个加密的用于存储证书的数据库,里面用于存储各类证书,每个证书由 alias 区分,并且有自己单独的密码。
- keystore 生成
- keystore 查看
- keystore 密码修改
- alias 修改
- keystore 格式转换
- alias 密码修改
- key 归集
- 删除 key
- CA 认证
- CA 生成证书
- 查看 cer
- cer 存入 keystore
使用 keytool 命令过程中,有一些可选参数,参数默认值如下:
参数 | 默认值 | 描述 |
---|---|---|
-keystore | ${USER_HOME}/.keystore | keystore 文件路径 |
-storepass | - | keystore 文件密码,默认从终端读取 |
-keypass | - | alias 密码 |
-validity | 90 | 证书有效期,天 |
key 生成
# 生成名为 tmp.keystore 的 keystore 文件
keytool -genkeypair -keystore tmp.keystore -alias tmp \
-dname "CN=tmp.com,O=tmp" -keyalg RSA \
-keysize 2048 -storepass changeme -keypass changeme
key 查看
# 查看 tmp.keystore 中所有证书
keytool -list -keystore tmp.keystore -v
# 查看 tmp.keystore 中别名为 tmp 的证书
keytool -list -keystore tmp.keystore -v -alias tmp
# 查看 tmp.keystore 中别名为 tmp 的证书,密码为 changeme
keytool -list -keystore tmp.keystore -v -alias tmp -storepass changeme
keystore 密码修改
# 修改密码,需要在终端输入一次旧密码,两次新密码
keytool -storepasswd -keystore tmp.keystore
# 修改密码,changme -> changeme1
keytool -storepasswd -new changemex -keystore tmp.keystore -storepass changeme
alias 修改
keytool -changealias -keystore tmp.keystore -alias tmp -destalias tmp2 -storepass changemex
keystore 格式转换
# 将 tmp.keystore 的证书导入到新的类型为 jks 的 new.keystore 中
keytool -importkeystore -srckeystore tmp.keystore -destkeystore new.keystore -deststoretype jks
alias 密码修改
# 不支持 PKCS12 类型的修改,如果需要修改 PKCS12 类型,请参考前面的导入部分修改 keystore 类型
keytool -keypasswd -keystore new.keystore -alias tmp2 -storepass changemex -keypass changeme -new newkeypw
key 归集
# 将 tmp.keystore 中的 tmp2 导入到 new.keystore 中
keytool -importkeystore \
-srckeystore tmp.keystore -srcstoretype PKCS12 -srcstorepass changemex \
-keystore new.keystore -storepass changeme -deststoretype JKS \
-srcalias tmp2 -destalias key1
删除 key
# 删除 tmp.keystore 中名为 tmp2 的证书
keytool -delete -alias tmp2 -keystore tmp.keystore
申请 CA 认证
keytool -certreq -alias key1 -keystore new.keystore -storepass changeme | tee key.certreq
CA 生成证书
# cer
keytool -gencert -keystore new.keystore -alias key1 -infile key.certreq -outfile key.cer -validity 3650 -storepass changeme
# pem
keytool -gencert -keystore new.keystore -alias key1 -infile key.certreq -outfile key.pem -rfc -validity 3650 -storepass changeme
查看 cer
keytool -printcert -v -file key.cer
cer 加入 keystore
keytool -importcert -alias key_cer -file key.cer