Write some code you believe would solve this homework problem. Post the source here using code tags, along with any errors you might get from your compiler or problems you have with possible incorrect output.
We will help you when you put some effort, we won't do your work for you.
To run the program the user should type otp -e <unencrypted_file>
<output_file> <secret_file>, ...
You haven't bothered to implement this.
For each character p in the unencrypted file, ...
This doesn't mean allocate an array of p (that you don't delete).
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
I can't see where you implement this mapping.
This list goes on, but you have to respond first for anyone to help.
dark nepthys
"Your" code has been cut and paste from the sanfoundry site I quoted above.
If it is genuinely your work then please prove it. If it is not then please note this is not a homework site and not a vehicle for your plagiarism and cheating.
[BTW If it is your code I don't think too many people would believe you can't make a start.]
(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]
constchar chmap[] =
"abcdefghijklmnopqrstuvwxyz""ABCDEFGHIJKLMNOPQRSTUVWXYZ"" ";
char encrypt(char c, int r)
{
// find the position of the char in the char map
char *p = std::find(chmap, chmap + sizeof(chmap), p);
if (p == chmap + sizeof(chmap))
throw std::domain_error("Input character out of range");
// find the index of the char
ptrdiff_t idx = p - chmap;
// add on the random offset
idx += r;
idx = idx % sizeof(chmap);
return chmap + idx;
}
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.
This means you need to save the random numbers in a container somewhere so you can save them when you're done.
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.
This means you need to handle the command line args. We need to have exactly these args:
0: program name
1: -e (or -d to be added later)
2: plain text file name
3: cipher text file name
4: random numbers file used to generate cipher text
The op has removed the post. I can imagine what the command line would be for decrypt and of course the decrypt is just the opposite of the encrypt.
If you want help, ask for it. Don't try to bait someone who spending their own valuable time to help for free.
This is the original text:
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 encrypt characters and blank spaces following exactly this mapping:
‘a’ ‘b’ … ‘z’ ‘A’ ‘B’ … ‘Z’ [blank space]
1 2…26 27…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.