PowerShell管道传递OpenSSL加密数据出现解密错误的问题
PowerShell管道传递OpenSSL加密数据时的解密错误问题
在PowerShell脚本中,尝试将经public.pem加密的数据从变量通过管道传入OpenSSL,使用private.pem解密时,始终出现data greater than mod len错误。已知该错误的通用含义,但此场景下报错不符合预期,相关示例如下:
可行验证示例
Set-Location $workingPath # 该路径下存放private.pem和public.pem # 此代码可正常运行 ("Hello World for File" | openssl rsautl -encrypt -inkey .\public.pem -pubin -out .\test.enc) (openssl rsautl -decrypt -inkey .\private.pem -in .\test.enc)
上述代码可正常执行,解密命令能成功还原原始字符串,说明加解密逻辑及密钥均有效,且OpenSSL可通过标准输入接收数据。
出现错误的示例
# 这些代码会产生'data greater than mod len'错误 $enc = ("Hello World for variable" | openssl rsautl -encrypt -inkey .\public.pem -pubin) ($enc | openssl rsautl -decrypt -inkey .\private.pem) (echo $enc | openssl rsautl -decrypt -inkey .\private.pem) (echo "$enc" | openssl rsautl -decrypt -inkey .\private.pem)
移除-out参数后,加密结果会输出到标准输出并存入变量$enc,但将$enc通过管道传入OpenSSL解密时,始终出现错误:
error:0406506C:rsa routines:RSA_EAY_PRIVATE_DECRYPT:data greater than mod len:rsa_eay.c:508:
读取文件数据管道解密也报错
# 同样产生'data greater than mod len'错误 ("Hello World for File for read" | openssl rsautl -encrypt -inkey .\public.pem -pubin -out .\test.enc) $fileData = (Get-Content .\test.enc) (Get-Content .\test.enc | openssl rsautl -decrypt -inkey .\private.pem) ($filedata | openssl rsautl -decrypt -inkey .\private.pem)
即使先将加密数据写入文件再读取,直接管道传递或存入变量后管道传递,都会出现相同错误。尝试过先将加密数据Base64编码再解码后解密,但无论何种方式,管道传递数据解密时都会报错。密钥对是通过OpenSSL生成的4096位RSA密钥。
AES加密也存在类似问题
$symKey = '33333333333333333333222222222222' $symIV = '1111111111666666' # 此代码可正常运行 ("Hello AES" | openssl enc -aes-256-cbc -K $symKey -iv $symIV -out testAES.enc ) (openssl enc -aes-256-cbc -d -K $symKey -iv $symIV -in .\testAES.enc) # 产生"bad decrypt error"错误 # 83764:error:0606506D:digital envelope routines:EVP_DecryptFinal_ex:wrong final block length:evp_enc.c:460: $encAES = ("Hello AES" | openssl enc -aes-256-cbc -K $symKey -iv $symIV ) ($encAES | openssl enc -aes-256-cbc -d -K $symKey -iv $symIV )
内容的提问来源于stack exchange,提问作者George Frazee
相关产品推荐
相关产品推荐

