Thread: Simple Encryption Program

  1. #1
    Registered User
    Join Date
    Jul 2005
    Posts
    10

    Simple Encryption Program

    First off let me say that I appreciate any help that I recieve.

    Okay, here is my problem:

    I am writing a simple encryption program that will encrypt text files. The encryption key to decrypt the files is held at the very beginning and is seperated by a tab from the rest of the files.

    Example:

    2344321[tab]afldsfjkasldfkjasljkfsajfal;kf;lkkl;adfkl;a.

    My question is how would I go about reading the first few numbers from the tab and then continue to read the other characters. I am using rand to generate the key and I seeded it with the time.

    My main problem is taking in the numbers on as a whole integer instead of passing them into an array. How would I go about doing this and still be able to scan the rest of the file.

    I appreciate any assistance.

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    To me it sounds like you'd first want to use something like fscanf with a %d to obtain the key. Then you'd skip the tab and encrypt the rest of the characters obtained with something like fgetc.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Just use atoi or strtol or sscanf to read the number from your char array into an int.

  4. #4
    Registered User
    Join Date
    Jul 2005
    Posts
    10
    Okay.. .but how do I tell the compiler to scan everything up the the tab? I pretty much got it after that I think.

    Hm..... I would think this would work but it doesn't"
    Code:
    while ((x=fgetc(fpdata) ! = '\t'))
             {
                key[i] = x;
                i++
             }
    Then after it reaches the tab it goes ahead and scans the rest of the document with fget c

    Code:
    tab = fgetc(fpdata);
    while  ( ( x = fgetc( fpAtad ) ) != EOF )
                {
                    printf( "%c", x );
                }

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Like I said earlier.
    Code:
    #include <stdio.h>
    
    int main(void )
    {
       static const char filename[] = "file.txt";
       FILE *file = fopen(filename, "r");
       if ( file )
       {
          int key;
          char tab;
          if ( fscanf(file, "%d%c", &key, &tab) == 2 && tab == '\t' )
          {
             printf("key = %d\n", key);
             for ( ;; )
             {
                int c = fgetc(file);
                if ( c == EOF )
                {
                   break;
                }
                /* do stuff (here I'll just print the character) */
                putchar(c);
             }
          }
          fclose(file);
       }
       else
       {
          perror(filename);
       }
       return 0;
    }
    
    /* my output
    key = 2344321
    afldsfjkasldfkjasljkfsajfal;kf;lkkl;adfkl;a.
    */
    Although you may want to open the file in binary mode.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with simple program
    By nik2007 in forum C++ Programming
    Replies: 4
    Last Post: 02-27-2006, 09:54 AM
  2. need a simple program
    By Peter Griffin in forum C++ Programming
    Replies: 7
    Last Post: 12-04-2005, 04:23 PM
  3. Replies: 5
    Last Post: 11-27-2005, 09:50 PM
  4. Simple message encryption
    By Vicious in forum C++ Programming
    Replies: 10
    Last Post: 11-07-2004, 11:48 PM
  5. Simple simple program
    By Ryback in forum C++ Programming
    Replies: 10
    Last Post: 09-09-2004, 05:48 AM