使转换类似于在C中没有openSSL的BN_hex2bn + BN_bn2bin [英] Making conversion similar to BN_hex2bn + BN_bn2bin without openSSL in C

查看:1101
本文介绍了使转换类似于在C中没有openSSL的BN_hex2bn + BN_bn2bin的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前使用openSSL将值从加密的字符串转换为我认为是二进制数组的值.然后,我解密此数组"(传递给EVP_DecryptUpdate).我这样进行转换:

I currently use openSSL to convert values from encrypted string to what I thought was a binary array. I then decrypt this "array" (pass to EVP_DecryptUpdate). I make the conversion like this:

    BIGNUM *bnEncr = BN_new();
    if (0 == BN_hex2bn(&bnEncr, encrypted)) { // from hex to big number
        printf("ERROR\n");
    }
    unsigned int numOfBytesEncr = BN_num_bytes(bnEncr);
    unsigned char encrBin[numOfBytesEncr];
    if (0 == BN_bn2bin(bnEncr, encrBin)) { // from big number to binary
        printf("ERROR\n");
    }

然后,我将encrBin传递给EVP_DecryptUpdate并进行解密.

Then I pass encrBin to EVP_DecryptUpdate and decryption works.

我在代码的很多地方都这样做,现在想编写自己的C函数,将十六进制转换为二进制数组,然后将其传递给EVP_DecryptUpdate.我对此进行了尝试,并将加密的十六进制字符串转换为0和1的数组,但事实证明EVP_DecryptUpdate不适用于该数组.根据我在网上可以找到的信息,BN_bn2bin创建了一个真正的二进制表示形式(即,一个位序列).更具体地说,它创建了该数字的大端表示形式."因此,这不仅仅是一个0和1的数组,对吧?

I do this in many places in my code and now want to write my own C function of converting hex to binary array, which I can then pass to EVP_DecryptUpdate. I had a go at this and converted my encrypted hex string to an array of 0s and 1s, but turns out that EVP_DecryptUpdate won't work with that. From what I could find online, BN_bn2bin "creates a representation that is truly binary (i.e. a sequence of bits). More specifically, it creates a big-endian representation of the number." So this is not just an array of 0s and 1s, right?

有人可以解释我如何用C自己进行hex->(真正)二进制转换,这样我就可以得到EVP_DecryptUpdate期望的格式?这很复杂吗?

Can someone explain how I can make the hex->(truly) binary conversion myself in C, so I would get the format that EVP_DecryptUpdate expects? Is this complicated?

推荐答案

BN_bn2bin创建了一个真正的二进制表示形式(即 位序列).更具体地说,它创建了一个大端 数字的表示形式."因此,这不仅是0的数组,而且 1s,对吧?

BN_bn2bin "creates a representation that is truly binary (i.e. a sequence of bits). More specifically, it creates a big-endian representation of the number." So this is not just an array of 0s and 1s, right?

此处提到的位序列表示为字节数组.每个字节包含8位,这可以解释为"0和1的数组".如果您要问的话,它不是具有0或1的整数数组".

The sequence of bits mentioned here is represented as an array of bytes. With each of those bytes containing 8 bits, this can be interpreted as an "array of 0s and 1s". It is not an "array of integers that have the value 0 or 1", if that is what you are asking.

由于您不清楚BN_bn2bin()的工作原理,因此仅分析代码段的最终结果会有所帮助.您可以这样做(忽略任何错误检查):

Since you are unclear of the workings of BN_bn2bin(), it helps to just analyze the end result of your code snippet. You could do that like this (omitting any error checking):

#include <stdio.h>
#include <openssl/bn.h>

int main(
    int argc,
    char **argv)
{
    const char *hexString = argv[1];

    BIGNUM *bnEncr = BN_new();
    BN_hex2bn(&bnEncr, hexString);
    unsigned int numOfBytesEncr = BN_num_bytes(bnEncr);
    unsigned char encrBin[numOfBytesEncr];
    BN_bn2bin(bnEncr, encrBin);
    fwrite(encrBin, 1, numOfBytesEncr, stdout);
}

这会将encrBin的内容输出到标准输出,这从来都不是一件容易的事,但是您可以通过hexdump之类的工具将其通过管道传输,或将其重定向到文件以十六进制进行分析编辑.看起来像这样:

This outputs the contents of encrBin to the standard output, which is never a nice thing to happen, but you can then pipe it through a tool like hexdump, or redirect it to a file for analyzing with a hex editor. It looks like this:

$ ./bntest 74162ac74759e85654e0e7762c2cdd26 | hexdump -C
00000000  74 16 2a c7 47 59 e8 56  54 e0 e7 76 2c 2c dd 26 |t.*.GY.VT..v,,.&|
00000010

或者,如果您确实希望看到这些0和1:

Or, if you do want to see those 0s and 1s:

$ ./bntest  74162ac74759e85654e0e7762c2cdd26 | xxd -b -c 4
00000000: 01110100 00010110 00101010 11000111  t.*.
00000004: 01000111 01011001 11101000 01010110  GY.V
00000008: 01010100 11100000 11100111 01110110  T..v
0000000c: 00101100 00101100 11011101 00100110  ,,.&

这表明您的问题

有人可以解释我如何进行hex->(真正)二进制转换 我自己使用C语言,所以我会得到EVP_DecryptUpdate期望的格式吗? 这很复杂吗?

Can someone explain how I can make the hex->(truly) binary conversion myself in C, so I would get the format that EVP_DecryptUpdate expects? Is this complicated?

本质上与SO问题如何将十六进制字符串转换为无符号字符数组? 评论了.

这篇关于使转换类似于在C中没有openSSL的BN_hex2bn + BN_bn2bin的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆