{"id":1794,"date":"2016-03-05T00:01:09","date_gmt":"2016-03-05T08:01:09","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=1794"},"modified":"2016-03-12T08:14:02","modified_gmt":"2016-03-12T16:14:02","slug":"brute-force-permutations","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=1794","title":{"rendered":"Brute Force Permutations"},"content":{"rendered":"<p>If Arthur C. Clarke&#8217;s story <em>The Nine Billion Names of God<\/em> were true, then it seems rather pointless to plow through all 9,000,000,000 permutations to find one matching name. In fact, the exercise is more like a brute-force password cracking program than some celestial name search. What if the monks already knew the name? That would save time and effort.<br \/>\n<!--more--><br \/>\nAssume that out of the billions of letter combinations, the monks knew that the Big Guy&#8217;s holy password contained six letters, all lower case. If so, you could craft a program that would spin through all letter combinations and eventually spit out the match. This technique is pretty much how password cracking programs work, albeit at a very crude level.<\/p>\n<p>The following code demonstrates how nested <em>for<\/em> loops can permute letter combinations in an attempt to match a preset password, <code>zzyxx<\/code>.<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;string.h&gt;\r\n\r\nint main()\r\n{\r\n    char password[6] = \"zzyxx\";\r\n    char brute[6] = \"\\0\";\r\n\r\n    <span class=\"comments.\"\/* start the comparison *\/<\/span>\r\n    for(brute[0]='a';brute[0]&lt;='z';brute[0]++)\r\n        for(brute[1]='a';brute[1]&lt;='z';brute[1]++)\r\n            for(brute[2]='a';brute[2]&lt;='z';brute[2]++)\r\n                for(brute[3]='a';brute[3]&lt;='z';brute[3]++)\r\n                    for(brute[4]='a';brute[4]&lt;='z';brute[4]++)\r\n                        if( strcmp(brute,password)==0 )\r\n                        {\r\n                            printf(\"%s -&gt; %s\\n\",brute,password);\r\n                            return(0);\r\n                        }\r\n\r\n    return(0);\r\n}<\/pre>\n<p>Line 6 defines the <code>password<\/code>, the target text to match.<\/p>\n<p>In Line 7, the <code>brute[]<\/code> <em>char<\/em> buffer is initialized to null characters. This step is important! if you don&#8217;t initialize the buffer, the strings may never match.<\/p>\n<p>The point of all the <em>for<\/em> loops is to fill the <code>brute[]<\/code> array with letters <code>aaaaaa<\/code> through <code>zzzzzz<\/code>. At each permutation, the <em>strcmp()<\/em> function checks to see whether the letters match those stored in string <code>password<\/code>. If so, the looping madness stops and the result is displayed:<\/p>\n<pre><code>zzyxx -> zzyxx<\/code><\/pre>\n<p>The big problem with this brute force method is time; nested loops consume a lot of processing power. The more loops you have, the slower the code runs. Eventually, adding yet another loop to the nest makes the program crawl.<\/p>\n<blockquote><p>Back in the old microcomputer days, programmers used loops to delay execution. On my TRS-80, a <em>for<\/em> loop that counted to 2400 would pause the program one second. As processor technology improved, these timing loops were dropped in favor of using the computer&#8217;s internal clock. In fact, many early PC games can no longer be played on a modern PC because their timing loops run too quickly.<\/p><\/blockquote>\n<p>Another problem is a lack of flexibility: You must re-write the code to accommodate a longer or shorter target string. One solution to handle that issue is to use recursion instead of writing nested loops. I&#8217;ll present that solution in <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=1800\">next week&#8217;s Lesson<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Stack a buncha <em>for<\/em> loops and you can permute just about any text. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=1794\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-1794","post","type-post","status-publish","format-standard","hentry","category-main"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1794","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1794"}],"version-history":[{"count":4,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1794\/revisions"}],"predecessor-version":[{"id":1814,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1794\/revisions\/1814"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1794"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1794"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1794"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}