Home > Crypto > Chapter 7 - RSA
RSA relies on multiplying two large primes. It’s easy to do, but very hard to reverse.
Done in ring $\Bbb Z_n$ - so $x$ (as a bitstring) has to be smaller than n.
Given:
Encryption is:
\[y = e_{k_{pub}}(x) \equiv x^e \bmod n\]Where $x, y \in \Bbb Z_n$
Given:
Decryption is:
\[x = d_{k_{pr}}(y) \equiv y^d \bmod n\]How does the decrypter get n? Ah, that’s part of their public key?
Where $x, y \in \Bbb Z_n$
So, plain to the $e$ is cipher, which then to the $d$ is plain again. In the ring ofc.
You can use the EEA to find the inverse of $e$ - call that $t$.
You can use $t$ to find $d$ using:
\[d = t \bmod \phi(n)\]$x^8 = x * x * x * x * x * x * x * x$
That’s a lot, and if we had a number that’s 1024 bits long, this would take absolutely forever. However, $x^8 = ((x^2)^2)^2$, which is only 3 operations.
Through combinations of squaring and multiplying, we can get big fast.
Start with y=1, and scan the bits of the exponent from most to least significant.
Basically, squaring left-shifts the exponent (in binary), and multiplying switches the rightmost bit from zero to 1.
BigInt.toString(2) will print your bigint as binary.
tl;dr: doing big math is hard
make e small, like 3, 17, and 2^16+1. These are easy to do fast exponentiation with.
While $e$ can be small, $d$ cannot - otherwise attackers can brute-force it pretty easily. $d$ should be at least $0.3t$, where $t$ is the bit length of $n$.
Goal: $x^d \bmod n$
But, $x^d$ is gonna be absolutely absurdly big, and $n$ is also huge.
So, use CRT to get $x_d$ and $x_p$, and do $y_p = {x_p}^d_p \bmod p$, and the $q$ version, and then bring it back to the real world.
$p$ and $q$ should have about half the bitlength of the desired bitlength of $n$. So if $n$ should have a bitlength of 1024, $p$ and $q$ should be about 512.
Generate random big numbers, and see if they’re prime tho.
yes.
Fermat
Basically, $a^{p-1} \equiv 1 \bmod p$ if p is prime. $a$ can be anything from 2 to $p-2$. We generally test a bunch of a’s, and if they all pass, it’s probably fine.
Charmichael Numbers
These are composite ints that behave like primes - they’ll pass the Fermat test if $gcd(a,p)=1$. That’s a lot of a’s. They’re sneaky.