# 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. ## 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. 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?

3. Caesar Cipher - Wikipedia Take a look at that link.

4. ## 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. 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.