{"id":3595,"date":"2019-05-11T00:01:20","date_gmt":"2019-05-11T07:01:20","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=3595"},"modified":"2019-05-06T11:06:10","modified_gmt":"2019-05-06T18:06:10","slug":"the-cryptic-text-encoder","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=3595","title":{"rendered":"The Cryptic Text Encoder"},"content":{"rendered":"<p>In <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=3581\">last week&#8217;s Lesson<\/a>, I waxed on about how a text message can be concealed in a delightful manner, making it not look like a text message at all. To get to that point, you must write some code that translates plain text into the values desired.<br \/>\n<!--more--><br \/>\nFor this type of encryption, the code munches through a string of text in this manner:<\/p>\n<ol>\n<li>The first character of the string is output as its ASCII value.<\/li>\n<li>Obtain the next character of the string.<\/li>\n<li>Subtract the previous character&#8217;s ASCII value<\/li>\n<li>Output the result.<\/li>\n<li>If the newline is encountered, stop, otherwise repeat at Step 2.<\/li>\n<\/ol>\n<p>Here&#8217;s the code I concocted:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\n#define SIZE 32\r\n\r\nint main()\r\n{\r\n    char input[SIZE];\r\n    signed int x,ch;\r\n\r\n    printf(\"Text: \");\r\n    fgets(input,SIZE,stdin);\r\n\r\n    x = 0;\r\n    printf(\"Translation:\\n%d\",input[x]);\r\n    while( (ch = input[x]) )\r\n    {\r\n        x++;\r\n        printf(\", %d\",input[x] - ch);\r\n        if( ch == '\\n' )\r\n            break;\r\n    }\r\n    printf(\"\\nCount: %d\\n\",x+1);\r\n    \r\n    return(0);\r\n}<\/pre>\n<p>Lines 10 and 11 fetch input. The <em>fgets()<\/em> function is used, so the string is terminated by a newline or null character, but most likely the newline.<\/p>\n<p>Line 14 outputs the initial results, which should always be at least one character long: Even if no text is input, the character at element <code>input[x]<\/code> (where <code>x<\/code> is equal to zero) is the newline. That character is output as ASCII code 10 and the program stops. Otherwise, the entire string is processed.<\/p>\n<p>The <em>while<\/em> loop at Line 15 plows through the string as long as the null character at <code>input[x]<\/code>. This character is saved in variable <code>ch<\/code>. Note how the assignment is enclosed in parentheses? The character fetched is generated as the <em>while<\/em> loop&#8217;s termination value; the null character ends the loop.<\/p>\n<p>Lines 17 and 18 test for the newline. If found, the loop is broken.<\/p>\n<p>Variable <code>x<\/code> is incremented at Line 19, referencing the next character in the string.<\/p>\n<p>At Line 20, the value of the previous character, <code>ch<\/code>, is subtracted from the current character, <code>input[x]<\/code>. This value is output. The loop repeats.<\/p>\n<p>Here&#8217;s a sample run:<\/p>\n<p><code>Text: Secret, sneaky text.<br \/>\nTranslation:<br \/>\n83, 18, -2, 15, -13, 15, -72, -12, 83, -5, -9, -4, 10, 14, -89, 84, -15, 19, -4, -70, -36<br \/>\nCount: 21<\/code><\/p>\n<p>The count value was added (at Line 22 in the code) to reference how many bytes are in the string. This value helps when writing the decoder because the values generated have no end marker. As I wrote in last week&#8217;s Lesson, the encrypted values can be positive, negative, or zero. If I assigned a specific value as the end marker, then I wouldn&#8217;t need the count. But as this code is written (encrypted and decrypted), the count value is used instead.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Before you have fun with encrypted text, it helps to have a program that does the encryption for you. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=3595\">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-3595","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\/3595","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=3595"}],"version-history":[{"count":3,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3595\/revisions"}],"predecessor-version":[{"id":3599,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3595\/revisions\/3599"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3595"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3595"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3595"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}