{"id":3581,"date":"2019-05-04T00:01:37","date_gmt":"2019-05-04T07:01:37","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=3581"},"modified":"2019-05-11T08:42:13","modified_gmt":"2019-05-11T15:42:13","slug":"cryptic-text-encoding","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=3581","title":{"rendered":"Cryptic Text Encoding"},"content":{"rendered":"<p>Typing in sample code was all the rage back when I first learned assembly language. The programs were short. Most of them I could understand based on my limited knowledge, but I still enjoyed seeing the result and wondering how they did it. Hopefully, I&#8217;d learn something.<br \/>\n<!--more--><br \/>\nOne such program I typed in completely surprised me. The output displayed wasn&#8217;t to be found anywhere inside the code. Somehow, the programmer was able to get a text message generated from what looked like random data. It took me a while, but eventually I gleaned what was going on.<\/p>\n<p>The sample code below works along a similar principle to the assembly language code I looked at years ago. You see an integer array of what appears to be various positive and negative values.<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    int x, c;\r\n    signed int delta[] = {\r\n        72, 29, 7, 0, 3, -79, 70, 12, -3, -2,\r\n        -77, 36, 29, 13, -78, 39, 40, 0, -4, -2,\r\n        5, -77, -23, -10\r\n    };\r\n\r\n    x = c = 0;\r\n    while(x&lt;24)\r\n    {\r\n        c += delta[x];\r\n        putchar(c);\r\n        x++;\r\n    }\r\n    return(0);\r\n}<\/pre>\n<p>The values in the <code>delta[]<\/code> array aren&#8217;t ASCII codes. Yet, when you run the program, you see the following text output:<\/p>\n<p><code>Hello from Dan Gookin!<\/code><\/p>\n<p>What happens in the <em>while<\/em> loop is that values are read from the <code>delta[]<\/code> array and output via a <em>putchar()<\/em> function at Line 16. The first value is an ASCII code, 72, for the letter <code>H<\/code>. Instead of the next element being an ASCII code, however, its value is added to the current ASCII value. Or, in the case of a negative number, the value is subtracted.<\/p>\n<p>The effect is that 29 (the second element in the <code>delta[]<\/code> array) is added to 72, yielding 101 or the ASCII code for the letter <code>e<\/code>. The next element, adds 7 to that value, which becomes letter <code>l<\/code>. That&#8217;s the way the string is processed, one character after another.<\/p>\n<p>I&#8217;m not certain whether this method was the one used as an example in Assembly language years ago, nor is it any form of clever encryption. In fact, the only conundrum I faced was how to tell when the string was terminated. Rather than use some value such as 1000, I just counted the bytes in the string and used that value to terminate output; <code>(x&lt;24)<\/code> at Line 13.<\/p>\n<p>This program is actually the second one I wrote. After all, you need a way to convert a string of text into positive and negative values. To do that I wrote a program that swallows a string and then outputs the values added and subtracted. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=3595\">Next week&#8217;s Lesson<\/a> details how the program works.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Since I first learned to program, I&#8217;ve been fascinated by programs that hide text. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=3581\">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-3581","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\/3581","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=3581"}],"version-history":[{"count":4,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3581\/revisions"}],"predecessor-version":[{"id":3602,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3581\/revisions\/3602"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3581"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3581"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3581"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}