{ "cells": [ { "cell_type": "markdown", "id": "a35eeb9f-df70-4ab1-a243-2d2025888eb0", "metadata": {}, "source": [ "# Exercice 1\n", "1) Codez une fonction qui permet de chiffrer avec le chiffrement parfait (C = M ⊕ K). Cette\n", "fonction prend en entrée une clé et un message, tous deux sous forme de chaîne de caractères,\n", "et sa sortie est également une chaîne de caractères. Vous aurez peut-être l’usage de l’opérateur\n", "∧." ] }, { "cell_type": "code", "execution_count": 59, "id": "57595af7-bd28-427e-b407-eb0bf49f483a", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "72" ] }, "execution_count": 59, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ord('H')" ] }, { "cell_type": "code", "execution_count": 1, "id": "06c3d56f-6872-4179-a1f8-e2a44ba117f3", "metadata": {}, "outputs": [], "source": [ "def bit(nombre, n):\n", " resBin = [0]*n\n", " i = 0\n", " while nombre > 0:\n", " resBin[n-1-i] = nombre%2\n", " nombre = nombre // 2\n", " i += 1\n", " return resBin\n", "\n", "def sToBit(s):\n", " return bit(ord(s),8)" ] }, { "cell_type": "code", "execution_count": 2, "id": "277ebad9-936c-4e84-9f3c-b45bb69dd388", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[0, 1, 0, 0, 1, 0, 0, 0]" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sToBit(\"H\")" ] }, { "cell_type": "code", "execution_count": 3, "id": "c1376cfe-4eae-4323-8ec2-6fd5e3504f68", "metadata": {}, "outputs": [], "source": [ "def stringToBit(string):\n", " resBin = []\n", " for i in string:\n", " resBin.append(sToBit(i))\n", " return resBin" ] }, { "cell_type": "code", "execution_count": 4, "id": "6f9311ce-b1d9-458a-bdb7-ce9cd2dc8f32", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[[0, 1, 0, 0, 1, 0, 0, 0],\n", " [0, 1, 1, 0, 0, 1, 0, 1],\n", " [0, 1, 1, 0, 1, 1, 0, 0],\n", " [0, 1, 1, 0, 1, 1, 0, 0],\n", " [0, 1, 1, 0, 1, 1, 1, 1]]" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "stringToBit(\"Hello\")" ] }, { "cell_type": "code", "execution_count": 5, "id": "7b1e1f48-45e8-4b60-94c2-f3fa2a1ac7e8", "metadata": {}, "outputs": [], "source": [ "def chiffrement(cle, message):\n", " cleBin = stringToBit(cle)\n", " messageBin = stringToBit(message)\n", " print(cleBin[0][0])\n", " print(message)\n", " res = \"\"\n", " for i in range(len(message)):\n", " for j in range(8):\n", " res = res + str(cleBin[i][j] ^ messageBin[i][j])\n", " return res" ] }, { "cell_type": "code", "execution_count": 6, "id": "8515d99f-d0e2-4da5-a186-3ed78e233b02", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "Hello\n" ] }, { "data": { "text/plain": [ "'0000101100000000000111110000110100011101'" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "chiffrement(\"Cesar\", \"Hello\")" ] }, { "cell_type": "markdown", "id": "9211c83a-2fde-4614-b066-36a1b09d9ff3", "metadata": {}, "source": [ "# Exercice 2" ] }, { "cell_type": "code", "execution_count": 8, "id": "bfd0dfc8-0ebb-4103-a12d-0ccfcdcb15aa", "metadata": {}, "outputs": [], "source": [ "def LSFR(s, c, nbtours):\n", " i = 0\n", " resTot = s\n", " t = len(s)\n", " while i < nbtours:\n", " res = s[t]\n", " j = t\n", " \n", " for j in range(t):\n", " print(c[j])\n", " res = res ^ c[j]\n", " for j in range(t):\n", " if i == 1:\n", " resTot[0] = res\n", " break\n", " resTot[t - 1] = resTot[t - 2]\n", " return resTot" ] }, { "cell_type": "code", "execution_count": 9, "id": "a2bbdc83-30f3-4aca-9026-b2a2aba64171", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "4\n", "4\n", "4\n", "4\n" ] }, { "ename": "IndexError", "evalue": "list index out of range", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mIndexError\u001b[0m Traceback (most recent call last)", "Cell \u001b[0;32mIn[9], line 3\u001b[0m\n\u001b[1;32m 1\u001b[0m s \u001b[38;5;241m=\u001b[39m [\u001b[38;5;241m0\u001b[39m, \u001b[38;5;241m0\u001b[39m, \u001b[38;5;241m1\u001b[39m, \u001b[38;5;241m0\u001b[39m]\n\u001b[1;32m 2\u001b[0m c \u001b[38;5;241m=\u001b[39m [\u001b[38;5;241m1\u001b[39m, \u001b[38;5;241m0\u001b[39m, \u001b[38;5;241m1\u001b[39m, \u001b[38;5;241m0\u001b[39m]\n\u001b[0;32m----> 3\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[43mLSFR\u001b[49m\u001b[43m(\u001b[49m\u001b[43ms\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mc\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m10\u001b[39;49m\u001b[43m)\u001b[49m)\n", "Cell \u001b[0;32mIn[8], line 6\u001b[0m, in \u001b[0;36mLSFR\u001b[0;34m(s, c, nbtours)\u001b[0m\n\u001b[1;32m 4\u001b[0m t \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mlen\u001b[39m(s)\n\u001b[1;32m 5\u001b[0m \u001b[38;5;28;01mwhile\u001b[39;00m i \u001b[38;5;241m<\u001b[39m nbtours:\n\u001b[0;32m----> 6\u001b[0m res \u001b[38;5;241m=\u001b[39m \u001b[43ms\u001b[49m\u001b[43m[\u001b[49m\u001b[43mi\u001b[49m\u001b[43m]\u001b[49m\n\u001b[1;32m 7\u001b[0m j \u001b[38;5;241m=\u001b[39m t\n\u001b[1;32m 8\u001b[0m \u001b[38;5;28mprint\u001b[39m(j)\n", "\u001b[0;31mIndexError\u001b[0m: list index out of range" ] } ], "source": [ "s = [0, 0, 1, 0]\n", "c = [1, 0, 1, 0]\n", "print(LSFR(s, c, 10))" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.11.5" } }, "nbformat": 4, "nbformat_minor": 5 }