我正在尝试RSA加密一个单词2个字符,一次使用Python填充空格,但不知道我是怎么做的。
例如,如果加密指数为8且模数为37329且单词为“Pound”,我将如何处理它?我知道我需要从pow(ord('P')开始,需要考虑这个单词是5个字符,我需要一次填充2个字符。我不确定但是做我还需要在某处使用<< 8;
谢谢
答案 0 :(得分:3)
这是一个基本的例子:
>>> msg = 2495247524
>>> code = pow(msg, 65537, 5551201688147) # encrypt
>>> code
4548920924688L
>>> plaintext = pow(code, 109182490673, 5551201688147) # decrypt
>>> plaintext
2495247524
有关使用RSA样式公钥加密的数学部分的更多工具,请参阅ASPN cookbook recipe。
如何将字符打包并解压缩为块以及如何编码数字的细节有点神秘。这是一个完整的,有效的RSA module in pure Python。
对于您的特定包装模式(一次2个字符,填充空格),这应该有效:
>>> plaintext = 'Pound'
>>> plaintext += ' ' # this will get thrown away for even lengths
>>> for i in range(0, len(plaintext), 2):
group = plaintext[i: i+2]
plain_number = ord(group[0]) * 256 + ord(group[1])
encrypted = pow(plain_number, 8, 37329)
print group, '-->', plain_number, '-->', encrypted
Po --> 20591 --> 12139
un --> 30062 --> 2899
d --> 25632 --> 23784
答案 1 :(得分:1)
如果你想使用python高效编码RSA加密,我的github存储库肯定会理解和解释python中RSA的数学定义
Cryptogrphic Algoritms Implementation Using Python
RSA密钥生成
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))