Android

Android 使用keytool生成keystore

2022-06-14  本文已影响0人  Jey

py生成keystore

# 查询
keytool -help
keytool -genkey -help
import os
import sys
import os.path
from os.path import join, getsize

import random
import string

chars = "abcdefghijklmnopqrstuvwxyz"
BChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"

key_alias = "key0"
key_pw = "123456"

# 生成随机字符串,str_size:产生随机的长度 | allowed_chars:用于生成的字符串合集
def random_string_generator(str_size, allowed_chars):
    return ''.join(random.choice(allowed_chars) for x in range(str_size))

def random_int_generator(min, max):
    return random.randint(min, max)

def createKeystore(startChannelId, endChannelId):
    keystoreName = "key-" + str(startChannelId) + ".keystore"
    # 有效年限
    validity = 365 * random_int_generator(20, 50)
    # CN=wu, OU=xxx公司, O=xxx组织, L=北京, ST=京, C=86是否正确?
    firstName = random_string_generator(1, BChars) + random_string_generator(random_int_generator(2, 6), chars)
    organization_Unit = random_string_generator(1, BChars) + random_string_generator(random_int_generator(2, 6), chars)
    organization = random_string_generator(1, BChars) + random_string_generator(random_int_generator(2, 6), chars)
    city_locality = random_string_generator(1, BChars) + random_string_generator(random_int_generator(2, 6), chars)
    state_province = random_string_generator(1, BChars) + random_string_generator(random_int_generator(2, 6), chars)

    cmd = 'keytool -genkeypair -v -keystore %s -alias %s -storetype PKCS12 -keyalg RSA -keysize 2048 -validity %s -dname "CN=%s,OU=%s,O=%s,L=%s,ST=%s,C=" -keypass %s -storepass %s' % (keystoreName, key_alias, validity, firstName, organization_Unit, organization, city_locality, state_province, key_pw, key_pw)
    print("cmd = {}\n".format(cmd))
    # keytool -genkey -alias key123 -keyalg RSA -validity 36500 -keystore test.keystore -dname "CN=xx,OU=cc,O=vv,L=bb,ST=nn,C=mm" -keypass 111111 -storepass 111111
    os.system(cmd)
    print("\n")
    if endChannelId == 0:
        exit()

    if int(startChannelId) < int(endChannelId):
        startChannelId = int(startChannelId) + 1
        createKeystore(startChannelId, endChannelId)

if __name__ == "__main__":
    resPath = os.getcwd()
    print ("当前目录=== {}\n".format(resPath))

    endChannelId=0
    if len(sys.argv) >= 3:
        endChannelId = sys.argv[2]

    startChannelId = 1000
    if len(sys.argv) >= 2:
        startChannelId = sys.argv[1]

    createKeystore(startChannelId, endChannelId)

查询keystore信息

# 查询所有
keytool -list -v -keystore ${keystore_file} -storepass 密码

# 查询前20行
keytool -list -v -keystore ${keystore_file} -storepass 密码 | head -n 20

生成散列

keytool -exportcert -alias 别名 -keystore ${keystore_file} -storepass 密码 | openssl sha1 -binary | openssl base64
上一篇 下一篇

猜你喜欢

热点阅读