# RSA Encryption not returning result

Page 1 of 1

## 4 Replies - 801 Views - Last Post: 17 February 2013 - 09:53 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=312455&amp;s=9ae72f4f25e97849ebcdd86b08293ff7&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 stephanie904

Reputation: 1
• Posts: 33
• Joined: 14-February 12

# RSA Encryption not returning result

Posted 16 February 2013 - 08:08 PM

Im trying to do RSA Encryption and I am not sure what to try anymore.
I know modExp() works cause I tested it already. So, I think the problem is rsa_encypt().
If someone could help me I would appreciate it.

My code compiles and when I try to type in the word for example "The Queen Can't Roll When Sand is in the Jar." It tells me None...

My code so far
p = 61
q = 53
e = 17
n = p * q

def rsa_encypt(m, message):
c = ""
m = ""
#make message length even
if len(message) % 2 == 1:
message += " "
for val in message:
if len(number) < 2:
number = "0" + number
m += number
#encrypt
if len(m) == 4:
c +=modExp(m,e,n) + ""
m = ""
return c

aDict = { chr(x):x for x in range(129) }

def modExp(a, b, m) :
"""Computes a to the power b, modulo m, using binary exponentiation
"""
a %= m
ret = None

if b == 0 :
ret = 1
elif b%2 :
ret = a * modExp(a,b-1,m)
else :
ret = modExp(a,b//2,m)
ret *= ret

return ret%m

print (rsa_encypt(n,message))

Is This A Good Question/Topic? 0

## Replies To: RSA Encryption not returning result

### #2 stephanie904

Reputation: 1
• Posts: 33
• Joined: 14-February 12

## Re: RSA Encryption not returning result

Posted 17 February 2013 - 12:43 AM

ok I re did my code a bit

I'm following the RSA algorithm from the Wiki: http://en.wikipedia....RSA_(algorithm)

I'm trying to do RSA Encryption and I am not sure what to try anymore and getting frustrated. I know mod_exp works from modular class because I tested it already. So, I think the problem is in the Encryptions class.

My code compiles and when I try the phrase "The Queen Can't Roll When Sand is in the Jar.", it doesn't do anything

My code so far:

modular class
def _base_b_convert(n, B)/>/>/>:
if b < 1 or n < 0:
raise ValueError("Invalid Argument")

q = n
a = []
while q != 0:
value = int(q % B)/>/>/>
a.append(value)
q =int(q / B)/>/>/>
return a

def mod_exp(base, n, mod):
if base < 0 or n < 0 or mod < 0:
raise ValueError("Invalid Argument")
a = (_base_b_convert(n, 2))
x = 1
pow = base % mod
for i in range(0, len(a)):
if a[i] == 1:
x = (x * pow) % mod
pow = pow**2 % mod
return x

encryptions class
from Modular import mod_exp

class Encryptions(object):
def __init__(self):
self.p = 61
self.q = 53
self.e = 17

def encrypt(self, message):
a = self._convert_to_numeric(message)

for i in range(len(a)):
a[i] = self._encrypt(a[i])
m = ""
for i in range(len(a)):
if i != 0:
m += ","
m += str(a[i])
return m

def _encrypt(self, m):
p = self.p
q = self.q
n = p * q
e = self.e
return mod_exp(m, e, n)

def decrypt(self, message):
a = message.split(',')
m = ""
for i in range(len(a)):
m += self._num_to_letter(self._decrypt(int(a[i])))
return m

def _decrypt(self, c):
n = self.p * self.q
e = self.e
phi = (self.p - 1) * (self.q - 1)
d = 2753
return mod_exp(c, d, n)

def _convert_to_numeric(self, message):
m = []

for i in range(len(message)):
m.append(self._letter_to_num(message[i]))
return m

def _num_to_letter(self, m):
return chr(int(m))

def _letter_to_num(self, m):
return ord(m)

main class
from encryptions import Encryptions

def main():
enc = Encryptions()
print("Encrypting message: The Queen Can't Roll When Sand is in the Jar.")
message = "The Queen Can't Roll When Sand is in the Jar."
message = enc.encrypt(message)
print(message)
print()
print("Decrypting message:")
message = enc.decrypt(message)
print(message)

input("--Press any key to end--")

if __name__ == '__main__':
pass

This post has been edited by stephanie904: 17 February 2013 - 12:45 AM

### #3 Mekire

Reputation: 118
• Posts: 216
• Joined: 11-January 13

## Re: RSA Encryption not returning result

Posted 17 February 2013 - 07:52 AM

Well... I haven't actually messed around doing anything with RSA myself but...
You didn't call your main function at the end of the program so... that might be a problem.

Change this:
if __name__ == '__main__':
pass
To this:
if __name__ == '__main__':
main()

After changing that and fixing the indentation (which was probably just a result of how you pasted it in) this is the output I get:
Encrypting message: The Queen Can't Roll When Sand is in the Jar.
2159,2170,1313,1992,2718,2160,1313,1313,2235,1992,641,1632,2235,1956,884,1992,1859,2185,745,745,1992,604,2170,1313,2235,1992,2680,1632,2235,1773,1992,3179,1230,1992,3179,2235,1992,884,2170,1313,1992,1877,1632,2412,2825

Decrypting message:
The Queen Can't Roll When Sand is in the Jar.

Kinda looks like it is working.
-Mek

### #4 stephanie904

Reputation: 1
• Posts: 33
• Joined: 14-February 12

## Re: RSA Encryption not returning result

Posted 17 February 2013 - 09:34 AM

Mekire, on 17 February 2013 - 07:52 AM, said:

Well... I haven't actually messed around doing anything with RSA myself but...
You didn't call your main function at the end of the program so... that might be a problem.

Change this:
if __name__ == '__main__':
pass
To this:
if __name__ == '__main__':
main()

After changing that and fixing the indentation (which was probably just a result of how you pasted it in) this is the output I get:
Encrypting message: The Queen Can't Roll When Sand is in the Jar.
2159,2170,1313,1992,2718,2160,1313,1313,2235,1992,641,1632,2235,1956,884,1992,1859,2185,745,745,1992,604,2170,1313,2235,1992,2680,1632,2235,1773,1992,3179,1230,1992,3179,2235,1992,884,2170,1313,1992,1877,1632,2412,2825

Decrypting message:
The Queen Can't Roll When Sand is in the Jar.

Kinda looks like it is working.
-Mek

hey thanks...I actually did notice if __name__ == '__main__': as well but im still getting an error...what version of Python are you running ? can you post the code here please I think im not getting indentation right...

This post has been edited by stephanie904: 17 February 2013 - 09:43 AM

### #5 stephanie904

Reputation: 1
• Posts: 33
• Joined: 14-February 12

## Re: RSA Encryption not returning result

Posted 17 February 2013 - 09:53 AM

nevermind I got it

thanks!