简体   繁体   English

使用crypto ++库验证openssl签名

[英]verify openssl signature using crypto++ library

I am trying to verify open-ssl signature using crypto++, here is the open-ssl part: 我正在尝试使用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

Now in my C++ code: 现在在我的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;
}

But I always get Message verification failed 但我总是收到消息验证失败

Are you sure data.txt doesn't contain a final trailing \\n ? 您确定data.txt不包含结尾的\\n吗?

If not so append one to the string lietrale in this line like so: 如果不是这样,请在此行中在字符串lietrale后面附加一个,如下所示:

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM