#RSA Code not Encrypting or Decrypting Properly

14 messages · Page 1 of 1 (latest)

light swallow
#

I need to write a RSA code for an assignment, but the final answer never matches up to my starting message. I've checked multiple sites, but I have not found my error. Any thoughts?

p=3
q=7
 
n=(p)*(q) 
t=(p-1)*(q-1) 
phi=totient(n)
print(n)
print(t)
print(phi)

o=5
d=(o^-1)%t 

print(d)

print(math.gcd(t))
print(math.gcd(o))

m=0
    
print('original message is', m)
print('the public key is', o,n)
print('the private key is', d,n)

c=(t^o)%n 
m=(c^o)%n 
m1=0

print('encrypted message is', c)
print('decrypted message is', m)

if (m==m1):
    print("Finally!")
else: 
    print("Not again!!") ```
cosmic isle
#

^ isn't raised to the power

light swallow
#

You are right and thank you for noticing. However, that didn’t fix it

cosmic isle
#

can you show your full code?

light swallow
#
q=7
 
n=(p)*(q) 
t=(p-1)*(q-1) 
phi=totient(n)
print('n is',n)
print('t is',t)
print('phi is',phi)

o=5
d=(o**-1)%t 

print('d is',d)

print('gcd t is', math.gcd(t))
print('gcd o is', math.gcd(o))

m=8675309
    
print('original message is', m)
print('the public key is', o,n)
print('the private key is', d,n)

c=(t**(o))%n 
m=(c**(d))%n 
m1=8675309

print('encrypted message is', c)
print('decrypted message is', m)

if (m==m1):
    print("Finally!")
else: 
    print("Not again!!")```
cosmic isle
#

Are you sure that's how you find d?

#

modular inverse I don't think is just

timid nacelleBOT
light swallow
#

I didn't think so either, but when I run it as a positive, I get 12 back

cosmic isle
#

That's just not how modular inverse works.

#

You can find it using the extended euclidean algorithm IIRC.

light swallow
#

👍

cosmic isle
#

It will look like finding the GCD of d, and phi IIRC.

#

Ping me if you need anything else!