2

我正在尝试在 python 2.7 中实现 El Gamal 的基本示例。解密中有一些我无法解决的错误。

它的解密步骤应该是:e^-d * c mod p

任何帮助表示赞赏。

from __future__ import division
from random import SystemRandom

cryptogen = SystemRandom()

# find a prime
p = 809

# random g
g = 256

# private key d from {1..,p-2}
d = 68

# public key part
k = (g ** d) % p


## k_rpiv d
## k_pub: p, g, k


# message
m = 100
# r is any real number
r = 89
# encryption
e = (g ** r) % p
# cipher
c = (k ** r * m) % p

# decryption
s = (e ** d) % p
dec = (1/s * c) % p
print(m, e, c, dec)

output:
(100, 468L, 494L, 1.7642857142857142)

请参阅解决方案的副本或简而言之此处(另请参阅关于计算效率的评论)

# decryption
# find inverse of e ** d under p
# p is prime, so any number a \in Z_p^* is coprime with p
# => Eulers Theorem: a^(p-1) \equiv 1 (mod p)
# and inverse of a is given by a^(p-2) (mod p)
dec = ((e ** d) ** (p - 2) * c) % p
4

0 回答 0