Cryptic Text Encoding

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’d learn something.

One such program I typed in completely surprised me. The output displayed wasn’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.

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.

```#include <stdio.h>

int main()
{
int x, c;
signed int delta[] = {
72, 29, 7, 0, 3, -79, 70, 12, -3, -2,
-77, 36, 29, 13, -78, 39, 40, 0, -4, -2,
5, -77, -23, -10
};

x = c = 0;
while(x<24)
{
c += delta[x];
putchar(c);
x++;
}
return(0);
}```

The values in the `delta[]` array aren’t ASCII codes. Yet, when you run the program, you see the following text output:

`Hello from Dan Gookin!`

What happens in the while loop is that values are read from the `delta[]` array and output via a putchar() function at Line 16. The first value is an ASCII code, 72, for the letter `H`. 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.

The effect is that 29 (the second element in the `delta[]` array) is added to 72, yielding 101 or the ASCII code for the letter `e`. The next element, adds 7 to that value, which becomes letter `l`. That’s the way the string is processed, one character after another.

I’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; `(x<24)` at Line 13.

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. Next week’s Lesson details how the program works.

3 thoughts on “Cryptic Text Encoding”

1. I’ve seen programs with decimal numbers hardcoded in which are then printed as hexadecimal, forming words. Bit restrictive obviously: you can only output word containing the first six letters of the alphabet.

Of course in c chars are basically numbers so it’s easy to represent text with integers.

2. I’ve experimented with other ways to represent letters, including packing bits across multiple bytes. I wish I would have saved the original Assembly code used so many years ago. It really stuck with me.

3. It’s probably still out there somewhere if you know where to look or what to search for (I know neither obviously!) Failing that ask on appropriate forums. Somebody somewhere must know. I expect there’s an Assembly subreddit somewhere.