Write a text‐based program that can encrypt or decrypt a text file
using the “unbreakable” One‐Time Pad cryptosystem. The program should read
a text file that contains only English characters (upper and lower case) and blank
spaces, and generate an encrypted or decrypted file following these rules:
(1) For encryption: For each character p in the unencrypted file, generate a
random number r between 1 and 53. The encrypted character C will be
computed by the formula C = p + r. To do this you will need to enccrypt
characters and blank spaces following exactly this mapping:
‘a’ ‘b’ … ‘z’ ‘A’ ‘B’ … ‘Z’ [blank space]
1 2 … 26 27 28 … 52 53
Example: if your random number r is equal to 7, the unencrypted
character ‘z’ will be encrypted to character C = ‘z’ + 7 = 33 = ‘H’. Note
that this formula should wrap around; for example, for r = 6 and p = ‘Y’, C
= ‘Y’ + 6 = 51 + 6 = 4 = ‘d’.
The generated sequence of random numbers r should be stored in a
separate file which is the secret key to decrypt the encrypted file.
Also, of course, you need to generate an encrypted file containing the
encrypted characters.
To run the program the user should type otp -e <unencrypted_file>
<output_file> <secret_file>, where unencrypted_file is the name of the file
you want to encrypt, output_file is the name of the file you want to give to
the encrypted file, and secret_file is the name of the file that will contain the
generated random numbers which are the key to decrypt output_file at a
later time.
(2) For decryption: For each character C in the encrypted file, read the
corresponding number r from the secret key file generated during
encryption, and compute the unencrypted character p using the formula
p = C – r.
Example: if your secret key number for a particular character C is r = 7,
the unencrypted character ‘H’ will be computed using the formula p = 33
– 7 = 26 = ‘z’. Note again that this formula should wrap around; for
example, for r = 6 and C = ‘d’, p = ‘d’ ‐ 6 = 4 ‐ 6 = 51 = ‘Y’.
To run the program the user should type otp -d <encrypted_file>
<output_file> <secret_file>, where encrypted_file is the name of the file
you want to decrypt, output_file is the name of the file you want to give to
the unencrypted file, and secret_file is the name of the file that contains the
generated random numbers which are the key to decrypt the output_file.
Please use the code tag to make the program more readable, it looks like a mess now. And try to ask about what doesn't work as intended or doesn't work at all (like compile and run-time errors) rather than just throwing it around.
For start, you haven't put the main function arguments(i.e: argc and argv), you need them in order to task the program via command prompt by passing your arguments(the files)
I recomend you use pastebin if you're going to be posting large quantities of code (or an entire program). It will make it easier for people to read, and make your post more concise.
I recomend you use pastebin if you're going to be posting large quantities of code (or an entire program). It will make it easier for people to read, and make your post more concise.
I wish you would stop going around recommending people use pastebin for code that easily fits into one post. Using code tags will make it more readable. Moving code to another location will mean reviewers have to do more work to see the code and respond to it, so it will be less likely to garner a response.