Using the Rand Function to Decrypt

This is a discussion on Using the Rand Function to Decrypt within the C Programming forums, part of the General Programming Boards category; Hey everyone, I'm having some trouble finishing a code that is a Caesar Cipher with some modifications. "Write a program ...

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    2

    Using the Rand Function to Decrypt

    Hey everyone, I'm having some trouble finishing a code that is a Caesar Cipher with some modifications.

    "Write a program to decrypt a message located in a file named “encrypted.txt” then print the decrypted message to the screen and a new file named “decrypted.txt” .
    “encrypted.txt” has been created for you using a Caesar Cipher. Two things are different though.First, to ease some of the requirements for this assignment, we will not worry about ensuring that all encrypted values will be a letter (a-z and A-Z).Lastly, the encryption process has been completed using a random shift value.You must ask the user to supply a seed, which is being used as a passcode, to decrypt the message correctly.In addition, the encryption process has attempted to prevent characters from being encrypted as non-printable characters.So, the random shift has been restricted to values between 0 and 4, inclusive."

    What I have thus far is:

    Code:
    #include <stdio.h>
    
    int main()
    {
        char c;
        int p;
        FILE *fp,*fw;
        
        printf("Enter the passcode:\n ");
        scanf("%d",&p);
        
        if((fp = fopen("encrypted.txt", "r")) == NULL)
        {
               printf("Error: Can't Open encrypted.txt\n");
               return 1;
        }
        
        if((fw = fopen("decrypted","a")) == NULL)
        {
               printf("Error: Can't Open decrypted.txt\n");
               return 1;
        }
        
        if( p >= 0 && p <= 4)
        {
            do{
                 c = fgetc (fp);
                 c += rand() %p;
                 printf("%c", c);
                 fprintf(fw, "%c", c); 
            } while ( c != EOF );
        }
        else
        {
            printf("Invalid Passcode\n");
        }
        
        fclose(fp);
        fclose(fw);
        
        return 0;  
    }
    I thought this would of worked, but it seems to have problems on the upload site. I was wondering If anyone had any ideas on what I was doing wrong. Thanks for any and all help.

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Code:
            do{
                 c = fgetc (fp);
                 /// Must check for EOF here!
                 c += rand() %p;
                 printf("%c", c);
                 fprintf(fw, "%c", c); 
            } while ( c != EOF );
    But how is adding a random number to the characters going to decrypt them?
    Last edited by oogabooga; 02-18-2012 at 05:07 PM.

  3. #3
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,625
    Caesar Cipher - Wikipedia Take a look at that link.
    Devoted my life to programming...

  4. #4
    Registered User
    Join Date
    Feb 2012
    Posts
    2

    Figured it out

    I finally got it. It was actually pretty simple; I forgot to use the srand function to declare the seed. And also checking for the EOF before also helped, so thanks!

    But how is adding a random number to the characters going to decrypt them?


    A file I am reading from is encrypted using a "random" shift value. By declaring a seed, I am able to decrypt the message from any computer, even if it is supposedly random. That is, I am able to convert the letters in the message back using their ASCII values.

    Thanks everyone for the help!

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,308
    The catch is that the pseudorandom number generator algorithm is not standardised: only the functions srand and rand are standard; their implementation isn't, hence the "any computer" thing might not work the way you have in mind. Besides this, the algorithm used for the implementation is not likely to be cryptographically secure, though for a test program this does not matter.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 11-14-2011, 08:08 AM
  2. rand() function
    By thunderzone in forum C Programming
    Replies: 10
    Last Post: 05-02-2010, 08:09 AM
  3. rand() function
    By jduke44 in forum C Programming
    Replies: 9
    Last Post: 10-07-2005, 06:33 PM
  4. rand() function?
    By s_ny33 in forum C++ Programming
    Replies: 2
    Last Post: 03-08-2005, 07:58 PM
  5. the rand() function
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 05-25-2002, 12:30 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21