Remove the bug

This commit is contained in:
Johannes Røsvik 2020-05-24 15:15:42 +02:00
parent 5fca4cd283
commit 0e50709c23
No known key found for this signature in database
GPG Key ID: 8A47E30339E13FFD
1 changed files with 7 additions and 20 deletions

View File

@ -1,12 +1,5 @@
{ {
"cells": [ "cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{ {
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
@ -713,7 +706,7 @@
"3. Select $e$ randomly with $gcd(e, \\varphi(n)) = 1$.\n", "3. Select $e$ randomly with $gcd(e, \\varphi(n)) = 1$.\n",
"4. Compute $d = e1 \\mod \\varphi(n)$.\n", "4. Compute $d = e1 \\mod \\varphi(n)$.\n",
"5. The public key is the pair $n$ and $e$.\n", "5. The public key is the pair $n$ and $e$.\n",
"6. The private key consists of the values $p$, $q$ and $d$.\n", "6. The private key consists of the values $p$, $q$ (or $n = pq$) and $d$.\n",
"\n", "\n",
"### Encryption\n", "### Encryption\n",
"\n", "\n",
@ -723,13 +716,13 @@
"\n", "\n",
"### Decryption\n", "### Decryption\n",
"\n", "\n",
"The private key for decryption is KD = d (values p and q are not used here).\n", "The private key for decryption is $KD = d$ (values $p$ and $q$ are not used here).\n",
"1. Compute D(C,KD) = Cd mod n = M." "1. Compute $D(C,KD) = Cd \\mod n = M$."
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 24, "execution_count": 14,
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
@ -785,15 +778,13 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 22, "execution_count": 15,
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
"name": "stdout", "name": "stdout",
"output_type": "stream", "output_type": "stream",
"text": [ "text": [
"C = M^e (mod n) = 50^102900819 (mod 604604729) = 2488\n",
"\n",
"Ciphertext: 2488\n" "Ciphertext: 2488\n"
] ]
} }
@ -809,28 +800,25 @@
"\n", "\n",
"M = 50\n", "M = 50\n",
"C = rsa_encrypt(M, rsa_keys)\n", "C = rsa_encrypt(M, rsa_keys)\n",
"print(f\"C = M^e (mod n) = {M}^{e} (mod {n}) = {C}\\n\")\n",
"print(f\"Ciphertext: {C}\")" "print(f\"Ciphertext: {C}\")"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 23, "execution_count": 16,
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
"name": "stdout", "name": "stdout",
"output_type": "stream", "output_type": "stream",
"text": [ "text": [
"M = C^d (mod n) = 2488^1 (mod 604604729) = 50\n",
"\n",
"Message: 50\n" "Message: 50\n"
] ]
} }
], ],
"source": [ "source": [
"def rsa_decrypt(C, rsa_keys):\n", "def rsa_decrypt(C, rsa_keys):\n",
" # private key\n", " # Private key\n",
" p = rsa_keys['p']\n", " p = rsa_keys['p']\n",
" q = rsa_keys['q']\n", " q = rsa_keys['q']\n",
" d = rsa_keys['d']\n", " d = rsa_keys['d']\n",
@ -840,7 +828,6 @@
" return square_and_multiply(C,d,n)\n", " return square_and_multiply(C,d,n)\n",
"\n", "\n",
"M = rsa_decrypt(C, rsa_keys)\n", "M = rsa_decrypt(C, rsa_keys)\n",
"print(f\"M = C^d (mod n) = {C}^{d} (mod {n}) = {M}\\n\")\n",
"print(f\"Message: {M}\")" "print(f\"Message: {M}\")"
] ]
} }