RSA加密python不使用小素数
我已经实现了RSA加密和解密代码,该代码用于值RSA加密python不使用小素数,python,python-3.x,cryptography,rsa,modular-arithmetic,Python,Python 3.x,Cryptography,Rsa,Modular Arithmetic,我已经实现了RSA加密和解密代码,该代码用于值p,q,d=61,53,17。我接受了维基百科中提到的这些值。我认为p和q应该是素数,而d的选择使得d和φ(n)是相对素数 如果我将值更改为p,q,d=3,17,19,则解密无效。你能帮我做这个吗?这是我的密码: #!/usr/bin/python3 # -*- coding: utf-8 -*- def main(): str = 'computer' p,q,d = 61,53,17 #p,q,d = 3,17,19
p,q,d=61,53,17
。我接受了维基百科中提到的这些值。我认为p和q应该是素数,而d的选择使得d和φ(n)是相对素数
如果我将值更改为p,q,d=3,17,19
,则解密无效。你能帮我做这个吗?这是我的密码:
#!/usr/bin/python3
# -*- coding: utf-8 -*-
def main():
str = 'computer'
p,q,d = 61,53,17
#p,q,d = 3,17,19
cipher_text = list()
plain_text = list()
for c in str:
cipher_char = EncryptCharRSA(c, p, q ,d)
cipher_text.append(cipher_char)
for x in cipher_text:
plain_char = DecryptCharRSA(x, p, q, d)
plain_text.append(plain_char)
print ('Original Message: ', str)
print ('Encrypted Message(UTF-8 Unicode characters) : ', end='')
for element in cipher_text:
print(element,end = '')
print ('\nDecrypted Message: ', end='')
for element in plain_text:
print(element,end='')
def EncryptCharRSA(msg , p, q, d):
n = p * q
phi = (p-1) * (q-1)
cipher_no = 0
cipher_char = ''
for c in msg:
# conver char to ascii for calculation
cipher_no = (ord(c)** d) % n
cipher_char = chr(cipher_no)
return cipher_char
#print (cipher_no)
#plain_no = (cipher_no ** d) % n
def DecryptCharRSA(msg,p, q,d):
n = p * q
phi = (p-1) * (q-1)
e = ModularMultiplicativeInverse(d,phi)
for c in msg:
plain_no = (ord(c) ** e) % n
plain_char = chr(plain_no)
return plain_char
# Get modular multiplicative inverse
def ModularMultiplicativeInverse(d,n):
i = 1
while True:
if (d * i) % n == 1:
return i
i = i + 1
if __name__ == '__main__' : main()
你所谓的
d
实际上是e
公共指数,你所谓的e
实际上是d
私有指数
撇开命名不谈,您的问题是加密大于或等于n
的明文字符代码点。如果是,那么您实际上加密的不是ord(“A”)
(=65),而是ord(“A”)%n
。对于小型n
,如您的情况,这将导致无法恢复的密文:
>>> n = 3 * 17 # 51
>>> ord("A")
65
>>> ord("A") % n
14
而这正是你能够解密的。RSA不是可以用来加密任意大数据的东西。通常,您会将其与安全快速的分组密码(如AES-through)相结合。您所称的
d
实际上是e
公共指数,而您所称的e
实际上是d
私有指数
撇开命名不谈,您的问题是加密大于或等于n
的明文字符代码点。如果是,那么您实际上加密的不是ord(“A”)
(=65),而是ord(“A”)%n
。对于小型n
,如您的情况,这将导致无法恢复的密文:
>>> n = 3 * 17 # 51
>>> ord("A")
65
>>> ord("A") % n
14
而这正是你能够解密的。RSA不是可以用来加密任意大数据的东西。通常,您会将其与安全快速的分组密码(如AES-through)结合使用。for循环的
内的返回值是否是故意的?是的。在对字符进行加密/解密后,我将向main()函数返回一个字符。for
循环的内部的return
是否是故意的?是的。对字符进行加密/解密后,我将向main()函数返回一个字符。我使用字典限制了我的字符集,例如:字母={a:1,b:2…}。因此,现在它的工作很好。感谢您的帮助:-)我使用字典限制了我的字符集,例如:字母={a:1,b:2…}。因此,现在它的工作很好。谢谢你的帮助:-)