1

我已经阅读了许多相关文章并尝试了编码示例,但我对 RSA_verify 的调用仍然失败。这是我的详细信息:

我正在编写一个分多个阶段执行的应用程序。在第一阶段从 .pub 文件中读取公钥(2048 位),但只有无符号字符数组中的模数和指数被传递到后续阶段(这些后续阶段不知道原始密钥文件是什么)。

在其中一个阶段中,我必须根据由私钥(由单独的应用程序)签名的签名来验证哈希,但我所要做的只是用于 mod 和 exp 的 unsigned char 数组。我尝试创建 RSA 结构,然后将其传递给 RSA_verfiy 函数(连同签名),但出现以下错误:

5040:error:0306E06C:bignum routines:BN_mod_inverse:no inverse:.\crypto\bn\bn_gcd.c:492:

这是我的代码的复制品:

bool RSAVerifySig( uchar *hash, uint hashSize,
                   uchar *sig, uint sigSize,
                   uchar *mod, uchar *exp )
{
    RSA     *rsa = RSA_new();
    BIGNUM  *n = BN_new();
    BIGNUM  *e = BN_new();

    BN_bin2bn( mod, 256, n );
    BN_copy( rsa->n, n );
    BN_bin2bn( exp, 4, e );
    BN_copy( rsa->e, e );
    rsa->iqmp = NULL;
    rsa->d = NULL;
    rsa->p = NULL;
    rsa->q = NULL;
    ret = RSA_verify( EVP_sha256()->type, hash, hashSize, sig, sigSize, rsa );
    if( 0 != ERR_peek_error() )
    {
            SSL_load_error_strings();
            ERR_print_errors_fp( stdout );
            return( false );
    }
    return( ret == 1 );
}

几天来我一直在努力解决这个问题。如果有人可以提供帮助,将不胜感激。

4

0 回答 0