是否有一种方法(例如使用openssl命令)来检查给定的公钥是否与私钥匹配?
谢谢
发布于 2020-05-03 12:35:12
一种方法适用于任何签名方案和包括OpenSSL在内的任何程序:使用私钥对文件进行签名,并根据公钥检查签名和文件。
如果检查正常,则私钥和公钥匹配(或签名方案被破坏)。
如果出现某些故障,则私钥和公钥不匹配(或者程序被误用,无法与密钥格式兼容,或者损坏)。
例如,使用openssl,显示Verified OK
表示匹配的私钥/公钥tstpri.pem
/tstpub.pem
;显示Verification Failure
表示不匹配密钥altpri.pem
/tstpub.pem
。
# generate an secp256k1 private key
openssl ecparam -genkey -noout -name secp256k1 -out tstpri.pem
# extract the public key
openssl ec -in tstpri.pem -pubout -out tstpub.pem >/dev/null
# generate an alternate secp256k1 private key
openssl ecparam -genkey -noout -name secp256k1 -out altpri.pem
# create a file fil (any content will do)
echo foo >fil
# sign fil with private key
openssl dgst -sha256 -sign tstpri.pem -out tst.sig fil
# verify the signature with matching public key
openssl dgst -sha256 -verify tstpub.pem -signature tst.sig fil
Verified OK
# sign fil with alternate private key
openssl dgst -sha256 -sign altpri.pem -out tst.sig fil
# verify the signature with non-matching public key
openssl dgst -sha256 -verify tstpub.pem -signature tst.sig fil
Verification Failure
发布于 2020-05-04 00:25:18
假设您的EC私钥文件为pem格式(private.key),EC公钥文件为pem格式(publick.key),则可以通过从私钥文件派生公钥来验证这两个文件是否匹配,如下所示:
openssl ec -pubout -in private.key
此命令的输出应该与public.key的内容相匹配。
https://crypto.stackexchange.com/questions/80424
复制相似问题