def keyGen():
''' Generate Keypair '''
i_p=randint(0,20)
i_q=randint(0,20)
# Instead of Asking the user for the prime Number which in case is not feasible,
# generate two numbers which is much highly secure as it chooses higher primes
while i_p==i_q:
continue
primes=PrimeGen(100)
p=primes[i_p]
q=primes[i_q]
#computing n=p*q as a part of the RSA Algorithm
n=p*q
#Computing lamda(n), the Carmichael's totient Function.
# In this case, the totient function is the LCM(lamda(p),lamda(q))=lamda(p-1,q-1)
# On the Contrary We can also apply the Euler's totient's Function phi(n)
# which sometimes may result larger than expected
lamda_n=int(lcm(p-1,q-1))
e=randint(1,lamda_n)
#checking the Following : whether e and lamda(n) are co-prime
while math.gcd(e,lamda_n)!=1:
e=randint(1,lamda_n)
#Determine the modular Multiplicative Inverse
d=modinv(e,lamda_n)
#return the Key Pairs
# Public Key pair : (e,n), private key pair:(d,n)
return ((e,n),(d,n))
如果您想使用python高效地编码RSA加密,我的github存储库肯定会理解和解释python中RSA的数学定义
Cryptogrphic Algoritms Implementation Using Python
RSA密钥生成
下面是一个基本示例:
请参阅ASPN cookbook recipe以获取更多用于处理RSA样式公钥加密的数学部分的工具。
字符如何打包和解包成块以及数字如何编码的细节有点神秘。这是一个完整的,有效的RSA module in pure Python。
对于您的特定包装模式(一次2个字符,用空格填充),这应该有效:
相关问题 更多 >
编程相关推荐