openssl命令行验证签名


RSA签名验证的一般流程

发行者
  • 对被签名文件(假设为App.txt)计算hash,一般用SHA256
  • 用RSA私钥对hash值签名,对其结果进行base64编码,保存到签名文件(假设为App.sig)
  • 将App.txt,公钥文件(假设为App.pub),App.sig递交给用户
使用者
  • 对App.txt计算hash,一般用SHA256
  • 对App.sig进行base64解码,得到二进制签名(假设为App.sig.bin)
  • 使用hash,App.sig.bin,App.pub进行验证

使用openssl命令行实现,只讲用户验证过程

  • 对签名文件进行base64解码
    cat App.sig | base64 -d > App.sig.bin
  • 校验
    openssl dgst -sha256 -verify App.pub -signature App.sig.bin App.txt
  • 交校验成功会返回“Verified OK”

注意事项

  • 如果公钥为RSA PKCS#1格式,需要转换为RSA PKCS#8格式
    openssl rsa -RSAPublicKey_in -in App.pub -pubout > App.pub.8
  • PKCS#1格式的公钥以-----BEGIN RSA PUBLIC KEY-----开头
  • PKCS#8格式的公钥以-----BEGIN PUBLIC KEY-----开头

相关