Q:

RSA Encryption using Python

I'm trying to RSA encrypt a word 2 characters at a time padding with a space using Python but not sure how I go about it.
For example if the encryption exponent was 8 and a modulus of 37329 and the word was 'Pound' how would I go about it? I know I need to start with pow(ord('P') and need to take into consideration that the word is 5 characters and I need to do it 2 characters at a time padding with a space. I'm not sure but do I also need to use <<8 somewhere?
Thank you

A:

Here's a basic example:
>>> msg = 2495247524
>>> code = pow(msg, 65537, 5551201688147)               # encrypt
>>> code
4548920924688L

>>> plaintext = pow(code, 109182490673, 5551201688147)  # decrypt
>>> plaintext
2495247524

See the ASPN cookbook recipe for more tools for working with mathematical part of RSA style public key encryption.
The details of how characters get packed and unpacked into blocks and how the numbers get encoded is a bit arcane.  Here is a complete, working RSA module in pure Python.
For your particular packing pattern (2 characters at a time, padded with spaces), this should work:
>>> plaintext = 'Pound'    
>>> plaintext += ' '      # this will get thrown away for even lengths    
>>> for i in range(0, len(plaintext), 2):
        group = plaintext[i: i+2]
        plain_number = ord(group[0]) * 256 + ord(group[1])
        encrypted = pow(plain_number, 8, 37329)
        print group, '-->', plain_number, '-->', encrypted

Po --> 20591 --> 12139
un --> 30062 --> 2899
d  --> 25632 --> 23784

