4

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

3
  • Please, specify why you do it by hand instead of using existing libraries. Commented Nov 2, 2011 at 17:59
  • @J.F.Sebastian Because I am not actually learning the Python language just using it purely for the encryption as that is my focus. Where are the existing libraries for what I want to achieve? Commented Nov 2, 2011 at 19:03
  • In general If you are not writing yourself an encryption library i.e., you're just writing an app that does encryption then you should fight NIH syndrome and use existing libraries especially in such sensitive domain as cryptography. Here's a brief comparison of some Python modules related to crypto: python-crypto.pdf (2009) Commented Nov 2, 2011 at 19:25

2 Answers 2

4

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
Sign up to request clarification or add additional context in comments.

5 Comments

Thank you but how would I do the above if I was to do it in a terminal rather than write the code out in an editor then run it? Out of interest what it the 256 in there for?
The multiply by 256 is the same as the << 8 that you originally considered. It is a way of combining two bytes into a single number less than 32536. The snippets above are done in a terminal and not in a script created by an editor.
I would really use the square and multiply algorithm for exponentiation: en.wikipedia.org/wiki/Exponentiation_by_squaring
Square and multiply is more optimized for modular exponentiation
Whoops, you mean under the hood pow() implements the Square-multiply algorithm? My bad
1

If you want to efficiently code the RSA encryption using python, my github repository would definitely to understand and interpret the mathematical definitions of RSA in python

Cryptogrphic Algoritms Implementation Using Python

RSA Key Generation

def keyGen():
    ''' Generate  Keypair '''
    i_p=randint(0,20)
    i_q=randint(0,20)
    # Instead of Asking the user for the prime Number which in case is not feasible,
    # generate two numbers which is much highly secure as it chooses higher primes
    while i_p==i_q:
        continue
    primes=PrimeGen(100)
    p=primes[i_p]
    q=primes[i_q]
    #computing n=p*q as a part of the RSA Algorithm
    n=p*q
    #Computing lamda(n), the Carmichael's totient Function.
    # In this case, the totient function is the LCM(lamda(p),lamda(q))=lamda(p-1,q-1)
    # On the Contrary We can also apply the Euler's totient's Function phi(n)
    #  which sometimes may result larger than expected
    lamda_n=int(lcm(p-1,q-1))
    e=randint(1,lamda_n)
    #checking the Following : whether e and lamda(n) are co-prime
    while math.gcd(e,lamda_n)!=1:
        e=randint(1,lamda_n)
    #Determine the modular Multiplicative Inverse
    d=modinv(e,lamda_n)
    #return the Key Pairs
    # Public Key pair : (e,n), private key pair:(d,n)
    return ((e,n),(d,n))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.