0

I am trying to verify open-ssl signature using crypto++, here is the open-ssl part:

$ 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:

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

1

2 Answers 2

1

Are you sure data.txt doesn't contain a final trailing \n?

If not so append one to the string lietrale in this line like so:

string message = "data that is to be hashed and signed.\n";  //same as data.txt
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a ton! I was about to pull out my hair.
0

Openssl take limited length of string data so first hash using sha1 and then send data to opensssl

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.