gpt4 book ai didi

c# - Diffie-Hellman 与 BIGNUM (OpenSSL) 与 BigInteger (C#)

转载 作者:太空狗 更新时间:2023-10-29 15:08:03 25 4
gpt4 key购买 nike

我正在实现 Diffie-Hellman使用 OpenSSL 库进行 BIGNUM 操作的嵌入式设备与使用 System.Numerics.BigInteger 方法计算 secret 共享 key 生成的 C# 软件之间的 key 交换算法。

但是Alice和Bob交换 key 后,他们计算出不同的共享 secret 。

key 打印在每一面 (PubA, PrivA, PubB, PrivB, DHPrime, DHGenerator) 我可以看到它们是相同的。

我怀疑存在关于小/大字节序的问题,或者 openssl 可能不关心指数的负数,我不知道如何调试这些操作。我现在没有代码,但所有操作都保持简单,就像这样。

C# 端

BigInteger bintA = new BigInteger(baByteArrayReceived);
BigInteger bintb = new BigInteger(baRandomBobSecret);
BigInteger bintDHPrime = new BigInteger(baDHPrime2048);

BigInteger bintSharedSecret = bintA.ModPow(bintb,bintDHPrime);

C面

BIGNUM *bnB = BN_new();
BIGNUM *bna = BN_new();
BIGNUM *bnDHPrime = BN_new();
BIGNUM *bnResult = BN_new();
BN_CTX *bnctx = BN_CTX_new();

BN_bin2bn(baBReceived, 256,bnB);
BN_bin2bn(baRandomAliceSecret, 256,bna);
BN_bin2bn(baDHPrime2048, 256,bnDHPrime);

BN_mod_exp(bnResult,bnB,bna,bnDHPrime,bnctx);

OpenSSL 的 C 方法的一些附加信息:>

BIGNUM *BN_bin2bn(const uint8_t *in, size_t len, BIGNUM *ret);

BN_bin2bn sets |*ret| to the value of |len| bytes from |in|, interpreted as a big-endian number, and returns |ret|. If |ret| is NULL then a fresh |BIGNUM| is allocated and returned. It returns NULL on allocation failure.

 int BN_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, 
const BIGNUM *m, BN_CTX *ctx);

BN_mod_exp sets |r| equal to |a|^{|p|} mod |m|. It does so with the best algorithm for the values provided and can run in constant time if |BN_FLG_CONSTTIME| is set for |p|. It returns one on success or zero otherwise.

而且它们会产生不同的结果。

我该怎么办?你接下来要检查什么?

提前致谢。

最佳答案

通过使用 BouncyCaSTLe Crypto Library 的 BigInteger 方法解决。无法告诉 System.Numerics.BigInteger 我们正在使用大端无符号字符进行初始化。

关于c# - Diffie-Hellman 与 BIGNUM (OpenSSL) 与 BigInteger (C#),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28005253/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com