# Thread: A few simple questions that some1 might b able to help me with

1. ## A few simple questions that some1 might b able to help me with

Hi, Im making a basic encryption program at the moment, but im not using a normal method, instead, im using an array, some random numbers and a while loop. Anyway, the problems that Im having are as follows:

1. Writing to two files at the same time.
2. distinguishing between a random letter and a letter from the message.

That last point will seem a little strange, so here is a sample of code, and hopefully, ull c what i mean
Code:
cout << "Message >> ";
cin.getline(array, 200);
x = 0;

// MAIN ENCYPTION PART ////////////////////////////////////////////////////
while(x < 200)
{
y = rand()%3; // RANDOM NUMBER SELECTION
z = 64 + (rand()%25);
if(y == 1){
cout << (char)z; // TYPECASTING OF Z
a_file << (char)z;
}
if(y == 2){
cout << array[x];
a_file << array[x]; x++;
}
}

cout << "" << endl;
The plan is that the message gets mixed up with random letters and then gets saved to disk, but I need to be able to distinguish between the randomly generated letters and the message characters so that I can write another file that will tell you where the message characters are.
If you can understand that well done cos i didnt explain it too well

2. Well the easiest way to mix up random letters and non random ones is to use a fixed way of mixing them, for example one random, one message, one random, one message.

Of course this is not too good. But if you remember that a rand() will produce a same sequence of random numbers from the same seed you might be able to do something interesting with it. (it still won't be good encryption but might be enough to make it not completely obvious).

3. Well, if rand() does give the same random letters for the same speeds, how do u get completely random letters?

4. You can not make completely random letters, you can always calculate the next number (although it might be hard). Computers can only produce pseudo-random (spelling?) numbers.

5. As shakti said it is impossible for a program to generate completely random output.
The only way to truly generate random numbers on a computer is to use specialized hardware which is not common by any means. (one of the lecturers at my uni has a quantum random number generator that can be installed as a pci card )

6. Look up the srand() function. It seeds the inbuilt random number generator. With the same seed value, rand() will produce the same sequence every time, using srand() with the current date and time say, will generate a different pseudo-random sequence.

7. The decryption process needs to be able to undo the encryption in order for the entire process to be meaningful. If you don't like the idea of non-random placement of message char in the file as suggested by siegfriedmcwild, then you will need to encode a mechanism as to how to find the embedded message char if they are randomly placed in the file. One approach might be to keep track of where in the encrypted file you can find the desired char. The encrypted file could then contain the number of char in the message, the location of the embedded char of the message, and the encrypted message itself (where the message char are "randomly" placed). Of course, you would want to encrypt the administrative data in a non-random fashion so you can decrypt it before using it to decrypt the randomly embedded message, but it adds another couple levels of subterfuge the intrepid intruder needs to overcome.

8. so that I can write another file that will tell you where the message characters are
Do exactly what you just said. Keep a running counter that keeps track of how many characters you've written to the 'encrypted' file, and every time you write a 'real' character, you write the current position into a second file.
Code:
int count = 0;
while(x < 200)
{
y = rand()%2; // RANDOM NUMBER SELECTION
z = 'A' + (rand()%26);
if(y == 0){
cout << (char)z;
a_file << (char)z;
}
else{
cout << array[x];
a_file << array[x];
++x;
b_file << count << std::endl;
}

++count;
}
Note that I made a few other changes, which are bolded. These are just fixes for some small bugs that you had, see if you can figure out what was wrong

9. Oops. Missed another bug:
Code:
while(x < strlen(array))

10. the reason why I pointed out that rand() will return the same sequence gaivne the same seed was because that is a way to figure out if where the message characters are.

To encode:
Use the encryption password as the seed.
Each time you have to write a character call rand() and transform the result (let's call it r) in an int between 1 and someNumber.
Generate and write to the file r random characters (set a char to a value between 0 and 255).
Write one message character.
Repeat until the entire message is encoded.

To decode:
Use the encryption password as the seed.
Call rand() to generare a number between 1 and someNumber (let's call it r again).
Generate r random numbers and read r characters (if you don't the decoder will go out of sync from the encoder, plus if you check the values you generate with the values in the file it gives you some tamper warning).
Read and store one character, this is part of the message.
Repeat until you run out of file.

As a simple encoding scheme it might even be decently good, but I wouldn't trust it with anything important.