This is the code to decode a message that I have previously encrypted using the
ID: 3548506 • Letter: T
Question
This is the code to decode a message that I have previously encrypted using the same 'key' in both programs! I would like to stick to the general form I have already started using if possible
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(){
FILE *fileEncrypt = fopen("C:\Users\User\Desktop\Project_2\encryptMessage.txt", "r");//This file will contain the encrypted message.
FILE *fileKey = fopen("C:\Users\User\Desktop\Project_2\key.txt", "r");//This file will contain the key.
char Code, DecodedCode;
int key;
do{
Code = fgetc(fileEncrypt);
if(Code != 10)
printf( "%c", Code - key);
else{fprintf(fileEncrypt, " ", 10);}
//Code Scrambler(takes the number inputed and shifts the ASCII values of the message accordingly.
}
while( Code != EOF);//This loop will continue to print from the text file until it reaches the end.
while( Code != EOF);//This loop will continue to print from the text file until it reaches the end.
fclose(fileKey);//this will close the file containing the key.
fclose(fileEncrypt);//this will close the file containing the encrypted message.
return 0;
}
Explanation / Answer
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(){
FILE *fileEncrypt = fopen("C:\Users\User\Desktop\Project_2\encryptMessage.txt", "r");//This file will contain the encrypted message.
FILE *fileKey = fopen("C:\Users\User\Desktop\Project_2\key.txt", "r");//This file will contain the key.
char Code, DecodedCode;
int key;
fscanf(fileKey,"%d",&key);
Code = fgetc(fileEncrypt);
do{
if(Code>=65 && Code<=90)
{
DecodedCode = Code-key;
if(DecodedCode<65)
DecodedCode = DecodedCode+26;
printf( "%c", DecodedCode);
}
else if(Code>=97 && Code<=122)
{
DecodedCode = Code-key;
if(DecodedCode<97)
DecodedCode = DecodedCode+26;
printf( "%c", DecodedCode);
}
else
printf("%c",Code);
//Code Scrambler(takes the number inputed and shifts the ASCII values of the message accordingly.
Code = fgetc(fileEncrypt);
}
while( Code != EOF);//This loop will continue to print from the text file until it reaches the end.
fclose(fileKey);//this will close the file containing the key.
fclose(fileEncrypt);//this will close the file containing the encrypted message.
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.