{"id":1700,"date":"2015-01-19T09:02:29","date_gmt":"2015-01-19T09:02:29","guid":{"rendered":"http:\/\/www.dyalog.com\/blog\/?p=1700"},"modified":"2015-01-19T11:44:30","modified_gmt":"2015-01-19T11:44:30","slug":"solving-the-2014-apl-problem-solving-competition-cryptography-problem-1","status":"publish","type":"post","link":"https:\/\/www.dyalog.com\/blog\/2015\/01\/solving-the-2014-apl-problem-solving-competition-cryptography-problem-1\/","title":{"rendered":"Solving the 2014 APL Problem Solving Competition \u2013 Cryptography Problem 1"},"content":{"rendered":"<p>This post is a continuation of the series where we examine some of the problems selected for the 2014 <a href=\"http:\/\/www.dyalogaplcompetition.com\/\" target=\"_blank\">APL Problem Solving Competition<\/a>. I&#8217;ll start by looking at the cryptography problems from <a href=\"http:\/\/www.dyalog.com\/uploads\/files\/student_competition\/2014_problems_phase2.pdf\" target=\"_blank\">Phase II<\/a>.<\/p>\n<h2>Cryptography Problem 1 \u2013 Vigen\u00e8re Cipher<\/h2>\n<p>The cipher is described using a large table of letters, but you don&#8217;t need to create this table in APL. Instead, you can convert each letter of the key and the corresponding letter of the message to numbers; add them together; and then convert the result back to a letter. Some subtleties:<\/p>\n<ul>\n<li>We want the result of the addition to &#8220;wrap around&#8221; at the end of the alphabet, so 25\u00a0+\u00a01\u00a0=\u00a026\u00a0(Z) but 25\u00a0+\u00a02\u00a0=\u00a027 which should wrap round to 1\u00a0(A). This is called <a href=\"http:\/\/en.wikipedia.org\/wiki\/Modular_arithmetic\" target=\"_blank\">modular arithmetic<\/a>.<\/li>\n<li>We can implement modular arithmetic easily using APL&#8217;s Residue primitive, but this works most naturally when the alphabet is numbered from 0\u00a0to\u00a025, rather than from 1\u00a0to\u00a026. To use 0-based numbering, I&#8217;ll set <code class=\"language-apl\">\u2395IO\u21900<\/code> in the code below.<\/li>\n<li>An idiomatic way of converting letters to numbers is to look them up in the <code class=\"language-apl\">\u2395A<\/code>, the upper case alphabet: <code class=\"language-apl\">\u2395A\u2373\u2375<\/code>. To convert the other way we can simply index into <code class=\"language-apl\">\u2395A<\/code>: <code class=\"language-apl\">\u2395A[\u2375]<\/code>. (For more comprehensive conversions between characters and numbers see the <code class=\"language-apl\">\u2395UCS<\/code> system function.)<\/li>\n<\/ul>\n<p>The code is then straightforward:<\/p>\n<pre><code class=\"language-apl\">VigEncrypt\u2190{\r\n    \u2395IO\u21900                       \u235d use 0-based numbering\r\n    key\u2190(\u2374\u2375)\u2374\u2395A\u2373\u237a               \u235d convert key to numbers and expand to length of message\r\n    \u2395A[26|(\u2395A\u2373\u2375)+key]           \u235d add key to message and convert back to characters\r\n}<\/code><\/pre>\n<p>To encrypt the message we added the key (modulo 26). To decrypt, we\u00a0need to subtract the key (modulo 26) instead, so we can go from <code class=\"language-apl\">VigEncrypt<\/code> to <code class=\"language-apl\">VigDecrypt<\/code> just by changing one character in the code. Note that the result of the\u00a0subtraction might be negative, but Residue (unlike the remainder or\u00a0modulo operations in some <a href=\"http:\/\/en.wikipedia.org\/wiki\/C_%28programming_language%29\" target=\"_blank\">other<\/a> <a href=\"https:\/\/java.com\/en\/\" target=\"_blank\">programming<\/a> <a href=\"http:\/\/en.wikipedia.org\/wiki\/C_Sharp_%28programming_language%29\" target=\"_blank\">languages<\/a> you may have\u00a0used) will still give us the correct result in the range 0\u00a0to\u00a025. For\u00a0example, <code class=\"language-apl\">26|\u00af3<\/code> is <code class=\"language-apl\">23<\/code>.<\/p>\n<pre><code class=\"language-apl\">VigDecrypt\u2190{\r\n    \u2395IO\u21900                       \u235d use 0-based numbering\r\n    key\u2190(\u2374\u2375)\u2374\u2395A\u2373\u237a               \u235d convert key to numbers and expand to length of message\r\n    \u2395A[26|(\u2395A\u2373\u2375)-key]           \u235d subtract key from message and convert back to characters\r\n}<\/code><\/pre>\n<p>Here are the functions in action:<\/p>\n<pre><code class=\"language-apl\">      key\u2190'KEIVERSON'\r\n      key VigEncrypt'APLISFUNTOUSE'\r\nKTTDWWMBGYYAZ\r\n      key VigDecrypt key VigEncrypt'APLISFUNTOUSE'\r\nAPLISFUNTOUSE<\/code><\/pre>\n<p>To be continued&#8230;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This post is a continuation of the series where we examine some of the problems selected for the 2014 APL Problem Solving Competition. I&#8217;ll start by looking at the cryptography problems from Phase II. Cryptography Problem 1 \u2013 Vigen\u00e8re Cipher &hellip; <a href=\"https:\/\/www.dyalog.com\/blog\/2015\/01\/solving-the-2014-apl-problem-solving-competition-cryptography-problem-1\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":15,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13,8,22],"tags":[],"class_list":["post-1700","post","type-post","status-publish","format-standard","hentry","category-algorithms","category-apl-language","category-student-competition"],"_links":{"self":[{"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/posts\/1700","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/users\/15"}],"replies":[{"embeddable":true,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/comments?post=1700"}],"version-history":[{"count":36,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/posts\/1700\/revisions"}],"predecessor-version":[{"id":1895,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/posts\/1700\/revisions\/1895"}],"wp:attachment":[{"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/media?parent=1700"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/categories?post=1700"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/tags?post=1700"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}