user466534
user466534

Reputation:

RSA returns not expected result

below there is code for RSA algorithm :

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)

when i run program, i enter some text for instance computer , value for phi is 12, i choose some coprime number for instance 5, but it returns result like this :

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 

what is wrong?

Upvotes: 0

Views: 53

Answers (1)

bereal
bereal

Reputation: 34292

RSA formula assumes a very large value of N, so that the message is less than that. In your case, for N=21, and input message A:

ord(A)=65=2 (mod N)

so after encrypting it and decrypting back, you will receive 2.

Besides, it's not safe to use RSA for data encryption (see textbook RSA), it's only used for symmetric key exchange.

Upvotes: 1

Related Questions