A shift cipher is one of the simplest encryption techniques in the field of cryp
ID: 3824096 • Letter: A
Question
A shift cipher is one of the simplest encryption techniques in the field of cryptography. It is a cipher in which each letter in a plain text message is replaced by a letter some fixed number of positions up the alphabet (i.e. by right shifting the alphabetic characters in the plain text message). For example, with a right shift of 2, 'A' is replaced by 'C', 'B' is replaced by 'D', etc. Note that the shift wraps around from the end to the beginning of the alphabet such that, with a right shift of 2, 'Y' is replaced by 'A' and 'Z' is replaced by 'B'. Deciphering works in the same way, but shifts to the left and wraps around from the beginning of the alphabet to the end. Write two functions encrypt and decipher. encrypt takes a NULL terminated string and a shift parameter as inputs, and right shifts all alphabetic characters (i.e. 'a' - 'z' and 'A' - 'Z') by the shift parameter. Non- alphabetic characters should not be changed by encrypt. decipher takes a NULL terminated string and a shift parameter as inputs, and left shifts all alphabetic characters (i.e., 'a' - 'z' and 'A' - 'Z') by the shift parameter. Non- alphabetic characters should not be changed by encrypt. Your functions can be written assuming that the shift parameter will never be less than 0 or greater than 26. You must use the following function prototypes: void encrypt (char str[], int shift); void decipher (char str[], int shift);Explanation / Answer
Here is the code for encrpyting and decrypting:
#include <stdio.h>
#include <ctype.h>
int mod(int x, int y)
{
while(x < 0)
x += y;
return x%y;
}
void decrypt_fun(int shift, char*pt_letter)
//This function receives the shift value and the address of the encrypted letter and
//changes the encrypted letter to its decrypted value.
{
if(isalpha(*pt_letter) && isupper(*pt_letter))
*pt_letter = mod((*pt_letter - 65 - shift), 26) + 65;
else if(isalpha(*pt_letter) && islower(*pt_letter))
*pt_letter = mod((*pt_letter - 97 - shift), 26) + 97;
}
void encrypt_fun(int shift, char *pt_letter)
//This function receives the shift value and the address of the plaintext letter and
//changes the plaintext letter to its encrypted value.
{
if(isalpha(*pt_letter) && isupper(*pt_letter))
*pt_letter = mod((*pt_letter - 65 + shift), 26) + 65;
else if(isalpha(*pt_letter) && islower(*pt_letter))
*pt_letter = mod((*pt_letter - 97 + shift), 26) + 97;
}
int main()
{
FILE *fpIn, *fpOut;
fpIn = fopen("encrypted_message.txt", "r");
fpOut = fopen("decrypted_message.txt", "w");
int actualKey = 3, enteredKey;
char ch;
//Ask the user to enter the decryption key.
printf("Enter the decryption key: ");
scanf("%d", &enteredKey);
while(enteredKey != actualKey)
{
//Repeat the request until the right key is entered.
printf("Wrong key ");
printf("Enter the decryption key: ");
scanf("%d", &enteredKey);
}
while(!feof(fpIn))
{
fscanf(fpIn, "%c[^ ]c", &ch);
decrypt_fun(enteredKey, &ch);
fprintf(fpOut, "%c", ch);
}
//Display a message when file decryption is over.
printf("File decrypted! ");
fclose(fpIn);
fclose(fpOut);
char reEncrypt;
printf("Do you want to re-encrypt the file (y/n)? ");
scanf(" %c", &reEncrypt);
if(reEncrypt == 'y' || reEncrypt == 'Y')
{
//Ask the user to enter a key to re-encrypt the file.
printf("Enter the encryption key: ");
scanf("%d", &enteredKey);
actualKey = enteredKey;
//Re-encrypt file "decypted_message.txt" and store it at "encrypted_message.txt".
fpIn = fopen("decrypted_message.txt", "r");
fpOut = fopen("encrypted_message.txt", "w");
while(!feof(fpIn))
{
fscanf(fpIn, "%c[^ ]c", &ch);
encrypt_fun(enteredKey, &ch);
fprintf(fpOut, "%c", ch);
}
//Display a message when file encryption is over.
printf("File encrypted! ");
fclose(fpIn);
fclose(fpOut);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.