Python RSA返回非预期结果

Python RSA返回非预期结果,python,cryptography,rsa,Python,Cryptography,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 :&quo

下面是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,但它返回的结果如下:

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进行数据加密是不安全的(请参阅),它只用于对称密钥交换。

Btw,
modexp
pow
。您不必自己实现它。pow函数是否考虑可能的溢出?什么溢出?你是说模?是的,这就是第三个参数的作用。而且,
pow
的实现效率更高。当遇到用于实际生产性加密的密钥大小时,您的实现将几乎不可靠地运行。我的意思是,如果我以1000000次方2000000次方为例,诸如此类。谢谢,我明白了,椭圆曲线密码术呢?它能保存吗?@datodatuashvili对于椭圆曲线,他们使用Diffie-Helman()方案交换对称密钥。实际上,RSA目前主要用于PSS填充的数字签名。如果你知道,我很乐意知道。