{"id":2711,"date":"2022-01-09T21:32:03","date_gmt":"2022-01-09T21:32:03","guid":{"rendered":"https:\/\/dft.wiki\/?p=2711"},"modified":"2022-01-09T21:32:11","modified_gmt":"2022-01-09T21:32:11","slug":"affine-cipher-cheat-sheet","status":"publish","type":"post","link":"https:\/\/dft.wiki\/?p=2711","title":{"rendered":"Affine Cipher Cheat Sheet"},"content":{"rendered":"<p>Affine Cipher is a relatively simple way for encrypting\/decrypting data with low CPU cost but with the expensive of low level of security.<\/p>\n<p>Basic Syntax:\u00a0 \u00a0 \u00a0<strong>f <\/strong><span id=\"MathJax-Span-2\" class=\"mrow\"><strong><span id=\"MathJax-Span-4\" class=\"mo\">( <\/span><span id=\"MathJax-Span-5\" class=\"mi\">X <\/span><span id=\"MathJax-Span-6\" class=\"mo\">) = \u03b1 X<\/span><span id=\"MathJax-Span-9\" class=\"mi\">\u00a0<\/span><span id=\"MathJax-Span-10\" class=\"mo\">+ \u03b2 <\/span><\/strong><span id=\"MathJax-Span-14\" class=\"mo\">(<\/span><span id=\"MathJax-Span-15\" class=\"mi\">mod <\/span><span id=\"MathJax-Span-16\" class=\"mspace\"><\/span><span id=\"MathJax-Span-17\" class=\"mn\">26<\/span><span id=\"MathJax-Span-18\" class=\"mo\">)<\/span><\/span><\/p>\n<p>Note: the mod 26 means this cipher is using the English alphabet, which contains 26 characters:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignleft wp-image-2712 size-full\" src=\"https:\/\/dft.wiki\/wp-content\/uploads\/sites\/15\/2022\/01\/alphabet.png\" alt=\"\" width=\"664\" height=\"63\" srcset=\"https:\/\/dft.wiki\/wp-content\/uploads\/sites\/15\/2022\/01\/alphabet.png 664w, https:\/\/dft.wiki\/wp-content\/uploads\/sites\/15\/2022\/01\/alphabet-300x28.png 300w\" sizes=\"auto, (max-width: 664px) 100vw, 664px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<ul>\n<li><strong>X<\/strong> is the correspondent value of the character that will be encrypted.<\/li>\n<li>( <span id=\"MathJax-Span-2\" class=\"mrow\"><strong><span id=\"MathJax-Span-6\" class=\"mo\">\u03b1 ,<\/span><span id=\"MathJax-Span-10\" class=\"mo\">\u00a0\u03b2<\/span><\/strong><\/span> ) is a valid key if:\n<ul>\n<li>gcd ( <span id=\"MathJax-Span-2\" class=\"mrow\"><strong><span id=\"MathJax-Span-6\" class=\"mo\">\u03b1<\/span><\/strong><\/span> , 26 ) = 1<\/li>\n<li>1 \u2264 <span id=\"MathJax-Span-2\" class=\"mrow\"><strong><span id=\"MathJax-Span-6\" class=\"mo\">\u03b1<\/span><\/strong><\/span> \u2264 25<\/li>\n<li>0 \u2264 <span id=\"MathJax-Span-2\" class=\"mrow\"><strong><span id=\"MathJax-Span-10\" class=\"mo\">\u03b2<\/span><\/strong><\/span> \u2264 25<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>Let&#8217;s walk through the encryption and decryption process in the Python code below that is a fork of [<a href=\"https:\/\/www.geeksforgeeks.org\/implementation-affine-cipher\/\">Link<\/a>].<\/p>\n<p>Encryption:<\/p>\n<pre>def <strong>egcd<\/strong>(a, b):\r\n\tx,y, u,v = 0,1, 1,0\r\n\twhile a != 0:\r\n\t\tq, r = b\/\/a, b%a\r\n\t\tm, n = x-u*q, y-v*q\r\n\t\tb,a, x,y, u,v = a,r, u,v, m,n\r\n\tgcd = b\r\n\treturn gcd, x, y\r\n\r\ndef <strong>modinv<\/strong>(a, m):\r\n\tgcd, x, y = egcd(a, m)\r\n\tif gcd != 1:\r\n\t\treturn None\r\n\telse:\r\n\t\treturn x % m\r\n\r\ndef <strong>affine_encrypt<\/strong>(text, key):\r\n\t'''\r\n\tC = (a*P + b) % 26\r\n\t'''\r\n\treturn ''.join([ chr((( key[0]*(ord(t) - ord('A')) + key[1] ) % 26) + ord('A')) for t in text.upper().replace(' ', '') ])\r\n\r\ntext = '<span style=\"color: #008000;\"><strong>PlainText<\/strong><\/span>'\r\nkey = [<span style=\"color: #ff0000;\"><strong>3<\/strong><\/span>, <span style=\"color: #ff0000;\"><strong>18<\/strong><\/span>]\r\n\r\nprint(affine_encrypt(text, key))<\/pre>\n<p>Decryption:<\/p>\n<pre>def <strong>egcd<\/strong>(a, b):\r\n\tx,y, u,v = 0,1, 1,0\r\n\twhile a != 0:\r\n\t\tq, r = b\/\/a, b%a\r\n\t\tm, n = x-u*q, y-v*q\r\n\t\tb,a, x,y, u,v = a,r, u,v, m,n\r\n\tgcd = b\r\n\treturn gcd, x, y\r\n\r\ndef <strong>modinv<\/strong>(a, m):\r\n\tgcd, x, y = egcd(a, m)\r\n\tif gcd != 1:\r\n\t\treturn None\r\n\telse:\r\n\t\treturn x % m\r\n\r\ndef <strong>affine_decrypt<\/strong>(cipher, key):\r\n\t'''\r\n\tP = (a^-1 * (C - b)) % 26\r\n\t'''\r\n\treturn ''.join([ chr((( modinv(key[0], 26)*(ord(c) - ord('A') - key[1])) % 26) + ord('A')) for c in cipher ])\r\n\r\ncipher = '<span style=\"color: #008000;\"><strong>LZSQFXEJX<\/strong><\/span>'\r\nkey = [<span style=\"color: #ff0000;\"><strong>3<\/strong><\/span>, <span style=\"color: #ff0000;\"><strong>18<\/strong><\/span>]\r\nprint(affine_decrypt(cipher, key))<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Affine Cipher is a relatively simple way for encrypting\/decrypting data with low CPU cost but [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[],"class_list":["post-2711","post","type-post","status-publish","format-standard","hentry","category-hacking"],"_links":{"self":[{"href":"https:\/\/dft.wiki\/index.php?rest_route=\/wp\/v2\/posts\/2711","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dft.wiki\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dft.wiki\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dft.wiki\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dft.wiki\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2711"}],"version-history":[{"count":1,"href":"https:\/\/dft.wiki\/index.php?rest_route=\/wp\/v2\/posts\/2711\/revisions"}],"predecessor-version":[{"id":2713,"href":"https:\/\/dft.wiki\/index.php?rest_route=\/wp\/v2\/posts\/2711\/revisions\/2713"}],"wp:attachment":[{"href":"https:\/\/dft.wiki\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2711"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dft.wiki\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2711"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dft.wiki\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2711"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}