570
文章
·
29362
阅读
570
文章
·
29362
阅读

有19人阅读过 使用openssl创建安卓应用的证书,格式*.pk8和*.pem 【windows】
发布于2023/11/28 更新于2023/11/29
[ 教程仅保证更新时有效,请自行测试。]

  1. 准备必要软件

    生成高版本证书v1,v2,v3:Win64OpenSSL_Light-3_1_4.msi

    下载地址:https://slproweb.com/download/Win64OpenSSL_Light-3_1_4.exe

    生成低版本证书v1:Win64OpenSSL_Light-1_1_1w

    下载地址:https://slproweb.com/download/Win64OpenSSL_Light-1_1_1w.exe

  2. 安装后,将路径添加到系统变量中

    image.png

  3. 验证一下环境变量是否设置生效

    win+X,选择 终端管理员,

    image.png

    输入openssl version, 看是否正常返回版本信息。

    image.png

  4. 以下开始生成证书:

  5. 创建一个文件夹,用于保存生成的文件,在文件夹中右击,选择 在终端中打开

    image.png

  6. 生成root.key

  7. openssl genrsa -out root.key 2048

    image.png

  8. 生成root.cer

  9. openssl req -new -x509 -days 36500 -key root.key -out root.crt -subj "/C=CN/ST=Guangdong/L=Shenzhen/O=giraff/OU=giraff/CN=giraff"

    image.png

    说明:

    -subj拥有者信息,CN:姓名;OU:组织单位名称;O:组织名称;L:省/市/自治区名称;C:国家/地区代码

    days 36500 此处为天数(100年) 

  10. 证书转换:

  11. openssl x509 -in root.crt -out root.pem
    openssl x509 -in root.crt -out root.cer -outform der
    openssl pkcs12 -export -in root.crt -inkey root.key -out root.p12 -name root

    最后一步需要输入(设置)一个密码,回车后再输入一次确认。

    image.png

  12. 提取以下*.p12文件中的信息

  13. openssl pkcs12 -in root.p12 -nodes -out tmp.rsa.pem

    输入密码确认

    image.png

  14. 这个时候目录中有以下文件:

    image.png

  15. 使用文本工具打开文件 tmp.rsa.pem

    里面的内容如下

    image.png

  16. 创建一个文件 cert.x509.pem,把上一步文件中-----BEGIN CERTIFICATE-----这一块的信息复制进去,以UTF-8编码格式保存

    image.png

  17. 创建一个文件private.rsa.pem,把-----BEGIN PRIVATE KEY-----这一块的内容复制进去,以UTF-8编码格式保存

    image.png

  18. 11-13步可一键执行:


  19. $inputFile = "tmp.rsa.pem"
    $outputCertFile = "cert.x509.pem"
    $outputPrivateKeyFile = "private.rsa.pem"
    $startCert = "-----BEGIN CERTIFICATE-----"
    $endCert = "-----END CERTIFICATE-----"
    $startPrivateKey = "-----BEGIN PRIVATE KEY-----"
    $endPrivateKey = "-----END PRIVATE KEY-----"
    
    $certContent = Get-Content $inputFile -Raw | Select-String -Pattern "(?s)$startCert.*?$endCert" | ForEach-Object { $_.Matches.Value }
    $privateKeyContent = Get-Content $inputFile -Raw | Select-String -Pattern "(?s)$startPrivateKey.*?$endPrivateKey" | ForEach-Object { $_.Matches.Value }
    
    $certContent | Out-File -Encoding UTF8 $outputCertFile
    $privateKeyContent | Out-File -Encoding UTF8 $outputPrivateKeyFile
    
    Write-Host "Certificate content extracted and saved to $outputCertFile"
    Write-Host "Private key content extracted and saved to $outputPrivateKeyFile"

    执行后手动将两个文件编码格式转存成UTF-8编码

  20. 生成pk8格式的私钥

  21. openssl pkcs8 -topk8 -outform DER -in private.rsa.pem -inform PEM -out private.pk8 -nocrypt

    image.png

  22. 最终获得文件如下:

    实际我们需要的文件是 cert.x509.pem 和 private.pk8,其他的都可以删掉

    image.png

  23. 简单流程

  24. openssl genrsa -out root.key 2048
    # 下面一行修改成自己的信息
    openssl req -new -x509 -days 36500 -key root.key -out root.crt -subj "/C=CN/ST=Guangdong/L=Shenzhen/O=giraff/OU=giraff/CN=giraff"
    # 创建中间文件
    openssl x509 -in root.crt -out root.pem
    openssl x509 -in root.crt -out root.cer -outform der
    openssl pkcs12 -export -in root.crt -inkey root.key -out root.p12 -name root
    openssl pkcs12 -in root.p12 -nodes -out tmp.rsa.pem
    # 提取内容
    $inputFile = "tmp.rsa.pem"
    $outputCertFile = "cert.x509.pem"
    $outputPrivateKeyFile = "private.rsa.pem"
    $startCert = "-----BEGIN CERTIFICATE-----"
    $endCert = "-----END CERTIFICATE-----"
    $startPrivateKey = "-----BEGIN PRIVATE KEY-----"
    $endPrivateKey = "-----END PRIVATE KEY-----"
    $certContent = Get-Content $inputFile -Raw | Select-String -Pattern "(?s)$startCert.*?$endCert" | ForEach-Object { $_.Matches.Value }
    $privateKeyContent = Get-Content $inputFile -Raw | Select-String -Pattern "(?s)$startPrivateKey.*?$endPrivateKey" | ForEach-Object { $_.Matches.Value }
    $certContent | Out-File -Encoding UTF8 $outputCertFile
    $privateKeyContent | Out-File -Encoding UTF8 $outputPrivateKeyFile
    Write-Host "Certificate content extracted and saved to $outputCertFile"
    Write-Host "Private key content extracted and saved to $outputPrivateKeyFile"
    # 手动转存编码格式
    openssl pkcs8 -topk8 -outform DER -in private.rsa.pem -inform PEM -out private.pk8 -nocrypt
    # 完工

文章对你有帮助吗?
  • 一般[0]
  • 很赞[0]
  • 没用[0]
  • 垃圾[0]
  • 无语[0]

继续阅读:

扫一扫,手机浏览手机访问本站