【问题标题】:verify openssl signature using crypto++ library使用 crypto++ 库验证 openssl 签名
【发布时间】:2014-01-15 19:33:51
【问题描述】:

我正在尝试使用 crypto++ 验证 open-ssl 签名,这里是 open-ssl 部分:

$ openssl genrsa 1024 >priv.pem

$ openssl rsa -in priv.pem -pubout -out pubkey.pem

$ openssl dgst -sha1 -sign privkey.pem data.txt > sign

$ openssl dgst -sha1 -verify pubkey.pem -signature sign data.txt
  Verified OK

现在在我的 C++ 代码中:

int main(int argc, char* argv[])
{
    try
    {
        RSA::PublicKey publicKey;
        const char * pubFilename = "pubkey.pem";
    FileSource pubFile(pubFilename,true,new PEMStripper(new Base64Decoder()));
    RSASSA_PKCS1v15_SHA_Verifier pub(pubFile);
        cout << "n: " << pub.GetTrapdoorFunction().GetModulus() << endl;
        cout << "e: " << pub.GetTrapdoorFunction().GetPublicExponent() << endl;
        string message = "data that is to be hashed and signed.";  //same as data.txt

        ifstream file ("sign", ios::in | ios::binary | ios::ate);
    ifstream::pos_type sigSize;
    char* sig;
    if(file.is_open())
    {
          sigSize = file.tellg();
          sig = new char[sigSize];
          file.seekg(0, ios::beg);
          if(!file.read(sig, sigSize))
          {
          cout << "fail to read" << endl;
          }
          file.close();
    }
        bool result = pub.VerifyMessage( (const byte*)message.c_str(),
            message.length(), (const byte*)sig, sigSize );

        // Result
        if( true == result ) {
            cout << "Signature on message verified" << endl;
        } else {
            cout << "Message verification failed" << endl;
        }

    } // try

    catch( CryptoPP::Exception& e ) {
        std::cerr << "Error: " << e.what() << std::endl;
    }

    return 0;
}

但我总是收到消息验证失败

【问题讨论】:

标签: c++ c encryption openssl rsa


【解决方案1】:

您确定data.txt 不包含结尾的\n?

如果不是这样,在这一行中的字符串 lietrale 中添加一个,如下所示:

string message = "data that is to be hashed and signed.\n";  //same as data.txt

【讨论】:

  • 非常感谢!我正要拔头发。
【解决方案2】:

Openssl 采用有限长度的字符串数据,因此首先使用 sha1 进行哈希处理,然后将数据发送到 opensssl

【讨论】:

    猜你喜欢
    • 2011-06-24
    • 1970-01-01
    • 2020-06-11
    • 2017-03-24
    • 1970-01-01
    • 1970-01-01
    • 2014-03-22
    • 2015-04-18
    • 1970-01-01
    相关资源
    最近更新 更多