Sinisterly
Encoder/Decoder Challenge/Source - Printable Version

+- Sinisterly (https://sinister.li)
+-- Forum: Coding (https://sinister.li/Forum-Coding)
+--- Forum: C, C++, & Obj-C (https://sinister.li/Forum-C-C-Obj-C)
+--- Thread: Encoder/Decoder Challenge/Source (/Thread-Encoder-Decoder-Challenge-Source)



Encoder/Decoder Challenge/Source - killerOfCode - 05-13-2012

Hello again, community! I had a challenge for myself to create a encoder and a decoder program using timestamps. So, now I will present it to you with two options. 1 - Read the code and use it, or, 2 - Take the challenge for yourself. Here are the specs for the encoder:

asks for string, number, randomizes each character from the number, and displays the output.

Same thing with the decoder, it just does the opposite. :ok:

Anyway, here is my code if you don't want to take the challenge:
Spoiler:
Code:
#include <iostream> #include <conio.h> using namespace std; const int SIZE = 25565; char encode(char toEncode[SIZE], int timeStamp) { srand(timeStamp); int i = 0, j = 0; while(toEncode[i] != '\0') { if(static_cast<int>(toEncode[i]) % 2) toEncode[i] += rand() % (10 + 1) * 2; else toEncode[i] += rand() % (20 + 10) * 2 + 1; i++; j++; } for(i = 0; i <= j; i++) cout << toEncode[i] << " "; return 0; } int main() { char carray[SIZE] = { }, tstamp; cout << "string: "; cin.getline(carray, SIZE); cout << "time stamp: "; cin >> tstamp; encode(carray, tstamp); getch(); return 0; }

and
Spoiler:
Code:
#include <iostream> #include <conio.h> using namespace std; const int SIZE = 25565; char decode(char toEncode[SIZE], int timeStamp) { srand(timeStamp); int i = 0, j = 0; while(toEncode[i] != '\0') { if(static_cast<int>(toEncode[i]) % 2) toEncode[i] -= rand() % (10 + 1) * 2; else toEncode[i] -= rand() % (20 + 10) * 2 + 1; i++; j++; } for(i = 0; i <= j; i++) cout << toEncode[i] << " "; return 0; } int main() { char carray[SIZE] = { }, tstamp; cout << "string: "; cin >> carray; cout << "time stamp: "; cin >> tstamp; decode(carray, tstamp); getch(); return 0; }



RE: Encoder/Decoder Challenge/Source - killerOfCode - 09-19-2012

Wow, this post is a couple months old and nobody posted. lol.


RE: Encoder/Decoder Challenge/Source - Frooxius - 09-19-2012

How is it a timestamp when it's an user entered number? Timestamp is a number that determines some time and date, but the way you attempt to use it in this code makes it a password/key, it's not a timestamp. When you use it to initialize the random number generator, it's called a seed.

I see a bunch of weird things in this code. Like... why do you declare the function encode() as returning char, when you just return 0 (null character) in the end? What's the purpose of that? Why does it always return the same value that's basically useless? If you don't need to return any value from a function, just declare it as a void.

However... this function should return something and that something should be the the encoded/decoded (although better term would be encrypted/decrypted) string. Your function prints it instead, which is a very bad thing to do. It says "encode" so it should do that, nothing else.

A rule of thumb for writing a good code is - functions should never do anything in addition to what they're supposed to do, ideally they should have no side effects.

If the function is supposed to encode some string, it should do that and return the encoded string, nothing else. What if you wanted to store it somewhere instead? Using it internally? You can't, because it doesn't return it to you, it just prints in on the screen...

Instead, you should encode it and return it and then print the encoded string if you want. There are multiple ways to do that, either dynamically allocate memory in the function, write the encoded string into it and return a pointer to it, however then you have to remember to free this memory outside of the function.

The other, more traditional way of doing this is by passing a pointer to a buffer where to store the encoded/decoded string and optionally also providing maximum length for safety, like this:

Code:
void Encode(char *toEncode, char *buffer, int maxsize);

And write the encoded string to the buffer inside of a function.

Also you can simplify the
Code:
while(toEncode[i] != '\0')
by writing
Code:
while(toEncode[i])

Basically any value that's non-zero is considered true and '\0' is equal the number zero, so when the character at index i is zero, the loop will stop.

Anyway, I didn't look at your code much in detail, these are only things that I've noticed at first glance, hope this helps you to improve and others too.


RE: Encoder/Decoder Challenge/Source - killerOfCode - 09-19-2012

It works for me, and I think this may be an old version... Sorry. :'(


RE: Encoder/Decoder Challenge/Source - Frooxius - 09-19-2012

What do you mean that it works for you? I haven't said that it doesn't work (I didn't really look into that, but it seems as a rather trivial shift encryption at glance), but that it's poorly written and suggested good programming practices to use.


RE: Encoder/Decoder Challenge/Source - killerOfCode - 09-19-2012

(09-19-2012, 06:26 PM)Frooxius Wrote: What do you mean that it works for you? I haven't said that it doesn't work (I didn't really look into that, but it seems as a rather trivial shift encryption at glance), but that it's poorly written and suggested good programming practices to use.


Sorry, I looked over my code again and I realize that this was an *extremely* old post and I've revised it tons. Anyway...

[EDIT: Also, I'm not active much in the forums, even though I hope to be more often... Wink]


RE: Encoder/Decoder Challenge/Source - Frooxius - 09-19-2012

But you bumped up the topic up not a month ago, without providing a new version for a review, so I was going on by what you provided.


RE: Encoder/Decoder Challenge/Source - killerOfCode - 09-28-2012

Well, it was a challenge for the community to participate in. Wink