簡體   English   中英

如何使c openssl生成與JAVA SHA256withRSA / PSS簽名相同的簽名文本?

[英]how to make c openssl generate the same signed text as JAVA SHA256withRSA/PSS Signature?

我正在研究rpc調用簽名,為了使服務器接受我們的API調用,我們需要使用RSAPrivateKey來簽名http mime標頭。 服務器端代碼用JAVA編寫,並使用“ SHA256withRSA / PSS”來驗證簽名。

我的問題是我從JAVA和c openssl代碼中獲得了不同的簽名哈希。 所以問題是,openssl是否有可能生成與JAVA相同的簽名哈希?

JAVA代碼:

public static String getSignedString(PrivateKey privateKey, String text) throws Exception {
    Signature sig = Signature.getInstance("SHA256withRSA/PSS", "BC");
    sig.initSign(privateK);
    sig.update(text.getBytes(StandardCharsets.UTF_8));

    byte[] signed = sig.sign();

    String result = Base64.getEncoder().encodeToString(signed);
    System.out.println("signed : " + result);
    /// signed result works perfect.
}

C代碼:

bool RSASign(RSA* rsa, const unsigned char* Msg, size_t MsgLen,unsigned char** EncMsg,size_t* MsgLenEnc) {
EVP_MD_CTX* m_RSASignCtx = EVP_MD_CTX_create();
EVP_PKEY* priKey  = EVP_PKEY_new();
EVP_PKEY_CTX *pctx = EVP_PKEY_CTX_new(priKey, NULL);

EVP_PKEY_CTX_set_rsa_padding(pctx, RSA_PKCS1_PSS_PADDING);  
// tried RSA_PKCS1_PADDING w/o success.
EVP_PKEY_assign_RSA(priKey, rsa);
if (EVP_DigestSignInit(m_RSASignCtx,&pctx, EVP_sha256(), NULL,priKey)<=0) {
    return false;
}
if (EVP_DigestSignUpdate(m_RSASignCtx, Msg, MsgLen) <= 0) {
    return false;
}
if (EVP_DigestSignFinal(m_RSASignCtx, NULL, MsgLenEnc) <=0) {
    return false;
}
*EncMsg = (unsigned char*)malloc(*MsgLenEnc);
if (EVP_DigestSignFinal(m_RSASignCtx, *EncMsg, MsgLenEnc) <= 0) {
    return false;
}
// here EncMsg is different from JAVA output, server validation failed. need to make it the same as JAVA output.
}

“ RSA-PSS”中的“ PSS”代表概率簽名方案 -這是一種隨機算法。 您不應該每次都獲得相同的簽名。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM