1

我正在尝试在 openssl 中使用函数 RSA_private_decrypt 使用私钥解密。演示测试程序完成,但不幸的是它不起作用。所有代码都是从 openssl 中的原始测试模块修改的。

我检查了密钥和密文,一切正常。编译后运行程序报错:error:0406506C:lib(4):func(101):reason(108) 什么意思?如何找到原因?任何人都可以帮助我吗?谢谢大家!

/* test vectors from p1ovect1.txt */

#include <stdio.h>
#include <string.h>

#include "e_os.h"

#include <openssl/crypto.h>
#include <openssl/err.h>
#include <openssl/rand.h>
#include <openssl/bn.h>
#ifdef OPENSSL_NO_RSA
int main(int argc, char *argv[])
{
    printf("No RSA support\n");
    return(0);
}
#else
#include <openssl/rsa.h>

#define SetKey \
  BN_dec2bn(&key->n, n); \
  BN_dec2bn(&key->d, d); \
  BN_dec2bn(&key->e, e); \
  return 0;

int get_key(RSA *key)
{
    static unsigned char n[] =
"9371630458513538614700745634649520797491568155168133301060614397"
"9751660657428852845087672310448592787191681231125442581642251207"
"6274432436386486727760163436686441566403892007588636246674079539"
"8424907957591099362409156898826262775257410650392782996749570788"
"0595781666554070793222145163225943407723575126201759";

    static unsigned char e[] = "65537";

    static unsigned char d[] =
"1898149483594133261692443925634950319146483294806014944814053061"
"9149847316274937709472416531865886761023275045576683779067080313"
"8692140564270476598322907775584173165745869684564814756756282410"
"5523409115330652968833389931424803742277184063260858317510246331"
"1992939017567767983973542782514344223016146752560753";

    static unsigned char p[] =
"1898149483594133261692443925634950319146483294806014944814053061"
"9149847316274937709472416531865886761023275045576683779067080313"
"8692140564270476598322907775584173165745869684564814756756282410"
"5523409115330652968833389931424803742277184063260858317510246331"
"1992939017567767983973542782514344223016146752560753";

    static unsigned char q[] =
"\x00\xC9\x7F\xB1\xF0\x27\xF4\x53\xF6\x34\x12\x33\xEA\xAA\xD1\xD9"
"\x35\x3F\x6C\x42\xD0\x88\x66\xB1\xD0\x5A\x0F\x20\x35\x02\x8B\x9D"
"\x89";

    static unsigned char dmp1[] =
"\x59\x0B\x95\x72\xA2\xC2\xA9\xC4\x06\x05\x9D\xC2\xAB\x2F\x1D\xAF"
"\xEB\x7E\x8B\x4F\x10\xA7\x54\x9E\x8E\xED\xF5\xB4\xFC\xE0\x9E\x05";

    static unsigned char dmq1[] =
"\x00\x8E\x3C\x05\x21\xFE\x15\xE0\xEA\x06\xA3\x6F\xF0\xF1\x0C\x99"
"\x52\xC3\x5B\x7A\x75\x14\xFD\x32\x38\xB8\x0A\xAD\x52\x98\x62\x8D"
"\x51";

    static unsigned char iqmp[] =
"\x36\x3F\xF7\x18\x9D\xA8\xE9\x0B\x1D\x34\x1F\x71\xD0\x9B\x76\xA8"
"\xA9\x43\xE1\x1D\x10\xB2\x4D\x24\x9F\x2D\xEA\xFE\xF8\x0C\x18\x26";

  BN_dec2bn(&key->n, n);
  BN_dec2bn(&key->d, d);
  BN_dec2bn(&key->e, e);
  return 0;
}

static int pad_unknown(void)
{
    unsigned long l;
    while ((l = ERR_get_error()) != 0)
      if (ERR_GET_REASON(l) == RSA_R_UNKNOWN_PADDING_TYPE)
    return(1);
    return(0);
}

static const char rnd_seed[] = "string to make the random number generator think it has entropy";

int main(int argc, char *argv[])
    {
    int err=0;
    int v;
    RSA *key;
    unsigned char ptext[256];
    unsigned char err_msg[256];
    static unsigned char ptext_ex[] = "\x54\x85\x9b\x34\x2c\x49\xea\x2a";
    unsigned char ctext_ex[]=
    "11A10DBC32E75CD4A4A5FBDF79B9DCEB5FAA8CCA56925F8FFD09265CAE63"
    "02F4FAD8CF647D2C64107EEE254033B81DD22DACAE7ECBE873CD958D248C"
    "5F60CD223D5C963077406E398232CF4C36715FD63F640F1AF1E44E86D244"
    "6FB6B773A17B790938FAC5DFE9257639D60FB1FA4B2082098068549D37C6"
    "EBD5164E77A68876";
    int plen;
    int clen = 0;
    int num;
    int n;

    CRYPTO_malloc_debug_init();
    CRYPTO_dbg_set_options(V_CRYPTO_MDEBUG_ALL);
    CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);

    RAND_seed(rnd_seed, sizeof rnd_seed); /* or OAEP may fail */

    plen = sizeof(ptext_ex) - 1;


    key = RSA_new();
    clen = get_key(key);
    printf("clen[%d]\n", clen);
    key->flags |= RSA_FLAG_NO_CONSTTIME;

    key->p=NULL;
    key->q=NULL;
    key->dmp1=NULL;
    key->dmq1=NULL;
    key->iqmp=NULL;
    printf("n[%s]\n", BN_bn2dec(key->n));
    num = strlen(ctext_ex);
    printf("num[%d]\n", num);
    memset(ptext, '\0', sizeof(ptext));
    num = RSA_private_decrypt(num, ctext_ex, ptext, key,
                  RSA_PKCS1_PADDING);
    ERR_error_string(ERR_get_error(), err_msg);
    printf("num[%d], err[%s]\n", num, err_msg);
    for(v=0; v<6; v++) {
        printf("ptext%d[%d]\n", v, ptext[v]);
    }
    RSA_free(key);

    CRYPTO_cleanup_all_ex_data();
    ERR_remove_thread_state(NULL);

    CRYPTO_mem_leaks_fp(stderr);

#ifdef OPENSSL_SYS_NETWARE
    if (err) printf("ERROR: %d\n", err);
#endif
    return err;
    }
#endif
4

1 回答 1

3

呃……我终于找到原因了。密文需要先转换为 HEX。在我这样做之后,它工作得很好。这真是一个愚蠢的问题。

于 2012-09-06T07:40:12.340 回答