{"id":1826,"date":"2024-03-06T17:58:41","date_gmt":"2024-03-06T16:58:41","guid":{"rendered":"https:\/\/blog.univ-angers.fr\/mathsinfo\/?p=1826"},"modified":"2024-03-06T18:01:16","modified_gmt":"2024-03-06T17:01:16","slug":"python-cryptographie-niveau-lycee-utilisation-dune-grille","status":"publish","type":"post","link":"https:\/\/blog.univ-angers.fr\/mathsinfo\/2024\/03\/06\/python-cryptographie-niveau-lycee-utilisation-dune-grille\/","title":{"rendered":"Python &amp; Cryptographie niveau lyc\u00e9e : Utilisation d&rsquo;une grille"},"content":{"rendered":"\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"Python &amp; Cryptographie partie 3 : Chiffrement par grille\" width=\"584\" height=\"329\" src=\"https:\/\/www.youtube.com\/embed\/7pBvVEMG1-o?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n\n\n\n<p>Codes de la vid\u00e9o<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from random import randint\n\ndef grilleVide():\n grille = &#091;]\n for i in range(6):\n  ligne = &#091;]\n  for j in range(6): ligne.append('\u2b1b')\n  grille.append(ligne)\n return grille\n\ndef rotate(m, n):\n for _ in range(n): m = list(zip(*m))&#091;::-1]\n return m\n\ndef afficher(g):\n for l in range(6): print(''.join(g&#091;l]))\n\ndef creerGrille():\n    grille = grilleVide()\n    m = &#091;&#091;6 * l + c for c in range(6)] for l in range(6)]\n    for c in range(3):\n     for l in range(3):\n      m = rotate(m, randint(0, 3))\n      pos = m&#091;l]&#091;c]\n      x, y = pos % 6, pos \/\/ 6      \n      grille&#091;y]&#091;x] = '\u2b1c'\n    return grille\n    \ndef completeEtoiles(phrase):\n return phrase + \"*\" * (9 - len(phrase) % 9)\n\ndef listeTrous(g):\n trous = &#091;]\n for l in range(6):\n  for c in range(6):\n   if g&#091;l]&#091;c] == '\u2b1c': trous.append(6 * l + c)\n return trous  \n\ndef chiffrer(phrase, grille):\n phrase = completeEtoiles(phrase)\n chiffre = grilleVide()\n trous = listeTrous(grille)\n for i, v in enumerate(phrase):\n  pos = i % 9 \n  if pos == 0 and i &gt; 0:\n   grille = rotate(grille, 1)\n   trous = listeTrous(grille)\n  x, y = trous&#091;pos] % 6, trous&#091;pos] \/\/ 6\n  if v == \"*\": v = chr(randint(65, 90))\n  chiffre&#091;y]&#091;x] = v\n return chiffre \n\ndef dechiffrer(crypte, grille):\n clair = ''\n trous = listeTrous(grille)\n for i in range(36):\n  pos = i % 9 \n  if pos == 0 and i &gt; 0:\n   grille = rotate(grille, 1)\n   trous = listeTrous(grille)\n  x, y = trous&#091;pos] % 6, trous&#091;pos] \/\/ 6\n  clair += crypte&#091;y]&#091;x]\n return clair\n \ngrille = creerGrille()        \nafficher(grille)\nphrase='RENDEZVOUSAQUINZEHEURESPLACELECLERC'\ncrypte = chiffrer(phrase, grille)\nafficher(crypte)\nclair = dechiffrer(crypte, grille)\nprint(clair)\n  <\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Codes de la vid\u00e9o<\/p>\n","protected":false},"author":4913,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1826","post","type-post","status-publish","format-standard","hentry","category-non-classe"],"_links":{"self":[{"href":"https:\/\/blog.univ-angers.fr\/mathsinfo\/wp-json\/wp\/v2\/posts\/1826","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.univ-angers.fr\/mathsinfo\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.univ-angers.fr\/mathsinfo\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.univ-angers.fr\/mathsinfo\/wp-json\/wp\/v2\/users\/4913"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.univ-angers.fr\/mathsinfo\/wp-json\/wp\/v2\/comments?post=1826"}],"version-history":[{"count":2,"href":"https:\/\/blog.univ-angers.fr\/mathsinfo\/wp-json\/wp\/v2\/posts\/1826\/revisions"}],"predecessor-version":[{"id":1828,"href":"https:\/\/blog.univ-angers.fr\/mathsinfo\/wp-json\/wp\/v2\/posts\/1826\/revisions\/1828"}],"wp:attachment":[{"href":"https:\/\/blog.univ-angers.fr\/mathsinfo\/wp-json\/wp\/v2\/media?parent=1826"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.univ-angers.fr\/mathsinfo\/wp-json\/wp\/v2\/categories?post=1826"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.univ-angers.fr\/mathsinfo\/wp-json\/wp\/v2\/tags?post=1826"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}