打印MPI的unsigned int(基数10 /十进制)值

时间:2013-02-26 02:52:05

标签: c encryption libgcrypt

我正在尝试确认/获取hex'dmpi值的unsigned int(基数10 /十进制)值。我正在使用以下代码:

#include <gcrypt.h>
#include <stdio.h>

static gcry_mpi_t privkey = NULL;

int main(){

    char *parmstr = "48BFDA215C31A9F0B226B3DB11F862450A0F30DA";

    printf("starting program\n");

    gcry_mpi_scan(&privkey, GCRYMPI_FMT_HEX, (char *)parmstr, 0, NULL);

    printf("printing hashed (?) mpi\n");
    gcry_mpi_dump(privkey);
    printf("\n");

    size_t gablen;
    unsigned char *result;
    /* get length */
    gcry_mpi_print (GCRYMPI_FMT_USG, NULL, 0, &gablen, privkey);
    result = gcry_malloc_secure(gablen);
    /* get actual value */
    gcry_mpi_print (GCRYMPI_FMT_USG, result, gablen, NULL, privkey);

    /* print actual value */
    printf("result: %s\n", result);

    printf("finished\n");
}

我得到以下结果:

    $ ./foo
    starting program
    printing hashed (?) mpi
     48BFDA215C31A9F0B226B3DB11F862450A0F30DA
    result: H¿Ú!\1©ð²&³ÛøbE
0Ú
    finished

我会'result:'行打印实际的unsigned int(基数10 /十进制)值。

私钥取自我正在尝试使用的Off-The-Record Pidgin插件。

修改

任何人都可以确认实际的unsigned int(基数10 /十进制)值应该是

  

415325779662433871844955547383752003988573073626

我可能会更新程序,使用此值在gcrypt中创建一个新的mpi,看看HEX值是否与我已有的相同。我今天晚些时候会这样做。

编辑2

所以我正在尝试执行以下操作来打印上面提到的int值的HEX值。出了点问题:

gcry_mpi_t cript_prime;
char buffer[50] = {0};
char number[50] = {0};

cript_prime = gcry_mpi_new(50);

strcpy(number,"415325779662433871844955547383752003988573073626");
gcry_mpi_scan(&cript_prime,GCRYMPI_FMT_USG,number,sizeof(number),NULL);

gcry_mpi_print(GCRYMPI_FMT_USG,buffer,sizeof(buffer),NULL,cript_prime);

printf("The number tested is: %s\n",buffer);

printf("trying to convert to HEX\n");

/* get actual value */
gcry_mpi_print (GCRYMPI_FMT_HEX, buffer, sizeof(buffer), NULL, cript_prime);
/* print actual value */
printf("result: %s\n", buffer);

输出结果为:

result: 48BFDA215C31A9F0B226B3DB11F862450A0F30DA
The number tested is: 415325779662433871844955547383752003988573073626
trying to convert to HEX
result: 415325779662433871844955547383752003988573073626

编辑3

我稍微更新了帖子,基本上我正在尝试打印由gcrypt库生成的十六进制值的base10十进制值。我正在寻找这个值来确认我读取这些值的实现。我正在寻找一个gcrypt函数来实现这一目标。看来gcrypt不支持这个吗?

2 个答案:

答案 0 :(得分:1)

result[1] = (gablen >> 24) & 0xff;

该行将nul字节放入偏移量为1的字节result。我没有看到你在偏移零处初始化字节的位置,但偏移2和更高的字节超过了你构造的字符串的结尾。它的格式化字符串从结果+ 5开始,所以你应该在这里打印:

gcry_mpi_print (GCRYMPI_FMT_USG, result + 5, gablen, NULL, privkey);
...
printf("result: %s\n", result + 5);

答案 1 :(得分:1)

这是一个工作脚本,缓冲区太小了

#include <gcrypt.h>
#include <stdio.h>

int main(){
    gcry_error_t err;
    gcry_mpi_t cript_prime;
    unsigned char buffer[401] = {0};
    char number[101] = {0};

    cript_prime = gcry_mpi_new(101);

    strcpy(number, "415325779662433871844955547383752003988573073626");
    gcry_mpi_scan(&cript_prime, GCRYMPI_FMT_USG, number, sizeof(number), NULL);

    err = gcry_mpi_print(GCRYMPI_FMT_USG, buffer, sizeof(buffer), NULL, cript_prime);

    if (err != 0){
      printf("error: %d\n", err);
      return -1;
    }

    printf("The number tested is: %s\n",buffer);

    printf("trying to convert to HEX\n");

    /* get actual value */
    err = gcry_mpi_print(GCRYMPI_FMT_HEX, buffer, sizeof(buffer), NULL, cript_prime);

    if (err != 0){
      gcry_err_code_t code = gcry_err_code(err);
      printf("error: %d\n", code);
      return -1;
    }

    /* print actual value */
    printf("result: %s\n", buffer);

    printf("finished\n");

    return 0;
}

我也使用这个网站确认了我的想法:http://www.unitconversion.org/numbers/base-10-to-base-16-conversion.html

这将显示hex(base-16)48BFDA215C31A9F0B226B3DB11F862450A0F30DA实际为415325779662433762080404206464628666084640806848(base-10)