2012-06-21 58 views
3

如何通過crypto ++庫驗證數字簽名?使用加密++庫進行RAW RSA簽名驗證

輸入數據是:

  • PUBLIC_KEY BASE64編碼十六進制字符串。
  • 公開指數來自公鑰。
  • 簽名爲十六進制字符串。

我不知道私鑰部分。 我已經寫了這個測試函數,但總是以「VerifierFilter:數字簽名無效」錯誤結束。

這裏的關鍵是從有效的KeyPair導出的!

void rawRSAVerificationTest() 
{ 

// RSA 2048 digital signature verification 
try { 

    std::string pupKeyStr ("e0c3851114c758fb943a30ca8f9b4c7f506d52aa101c34ad5134f2cdbdddbef11ee6d1470d54caf3774d4d17d69488abf0b78beaebc046115cb29617610e98be3d5c303e19c14ae57ce0994701e3f24a628abf5c421777fb3c2b9b8b17f2a4cda4b9fd89e3c122085831c3e4502734bc3f3157d3ccd01198a8e3795f03661b55112acb69e8d5782bdf506bf5222777baf382d4d4bc2dd83e53af9236ed6e7a0fb8b5bb543ed4abbf478911bdc517e13e580b138f10f153eb2733ad60f3796e99b7d59f9abbd6666c69ba5ecc17a391424dc8ca3cf24a759c62d490056cda30265e11e316e7695028721c50eaf8e596161f0b59e4f598c85063bb3a847a5acb9d"); 
    //sha256 hashed data signature 
    std::string signatureStr = "d8d1df600d6781a9c030d9c697ec928eac34bf0670cf469f7fffb9c046deaee359b4e1218b419ff2589434510db8470ccf7abd28876a04b8d5a27293723e897f97a9367d2fbb379f8c52ec2a04cb71a06891a3f44d00e8bb9622b2038dbe6f29d0118203c372853ae09fb820702f1c16ee772998bd8a3db9e5127992a18d999fc422822caf0a82d9c12d6231605457ce651b0350b1e98584f9d4e6b973d6668df863d4b73784bbc00d8449918a0f049ddbeffc0d79579ade13a2d9012906b7ded7aae934bc54c5b85c924aee6627d66b7b200a23cd9b6a9c14650f1f9384e9ef9b90ac217ece026a1802bc0623150057ecd2b31f5f758e4ff866bb2e81d28368"; 

    //Chinese Remainder Theorem (CRT) 
    std::string pupExpStr ("0x10001"); 

    CryptoPP::AutoSeededRandomPool rng; 
    CryptoPP::RSA::PublicKey pubKeyRaw; 

    CryptoPP::Integer pup_key_cast(static_cast<CryptoPP::Integer> (pupKeyStr.c_str())); 
    CryptoPP::Integer pup_exp_cast(static_cast<CryptoPP::Integer> (pupExpStr.c_str())); 

    pubKeyRaw.Initialize(pup_key_cast, pup_exp_cast); 
    if (!pubKeyRaw.Validate(rng, 3)) 
    { 
     std::cout << "Error while public key validation" << std::endl; 
    } 

    CryptoPP::RSASS<CryptoPP::PSS, CryptoPP::SHA256>::Verifier verifier_sha256(pubKeyRaw); 


    CryptoPP::StringSource(signatureStr, true, 
     new CryptoPP::SignatureVerificationFilter( 
     verifier_sha256, NULL, 
     CryptoPP::SignatureVerificationFilter::THROW_EXCEPTION 
     ) // SignatureVerificationFilter 
     ); // StringSource 

} 
catch(CryptoPP::Exception& e) 
{ 
    std::cerr << "ERROR: " << e.what() << std::endl; 
} 
catch(...) 
{ 
    std::cerr << "ERROR: Unknown verify signature error" << std::endl; 
} 
} 

我錯過了什麼?

我將非常感謝您的幫助! 在此先感謝!

+0

「raw」是什麼意思?哪個RSA簽名方案被用來創建簽名? PSS? –

+0

這看起來更像是一個十六進制編碼模數。它的大小恰好是512個字符(即256個字節或2048個位)。 「演員」聲明做了什麼? –

+0

我的意思是RAW RSA:使用帶有RSA原語的Crypto ++執行加密和解密操作。 – Diethard

回答

2

如何通過crypto ++庫驗證數字簽名?

那麼,這是一個很容易,當你知道在哪裏看。從加密+維基上RSA Signature Schemes

RSA::PrivateKey privateKey = ...; 
RSA::PublicKey publicKey = ...; 

//////////////////////////////////////////////// 
// Setup 
string message = "RSA-PSSR Test", signature, recovered;  

//////////////////////////////////////////////// 
// Sign and Encode 
RSASS<PSSR, SHA1>::Signer signer(privateKey); 

StringSource ss1(message, true, 
    new SignerFilter(rng, signer, 
     new StringSink(signature), 
     true // putMessage for recovery 
    ) // SignerFilter 
); // StringSource 

//////////////////////////////////////////////// 
// Verify and Recover 
RSASS<PSSR, SHA1>::Verifier verifier(publicKey); 

StringSource ss2(signature, true, 
    new SignatureVerificationFilter(
     verifier, 
     new StringSink(recovered), 
     THROW_EXCEPTION | PUT_MESSAGE 
    ) // SignatureVerificationFilter 
); // StringSource 

cout << "Verified signature on message" << endl; 
cout << "Message: " << recovered << endl; 

CryptoPP::RSASS<CryptoPP::PSS, CryptoPP::SHA256>::Verifier verifier_sha256(pubKeyRaw); 

幾個問題:

  • 如果是這樣PSS(帶附屬的簽名方案)或PSSR(簽名與恢復方案)?
  • 你確定它不是,例如PKCS1v15?
  • 如果是恢復方案,是在十六進制編碼的開頭還是末尾的簽名signatureStr

這種原始Integer初始化可能是錯誤的。 Crypto ++會嘗試將字符串解析爲十進制整數,而不是十六進制整數,因爲缺少前綴0x和後綴h(應存在一個或另一個)。

您應該添加前綴,後綴或使用字節數組構造函數。字節數組的構造函數如下所示。

std::string pupKeyStr ("e0c3851114c758fb..."); 

string pupKeyBin; 
StringSource ss1(pupKeyStr, true, 
    new HexDecoder(
     new StringSink(pupKeyBin) 
    ) 
); 

CryptoPP::Integer pup_key_cast((unsigned char*)pupKeyBin.data(), pupKeyBin.size()); 

StringSource(signatureStr, true, ... 
... 

這可能是錯誤的。你可能需要這樣的東西:

string signatureBin; 
StringSource ss1(signatureStr, true, 
    new HexDecoder(
     new StringSink(signatureBin) 
    ) 
); 

StringSource ss2(signatureBin, true, 
    new SignatureVerificationFilter(... 
    ... 

我同時使用PSSAPSSR嘗試過的代碼與二進制簽名。都沒有工作。我也試過SignatureVerificationFilter's SIGNATURE_AT_BEGIN and SIGNATURE_AT_END。都沒有工作。我嘗試了與SHA1RSASSA_PKCS1v15_SHA_Verifier的組合。沒有工作。

你能確切地證實你有什麼嗎?

相關問題