打开SSL数字签名无法加载密钥文件

时间:2019-07-06 12:03:40

标签: ssl openssl digital-signature

我正在寻找一个创建文档,对文档进行数字签名并进行验证的示例。一切正常,直到我尝试验证签名,我得到的只是unable to load key file

创建一个需要协议(签名)的文档:

echo I, Bob, promise to pay Mark £1000 by 1/1/2020 > contract.txt

生成私钥:

openssl genpkey -algorithm RSA -out private_key.pem -pkeyopt rsa_keygen_bits:2048

从私钥生成公钥:

openssl rsa -in private_key.pem -RSAPublicKey_out -out public_key.pem

对文档进行数字签名:

openssl dgst -sha256 -hex -sign private_key.pem -out signature.sign contract.txt

然后,如果我们查看signature.sign的内容:

RSA-SHA256(contract.txt)= 2f5dc8216766562a9fb67a7b09b43b599889e7adea3d4d508194018961a82a9076051ee3c3952af9dbd607cbfe1095976ec5e877e22c0e4a884003ebef672f9a3e598128f819435a178c92ad10e4a409dc28db6e6500dfcee6a58e352446c354dec0852d6d826ee443fe158e6c30a231d30eb00e03c21a3e98855445bcc43a000f205b44ea8fc2f4ed85cd7c03c5708d649ef9a7d737b0948b9bdba322868e18492446eac054e2d4a31f0fa9bfccc627b621da0a9a261fb6169c1f107ec0311844151e77e50aeedb1be860c2b0b58f077c2886f9a7f05e727c0f9d4cc24d668f96bf7d6a2fff40a4b14951e745847c13812b35df95f91d202df0ef6ea5a05078

要验证签名:

openssl dgst -sha256 -hex -verify public_key.pem -signature signature.sign contract.txt

完整脚本:

echo I, Bob, promise to pay Mark £1000 by 1/1/2020 > contract.txt
openssl genpkey -algorithm RSA -out private_key.pem -pkeyopt rsa_keygen_bits:2048
openssl rsa -in private_key.pem -RSAPublicKey_out -out public_key.pem
openssl dgst -sha256 -sign private_key.pem -out signature contract.txt
openssl dgst -sha256 -verify public_key.pem -signature signature contract.txt

谢谢!

0 个答案:

没有答案