5

在执行这个 diffie hellman 交换示例时,我无法弄清楚为什么我的密钥不相等。我在 C (openssl/dh.h) 中使用 openssl 库。

这看起来很简单,但由于某种原因,键不一样。我错过了什么?

有任何想法吗?谢谢!

    void hexprint(unsigned char *printBuf, int len)
    {
        int i;
        for(i = 0; i < len; i++)
        {
            printf("%x ", printBuf[i]);
        }
        printf("\n");
    }

    int main(int argc, char *argv[])
    {
        srand(time(NULL));
        DH *dh1;
        DH *dh2;
        unsigned char *dh_secret1;
        unsigned char *dh_secret2;
        dh1 = DH_generate_parameters(256, 2, NULL, NULL);
        dh2 = DH_generate_parameters(256, 2, NULL, NULL);

        DH_generate_key(dh1);
        DH_generate_key(dh2);

        dh_secret1 = malloc(DH_size(dh1));
        memset(dh_secret1, 0, DH_size(dh1));
        dh_secret2 = malloc(DH_size(dh2));
        memset(dh_secret2, 0, DH_size(dh2));

        DH_compute_key(dh_secret1, dh2->pub_key, dh1);
        DH_compute_key(dh_secret2, dh1->pub_key, dh2);

        printf("Secret Key 1: \n");
        hexprint(dh_secret1, 32);
        printf("Secret Key 2: \n");
        hexprint(dh_secret2, 32);

        free(dh_secret1);
        free(dh_secret2);
        DH_free(dh1);
        DH_free(dh2);
    }
4

1 回答 1

5

因为在 DH 中,用户必须共享相同的参数。只执行DH_generate_parameters一次,并在用户之间共享。

于 2013-06-16T10:20:10.933 回答