简体   繁体   English

在C中添加元素到结构数组

[英]adding elements to array of structs in C

So for a crypto class, we are implementing Mental Poker with the SRA protocol. 因此,对于加密课程,我们正在使用SRA协议实施精神扑克。 We are using the openSSL library for BIGNUM http://www.openssl.org/docs/crypto/bn.html and I am having an issue when adding encrypted cards to an array of BIGNUM structs. 我们正在使用BIGNUM http://www.openssl.org/docs/crypto/bn.html的openSSL库,并且在将加密卡添加到BIGNUM结构数组时遇到问题。

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

BIGNUM* encryptedDeck[52];
BIGNUM* bobHand[5];
BIGNUM* aliceHand[5];

int main(int argc, char* argv[]){
    int lcv = 0;
    BIGNUM *P,*Q,*N,*alpha,*alphaPrime,*beta,*betaPrime,*temp,*Pminus;
    BN_CTX *ctx = BN_CTX_new();
    P = BN_new(); //same for all BIGNUM pointers

    BN_generate_prime(P,512,1,NULL,NULL,NULL,NULL);
    BN_generate_prime(Q,512,1,NULL,NULL,NULL,NULL);
    BN_generate_prime(alpha,128,1,NULL,NULL,NULL,NULL); //Alice's key [ gcd(alpha,P) = 1 ]
    BN_generate_prime(beta,128,1,NULL,NULL,NULL,NULL);  //Bob's key. Same rule as alice's

    BN_one(temp);  //set temp to be a BIGNUM equivalent to integer 1
    BN_sub(Pminus,P,temp);

    BN_mul(N,P,Q,ctx);

    temp = BN_new();
    BIGNUM *encryptedCard = BN_new();
    for(lcv; lcv < 52; lcv++){
        BN_bin2bn(deck[lcv],strlen(deck[lcv]),temp);
        BN_mod_exp(encryptedCard,temp,beta,P,ctx);    //encrypt temp, store in encryptedCard

        printf("ec: %s\n\n",BN_bn2dec(encryptedCard));  //prints *correct numbers

        encryptedDeck[lcv] = encryptedCard;  //store cards in the array of encrypted cards
    }

    printf("00: %s\n\n",BN_bn2dec(encryptedDeck[0]));
    printf("01: %s\n\n",BN_bn2dec(encryptedDeck[1]));
    //...
    printf("40: %s\n\n",BN_bn2dec(encryptedDeck[40]));
}

The print statement in the forloop prints 52 different values (1 for each card.) forloop中的print语句打印52个不同的值(每张卡1个)。

I intended for each of those values to simply be added to the array encryptedDeck, but when I check the values after exiting the loop, they are all equivalent to the 52nd card. 我打算将每个值简单地添加到数组encryptedDeck中,但是当我在退出循环后检查值时,它们都等同于第52张卡。 So for some reason, each index in the array is being overwritten for every new card? 所以出于某种原因,每个新卡都会覆盖数组中的每个索引? Or something weird like that. 或者类似的东西。

Is there some obvious thing that I am missing when dealing with arrays of structs? 在处理结构数组时,是否有一些显而易见的事情? The only thing that I would think of right off the bat is something about the array not being initialized with enough space or something. 我唯一能想到的就是没有用足够的空间或其他东西来初始化阵列。

I think that it is possible to convert the BIGNUM values into char* and store them in an array that way, but I tried to avoid this because I don't know how big the pointers need to be, or even have a guess of their range. 我认为可以将BIGNUM值转换为char *并将它们存储在数组中,但我试图避免这种情况,因为我不知道指针需要多大,甚至猜测它们范围。 something like 就像是

char encryptedDeck[52][something ridiculous];

I omitted some code, but I believe this should still compile (provided you have the openssl library, and link it when compiling) and fill in the BN_new() for all the other BIGNUM pointers that I didn't initialize. 我省略了一些代码,但我相信这仍然应该编译(假设您有openssl库,并在编译时链接它)并为我没有初始化的所有其他BIGNUM指针填充BN_new()。

Just above your for-loop: 就在你的for循环之上:

BIGNUM *encryptedCard = BN_new();

You never change what this points to. 你永远不会改变这指向的东西。 You just keep dropping new data into the same card, then save that card into the current slot of the for-loop. 您只需将新数据放入同一张卡中,然后将该卡保存到for循环的当前插槽中。

My surmise is to move the card inside the loop. 我的猜测是将卡循环。

for(lcv; lcv < 52; lcv++){
    BIGNUM *encryptedCard = BN_new();
    BN_bin2bn(deck[lcv],strlen(deck[lcv]),temp);
    BN_mod_exp(encryptedCard,temp,beta,P,ctx);
    printf("ec: %p: %s\n\n",encryptedCard, BN_bn2dec(encryptedCard));
    encryptedDeck[lcv] = encryptedCard;
}

Noted: I don't use this crypto library, so I can't tell you if temp needs the same locality in the loop, but seems like where your problem may be. 注意:我没有使用这个加密库,所以我不能告诉你temp需要循环中的相同位置,但似乎你的问题可能在哪里。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM