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?

// Filnavn:    melding.cpp
// Kommentar:  Dechiffrerer en kodet melding.
//             (Lsningsforslag til ving 3, oppgave 3a.)
// Forfatter:  Hkon Vatny (hva)
// Historie:   Nr     Hvem   Hva
//             040916  hva    Opprettet
//			   051011  jmj    Tilpasset
//             070205  jmj    Forbedret logikken og forkortet koden

#include <iostream> // "cin" og "cout"

using namespace std;

void main(){
	// Den kodete meldingen:
	char Kode[] = "sejhrMkvbekoakiouarbw34tkbo"; 
	// Finner antallet tegn i den kodete meldingen:
	const int KodeStorrelse = sizeof(Kode) / sizeof(Kode[0]);

	// I den dekodete meldingen kan det maksimalt vre en tredel s mange
	// tegn. I tillegg trenger vi et ekstra tegn for  lagre den avsluttende
	// '\0' for strengen. (Hvis den kodete meldingen inneholder 2 tegn -
	// et nkkeltegn og et meldingstegn, trenger vi 1 tegn, derfor legges 1
	// til i teller):
	const int MeldingStorrelse = (KodeStorrelse+1)/3 + 1;

	// Holder av plass til s mange tegn vi trenger. Initialiserer strengen
	// til en tom streng. Dette tilsvarer  sette det frste tegnet i
	// strengen til '\0':
	char Melding[MeldingStorrelse] = "";

	// Frste nkkeltegn er 'r':
	char Nokkel = 'r';

	// Initialiserer en indeks til  angi den kodete meldingens frste tegn, samt 
	// en indeks til til  angi skriveposisjonen i den dekodete meldingen:
	int iKode = 0, iMelding = 0;

	// Vi begynner med  lese frste tegn fra den kodete meldingen:
	//char KodetTegn = KodetMelding[iKodetMelding];

	// Itererer gjennom den kodete strengen, som slutter med tegnet '\0'.
	// Tester p iMelding slik at den holder seg innenfor MeldingStorrelse
	// for hindre at eventuelle logiske feil fr mer alvorlige konsekvenser:
	while((Kode[iKode] != '\0') && (iMelding < MeldingStorrelse)){
		// Sker etter neste nkkeltegn i den kodete strengen,
		// samtidig som vi passer p at vi ikke leser forbi terminatoren:
		while((Kode[iKode] != Nokkel) && (Kode[iKode] != '\0')){
			cout << "-";
		// Vi peker n p et nkkeltegn!    .... eller vi er kommet til slutten.

		// Tegnet etter nkkeltegnet er et tegn i meldingen ... dersom vi
		// ikke er kommet til slutten:
		if((Kode[iKode] != '\0'))
			Melding[iMelding++] = Kode[++iKode];

		// Tegnet etter meldingstegnet i den kodete strengen er ny nkkel 
		// ... dersom vi ikke er kommet til slutten:
		if((Kode[iKode] != '\0'))
			Nokkel = Kode[++iKode];         
		// Flytt indeksen som peker p den kodete meldingen n posisjon for
		//  gjre klar til sking etter neste nkkel:
	} //while
	// Terminering av strengen med den dekodete meldingen:
	// (Vi sikrer ogs mot logiske feil.)
	if(iMelding < MeldingStorrelse)
		Melding[iMelding] = '\0';
		Melding[MeldingStorrelse - 1] = '\0';

	// Presenterer den dekodete meldingen for bruker.
	cout << "Meldingen dekodet: " << endl << Melding << endl;
} // main()
7 Years
Discussion Span
Last Post by kekz0r
Featured 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 … Read More


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;

Edited by VernonDozier: n/a


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.

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

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


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);
	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;
		if(melding[i] == '\0')
			end = true;


	cout << endl << endl;


Confirmed working by my decoder app :)

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.