计算BIGNUM除以任意整数取模的余数。

huangapple go评论67阅读模式
英文:

compute the remainder of the BIGNUM divided by any integer modulo

问题

这是您提供的代码的翻译:

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

int main() {

    unsigned char message[] = "hello world"; // 消息
    // hello world = 01101000 01100101 01101100 01101100 01101111 00100000 01110111 01101111 01110010 01101100 01100100
    // 位的值为 126207244316550804821666916
    int length = strlen((char *)message);
    int i, j;

    BIGNUM *bn = BN_new();
    BN_zero(bn);

    for (i = 0; i < length; i++) {
        for (j = 0; j < 8; j++) {
            if (message[i] >> (7 - j) & 1) {
                BN_lshift(bn, bn, 1);
                BN_add_word(bn, 1);
            } else {
                BN_lshift(bn, bn, 1);
            }
        }
    }

    BIGNUM *modulus = BN_new();
    BN_set_word(modulus, 1024);

    BN_mod(bn, bn, modulus, NULL);

    char *dec = BN_bn2dec(bn);
    printf("%s\n", dec);

    OPENSSL_free(dec);
    BN_free(bn);
    BN_free(modulus);

    return 0;
}

这段代码的目标是将字符串"hello world"转换为位并计算其模1024的余数。您提供的代码似乎有一些错误,导致出现Segmentation Fault错误。如果需要进一步的调试和修复,请提供更多上下文或相关错误信息。

英文:

given some string, let assume , hello world. I know that the string in bits is: 01101000 01100101 01101100 01101100 01101111 00100000 01110111 01101111 01110010 01101100 01100100. so the value of the bits is 126207244316550804821666916. I was able to create code that give my the value of the bits. now I want to compute the remainder of the BIGNUM divided by any integer modulus, let assume 1024. I expect to get number between 0 to 1023. but when i trying to apply the modulo , i get error: Segmentation fault (core dumped)

this is the code that I using:

#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;string.h&gt;
#include &lt;openssl/bn.h&gt;


int main() {

    unsigned char message[] = &quot;hello world&quot;; //message
    //hello world = 01101000 01100101 01101100 01101100 01101111 00100000 01110111 01101111 01110010 01101100 01100100
    //the value of bits is 126207244316550804821666916
    int length = strlen((char *)message);
    int i,j;

    BIGNUM *bn = BN_new();
    BN_zero(bn);

    for (i = 0; i &lt; length; i++) {
        for(int j=0; j&lt;8; j++){
            if (message[i] &gt;&gt; (7-j) &amp; 1) {
                BN_lshift(bn, bn, 1);
                BN_add_word(bn, 1);
            }
            else {
                BN_lshift(bn, bn, 1);
            }
        }

    }

    BIGNUM *modolus = BN_new();
    BN_set_word(modolus, 1024);

    BN_mod(bn, bn, modolus, NULL);
    
    char *dec = BN_bn2dec(bn);
    printf(&quot;%s\n&quot;, dec);

    OPENSSL_free(dec);
    BN_free(bn);
    BN_free(modolus);

    return 0;

}


I'm new to C, i not sure what is exactly wrong here. after the loop i expect to get in bn the value 126207244316550804821666916, and its seems right. but when make module with 1024, i got error.

答案1

得分: 2

你不能将空指针用作上下文参数。它需要指向有效的 BN_CTX 对象的指针。

例如,

BN_CTX *ctx = BN_CTX_new();
BN_mod(bn, bn, modolus, ctx);
BN_CTX_free(ctx);

在实际代码中,你会希望提前分配上下文对象并在后续计算中重复使用它,而不是立即释放它,当然。

英文:

You can't pass a null pointer as the context argument. It needs a pointer to a valid BN_CTX object.

For example,

BN_CTX *ctx = BN_CTX_new();
BN_mod(bn, bn, modolus, ctx);
BN_CTX_free(ctx);

In real code you'll want to allocate the context object ahead of time and re-use it for subsequent computations instead of freeing it right away, of course.

huangapple
  • 本文由 发表于 2023年3月9日 16:18:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/75681966.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定