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

  1. #1
    Registered User Finchie_88's Avatar
    Join Date
    Aug 2004
    Posts
    154

    Question 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. #2
    Registered User
    Join Date
    Dec 2004
    Location
    UK
    Posts
    109
    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. #3
    Registered User Finchie_88's Avatar
    Join Date
    Aug 2004
    Posts
    154
    Well, if rand() does give the same random letters for the same speeds, how do u get completely random letters?


  4. #4
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    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. #5
    Registered User
    Join Date
    Dec 2004
    Location
    UK
    Posts
    109
    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. #6
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    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.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  7. #7
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    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. #8
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    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
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  9. #9
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Oops. Missed another bug:
    Code:
    while(x < strlen(array))
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  10. #10
    Registered User
    Join Date
    Dec 2004
    Location
    UK
    Posts
    109
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. questions....so many questions about random numbers....
    By face_master in forum C++ Programming
    Replies: 2
    Last Post: 07-30-2009, 08:47 AM
  2. A couple questions
    By Flakster in forum C++ Programming
    Replies: 7
    Last Post: 08-09-2005, 01:22 PM
  3. Few simple questions...
    By Shane in forum C++ Programming
    Replies: 9
    Last Post: 08-06-2005, 02:40 AM
  4. A few simple batch questions
    By sean in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 07-02-2003, 01:35 PM
  5. questions questions questions.....
    By mfc2themax in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 08-14-2001, 07:22 AM