Having some problems creating an encoder for class. I have the decoder, but I can't seem to make my encoder work properly.

I have attached the decoder to the post as a file.

Encryption example: "Hello" would be "xxxxx!H#xxxxxxx#e¤xxx¤l&xxxxxxxxx&l$xxxxxxxxxxx$o", where x is randomly generated letters, and ! is the first key. After the first key comes the first character in the message, and then the next key.

What I am having problems with is simply what to use... I tried using only char[] and loops, but was unable to separate the independent characters in the message when introducing them to the encoded string. I got H, He, Hel, Hell, Hello in the encoded string instead of the single characters. Perhaps I have to use arrays?

For creating new keys, I used

#include <iostream>
#include <ctime>

using namespace std;

int main() {
    srand((unsigned) time(NULL));
    
    char c = (char) (rand() % 26 + 'a');
    cout << char(c) << endl;
    cout << int(c) << endl;
    

}

However, I was unable to integrate them into the encryption due to type differance. I managed to go from char to char[], but I got the ASCII number instead of the letter.

Meh. Any suggestions?

Recommended Answers

All 4 Replies

I can't analyze the code you attached since it's in a language I don't know. But here's an example of a very simple encoding technique that simply pads to "encrypt" (not really encryption), and takes out the pads to "decrypt". It uses a character array and isolates characters using the [] operator.

#include <iostream>
#include <cmath>
#include <ctime>
#include <cstring>
using namespace std;


void encrypt (char* plaintext, char* encrypted)
{
   // stick in five random characters for each real character.

   int length = strlen (plaintext);

   for (int i = 0; i < length; i++)
   {
     encrypted[i * 6] = plaintext[i];
     for (int j = 1; j < 6; j++)
     {
         char randomLetter = (rand () % 26) + 'a';
         encrypted[i * 6 + j] = randomLetter;
     }
   }

   encrypted[length * 6] = 0; // null terminator
}

void decrypt (char* plaintext, char* encrypted)
{
   // remove random characters

   int length = strlen (encrypted);

   for (int i = 0; i < length; i+=6)
   {
       plaintext[i/6] = encrypted[i];
   }

   plaintext[length / 6] = 0; // null terminator
}

int main ()
{
    srand (time (NULL));

    char plaintext[11];
    char encrypted[61];

    cout << "Enter plaintext (length <= 10 characters) : ";
    cin >> plaintext;

    encrypt (plaintext, encrypted);
    cout << "Encrypted : " << encrypted << endl;
    decrypt (plaintext, encrypted);
    cout << "Decrypted : " << plaintext << endl;

    return 0;
}

Thanks for the reply. I did not think of using operators inside the brackets, so this helped alot. Now I just have to figure out some way to add a key in front of every character which is part of the message, using the method described in my first post. I would have to add something like this:

Message: SECRET
Output: goakaSogajkoErasjkarCiagkalaiRkagakEqagklqTakgal

Using caps for viewing convenience in this example. The letter in front of the first part of the message, 'S', is a. This is the first key. The letter just after 'S', is 'o'. This is the next key. The next 'o' in the string is followed by the next part of the message, 'E', which is in turn followed by the next key, 'r'. The next 'r' is followed by the next part of the message, 'C', and so on.

I guess I could just use the position of message characters +/- 1 to place these keys, but they would have to follow the pattern AB BC CD DE etc...

Now then, how would I go about implementing this pattern...

If you know the first key then maybe something like this will work.

Encrypt:
while there are letters to encrypt
generate random letters until current key is generated
place a letter of message
generate random letter to act as next key
repeat

Decrypt---need to know first key
while no further characters in message
find current key
extract a letter of message
find next key
repeat

Think I have it now:

#include <iostream>
#include <ctime>
#include <string>
using namespace std;

void main()
{
	char spam = 'k';
	char key = 'r';
	string melding;
	bool end = false;
	unsigned int seed = time(0);
	srand(seed);
	int i = 0;

	getline(cin, melding);

	while(spam != key && end != true)
	{
		spam = 65 + rand() % 63;
		cout << spam;
		
		if(spam == key)
		{
			cout <<  melding[i];
			key = 65 + rand() % 63;
			cout << key;
			i++;
		}
		if(melding[i] == '\0')
		{
			end = true;
		}

	}


	cout << endl << endl;


}

Confirmed working by my decoder app :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.