From 018fc1723e3ce12e760311b88f5d8032af47e8df Mon Sep 17 00:00:00 2001 From: 2211275 <2211275@stud.hs-mannheim.de> Date: Thu, 1 May 2025 19:12:22 +0200 Subject: [PATCH] basic implementation --- demonstration.ipynb | 98 +++++++++++++++++++++++++++++++++++++++++++++ rabin.py | 80 ++++++++++++++++++++++++++++++++++++ 2 files changed, 178 insertions(+) create mode 100644 demonstration.ipynb create mode 100644 rabin.py diff --git a/demonstration.ipynb b/demonstration.ipynb new file mode 100644 index 0000000..2064875 --- /dev/null +++ b/demonstration.ipynb @@ -0,0 +1,98 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "id": "4f8f476d", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(10, -1, 1)" + ] + }, + "execution_count": 1, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "from rabin import encrypt, decrypt, encode, decode" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "id": "402fb09a", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "81" + ] + }, + "execution_count": 17, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "#Verschlüsseln des Buchstaben \"j\" mit (p,q) = (11,19)\n", + "p, q = 11, 19\n", + "clear_text = \"j\"\n", + "\n", + "clear = encode(clear_text)\n", + "cipher = encrypt(clear, p, q)\n", + "\n", + "cipher" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "id": "8264c687", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "('j', 'ĩ', 'Ü', '·')" + ] + }, + "execution_count": 18, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "#Entschlüsseln unter Kenntnis des Schlüssels (p, q)\n", + "possible_solutions = decrypt(cipher, p, q)\n", + "\n", + "tuple(map(decode, possible_solutions))" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.13.2" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/rabin.py b/rabin.py new file mode 100644 index 0000000..442380f --- /dev/null +++ b/rabin.py @@ -0,0 +1,80 @@ +def egcd(a, b) -> tuple[int, int, int]: + """Erweiterter Euklidischer Algorithmus + + :param a: Ganzzahl A + :type a: int + :param b: Ganzzahl B + :type b: int + :return: (ggT(a,b), a_p, b_p) mit a_p * a + b_p * b = ggT(a,b) + :rtype: tuple[int, int, int] + """ + if a == 0: + return (b, 0, 1) + else: + g, x, y = egcd(b % a, a) + return (g, y - (b // a) * x, x) + + +def encrypt(m : int, p : int, q : int) -> int: + """Verschlüsselungsfunktion + + :param m: Klartextzahl m + :type m: int + :param p: Primzahl p + :type p: int + :param q: Primzahl q + :type q: int + :return: quadratischer Rest von m^2 mod pq als Geheimtext + :rtype: int + """ + n = p * q + return (m**2) % n + +def decrypt(c : int, p : int, q : int) -> tuple[int, int, int, int]: + """Entschlüsselungsfunktion + + :param c: Geheimtext + :type c: int + :param p: Primzahl p + :type p: int + :param q: Primzahl q + :type q: int + :return: Mögliche 4 Lösungen + :rtype: tuple[int, int, int, int] + """ + # mögliche Wurzeln modulo p,q bestimmen + m_p = c**((p + 1)//4) % p + m_q = c**((q + 1)//4) % q + n = p * q + + #y_p, y_q ermitteln + d, y_p, y_q = egcd(p, q) + + # Chinesischer Restsatz + r_1 = (y_p * p * m_q + y_q * q * m_p) % n + r_2 = n - r_1 + r_3 = (y_p * p * m_q - y_q * q * m_p) % n + r_4 = n - r_3 + + # Mögliche Lösungen zurückgeben + return (r_1, r_2, r_3, r_4) + +def encode(s : str) -> int: + """Kodiert einen Buchstaben nach lateinischem Alphabet a->0, b->1, z->25 + + :param s: Buchstabe + :type s: str + :return: kodierter Buchstabe + :rtype: int + """ + return ord(s.lower()) - 97 + +def decode(i : int) -> str: + """Dekodiert einen Buchstaben nach lateinischem Alphabet 0->a, 1->b, 25->z + + :param i: Ganzzahl (bestenfalls 0 <= i <= 25) + :type i: int + :return: dekodierter Buchstabe + :rtype: str + """ + return chr(i + 97) \ No newline at end of file