首页 > 解决方案 > 将文件保存为 .pem 从 powershell 5.1 编码为 utf8noBOM 并使用 openssl 读取它会引发错误

问题描述

我正在尝试使用 powershell 将 .pem 文件上传到 API,我们必须将其转换为 JSON 格式然后发布。好吧,使用下面的代码片段可以正常工作

$t = Get-Content -Delimiter "`n" -Path 'xxxxxx.pem'
$a = [pscustomobject]@{"certificate" = "$($t)" 
                       "description" = "" 
                       "extension" = ".pem"}
$body = $a | convertto-json
$web = Invoke-WebRequest -Uri $url -ContentType "application/json" -Headers $headers -Body $body -Method POST

检索时,我正在使用以下代码段

$web = Invoke-WebRequest -Uri $url -ContentType "application/json" -Headers $headers
$temp = $web.Content | ConvertFrom-Json
$cert = $temp.data.certificate
$Utf8NoBomEncoding = New-Object System.Text.UTF8Encoding $False
[System.IO.File]::WriteAllLines("*****.pem", $cert, $Utf8NoBomEncoding)

然后我将使用上述代码段创建的文件传递给 openssl,如下

openssl.exe x509 -in "*****.pem" -checkend 129600

这给我带来了以下错误,

openssl.exe : unable to load certificate
At line:1 char:1
+ openssl.exe x509 -in "******.pem" -checkend 129600
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (unable to load certificate:String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError

14660:error:0909006C:PEM routines:get_name:no start line:crypto\pem\pem_lib.c:745:Expecting: TRUSTED CERTIFICATE

我在这里很困惑,不知道我做错了什么。

因为这两个文件,原始 .pem 文件和从 PS 创建的文件都是 utf-8 编码的,并且在 notepad++ 中看不到任何 BOM 字符。

标签: jsonpowershellapiopensslbyte-order-mark

解决方案


推荐阅读