I’m trying to figure out how to implement RSA crypto from scratch (just for the intellectual exercise), and i’m stuck on this point:
For encryption, c = me mod n
Now, e is normally 65537. m and n are 1024-bit integers (eg 128-byte arrays). This is obviously too big for standard methods. How would you implement this?
I’ve been reading a bit about exponentiation here but it just isn’t clicking for me:
Wikipedia-Exponentiation by squaring
This Chapter (see section 14.85)
Thanks.
edit: Also found this – is this more what i should be looking at? Wikipedia- Modular Exponentiation
Exponentiation by squaring:
Let’s take an example. You want to find 1723. Note that 23 is
10111in binary. Let’s try to build it up from left to right.When you square, you double the exponent (shift left by 1 bit). When you multiply by m, you add 1 to the exponent.
If you want to reduce modulo
n, you can do it after each multiplication (rather than leaving it to the end, which would make the numbers get very large).65537 is
10000000000000001in binary which makes all of this pretty easy. It’s basicallywhere of course a, n and m are “big integers”. a needs to be at least 2048 bits as it can get as large as (n-1)2.