RSA returns 不是预期的结果
RSA returns not expected result
下面是RSA算法的代码:
def modexp(a,b,n):
r =1
for i in range(b):
r =r * a% n
return r
def RSA(plaintext,p,q):
encrypted =''
decrypted=''
n =p * q
phi =(p-1)*(q-1)
print("value of the totient function is %d :"%(phi))
e =int(input("enter some comprime number for e accoridng phi : "))
d =pow(e,-1,phi)
for letter in plaintext:
encoding =modexp(ord(letter),e,n)
encrypted =encrypted + chr(encoding)
print("Encrypted version of %s plaintext is %s"%(plaintext,encrypted))
for letter in encrypted:
decoding =modexp(ord(letter),d,n)
decrypted =decrypted +chr(decoding)
print("Decrypted version of %s is %s" % (encrypted, decrypted))
Name =input("enter you Name : ")
p =3
q =7
RSA(Name,p,q)
当我 运行 程序时,我输入一些文本例如计算机,phi 的值为 12,我选择一些互质数例如 5,但它 returns 结果如下 :
enter you Name : computer
value of the totient function is 12 :
enter some comprime number for e accoridng phi : 5
Encrypted version of computer plaintext is
Decrypted version of is
怎么了?
RSA 公式假定 N 的值非常大,因此消息小于该值。在您的情况下,对于 N=21
,并输入消息 A
:
ord(A)=65=2 (mod N)
所以加密后解密回来,你会得到2.
此外,使用 RSA 进行数据加密并不安全(参见 textbook RSA),它仅用于对称密钥交换。
下面是RSA算法的代码:
def modexp(a,b,n):
r =1
for i in range(b):
r =r * a% n
return r
def RSA(plaintext,p,q):
encrypted =''
decrypted=''
n =p * q
phi =(p-1)*(q-1)
print("value of the totient function is %d :"%(phi))
e =int(input("enter some comprime number for e accoridng phi : "))
d =pow(e,-1,phi)
for letter in plaintext:
encoding =modexp(ord(letter),e,n)
encrypted =encrypted + chr(encoding)
print("Encrypted version of %s plaintext is %s"%(plaintext,encrypted))
for letter in encrypted:
decoding =modexp(ord(letter),d,n)
decrypted =decrypted +chr(decoding)
print("Decrypted version of %s is %s" % (encrypted, decrypted))
Name =input("enter you Name : ")
p =3
q =7
RSA(Name,p,q)
当我 运行 程序时,我输入一些文本例如计算机,phi 的值为 12,我选择一些互质数例如 5,但它 returns 结果如下 :
enter you Name : computer
value of the totient function is 12 :
enter some comprime number for e accoridng phi : 5
Encrypted version of computer plaintext is
Decrypted version of is
怎么了?
RSA 公式假定 N 的值非常大,因此消息小于该值。在您的情况下,对于 N=21
,并输入消息 A
:
ord(A)=65=2 (mod N)
所以加密后解密回来,你会得到2.
此外,使用 RSA 进行数据加密并不安全(参见 textbook RSA),它仅用于对称密钥交换。