This repository will record some codes mentioned in Book 'From Mathematics to Generic programming' and use some examples to test it.
Install rsa package
CC=Clang pip install -e . -vvv
or
python setup.py install
then for d2l
pip install -U d2l
,and finally for Pytorch(optional here for MAC with only CPU)
conda install pytorch torchvision torchaudio -c pytorch
First we generate two large prime numbers:
- Choose two distinct prime numbers
$p$ and$q$ . - Compute
$n = pq$ . - Compute tue Eulear totient function
$\phi(n)=(p-1)(q-1)$ .
Now, we destroy
- Generate a random public key
$pub$ , coprime with$\phi(n)$ - Find the private key
$prv$ , which is multiplicative inverse of$pub$ modulo$\phi(n)$ .
Finally,
- the sender will encrypte message by raising
$m$ to the power$pub$ ; - the accepter will decrpte message by rasising private
$prv$ :
The above uses a theorem: $$ (m^{pub})^{prv} = m \text{ mod } n $$
Attension: In reality, we represent each word by an intger and so in order to avoid different words
$m$ and$m'$ encrypted by the same number like$m^{pub} = m^{'pub} \text{ mod } n$ , we must require that the lenght of dictionary <$n = pq$