1
I need help with that, I’ve done the functions that encryption and decryption, it encrypts normal, however, at the time of decryption it sure does not do the process properly.
The error and the following: to encrypt I think it encrypts correctly I even made a pritf there to see the result and how it is controlling the arrays, but when decrypting it eats a letter of the password, and says that there is a character more in the file, Besides the password entry go from 1 to 8 and I think it should go from 0 to 7 as in encryption.
follows a "Log" of what happens:
ESSE E O LOG DELE ENCRIPTANDO
O---------------------------------------O
| WELCOME TO ENCRYPTION SYSTEM |
|---------------------------------------|
| 1 - Encrypt file |
| 2 - Decrypt file |
| 0 - Quit |
O---------------------------------------O
o--> 1
Please, enter password: Senha123
Enter the name of the source file: test.txt
The source file will be encrypted in the target file.
Enter the target file name: enc
File chars: 15
Password chars: 8
char: �[15] - encrypted: � - password: S[0]
char: [14] - encrypted: � - password: e[1]
char: z[13] - encrypted: - password: n[2]
char: t[12] - encrypted: # - password: h[3]
char: n[11] - encrypted: 0 - password: a[4]
char: e[10] - encrypted: i - password: 1[5]
char: W[9] - encrypted: v - password: 2[6]
char: [8] - encrypted: � - password: 3[7]
char: s[7] - encrypted: 9 - password: S[0]
char: o[6] - encrypted: + - password: e[1]
char: i[5] - encrypted: ( - password: n[2]
char: c[4] - encrypted: 4 - password: h[3]
char: i[3] - encrypted: 5 - password: a[4]
char: n[2] - encrypted: ` - password: 1[5]
char: i[1] - encrypted: d - password: 2[6]
char: V[0] - encrypted: v - password: 3[7]
ESSE E O LOG DELE DESECRIPTANDO
O---------------------------------------O
| WELCOME TO ENCRYPTION SYSTEM |
|---------------------------------------|
| 1 - Encrypt file |
| 2 - Decrypt file |
| 0 - Quit |
O---------------------------------------O
o--> 2
Please, enter password: Senha123
Enter the name of the source file: enc
The encrypted file will be dencrypted in the target file.
Enter the target file name: dec
File chars: 16
Password chars: 8
char: �[16] - decrypted: - password: [8]
char: �[15] - decrypted: � - password: 3[7]
char: �[14] - decrypted: � - password: 2[6]
char: �[13] - decrypted: � - password: 1[5]
char: �[12] - decrypted: � - password: a[4]
char: �[11] - decrypted: � - password: h[3]
char: �[10] - decrypted: � - password: n[2]
char: �[9] - decrypted: � - password: e[1]
char: �[8] - decrypted: - password: [8]
char: �[7] - decrypted: � - password: 3[7]
char: �[6] - decrypted: � - password: 2[6]
char: �[5] - decrypted: � - password: 1[5]
char: �[4] - decrypted: � - password: a[4]
char: �[3] - decrypted: � - password: h[3]
char: �[2] - decrypted: � - password: n[2]
char: �[1] - decrypted: � - password: e[1]
char: �[0] - decrypted: - password: [8]
follows the explanation:
Algorithm 1 - Text encryption algorithm A program must be developed to encrypt and decrypt text files. The program must ask the user for the name of the file with the text to be encrypted, the name of the target file of the encrypted text, and a keyword for the encryption. With this information the program must create the second file containing the encrypted text. This same program must decrypt the text file. The program must ask the user the name of the source files (encrypted text) and destination (plain text) and the keyword that was used in the encryption, the program must generate the target file containing the plain text (decrypted).
Encryption protocol: Given any keyword (and any size), the program must read from the text block text file (vector) the size of that keyword, the vector must be reversed and calculated a displacement on top of each element of the text block, the amount to be shifted should be the ASCII number of the corresponding element in the keyword, only printable characters of the ASCII tables should be used, ranging from -127 +127 (or 0-255).
Example: Example text: This is an example text! Keyword: "Password 123"
keyword size: 8, then data blocks will be read from 8 on 8. ex: "This and u", the block should be inverted, ex: "u e etse", and calculated the keyword displacement in the case: 83, 101, 110, 104, 97, 49, 50, 51.
From this information one should apply the scroll on top of each element of the text block, for example, the text block (already inverted) "u e etse" has the following ASCII codes: 117, 32, 101, 32, 101 ,116, 115, 69. Now just add the displacement with the codes of the text block, example: 83+117, 101+32, 110+101, 104+32, 97+101, 49+116, 50+115, 51+69. This is the same as: -55, -122, -44, -119, -57, -90, -90, 120. Soon the encrypted text should look like this: "?ԉA6x]". Note that I said similar, and not identical, as the characters shown are not standard, their graphical display should vary from operating system to operating system.
This operation must be undone through the decryption function in the same program, which will basically do the reverse process.
Follows the codes of the functions that make the encryption and decryption:
ENCRYPTION
#include "encryptEngine.h"
void encrypt(FILE* sourceFile, FILE* encryptedFile, char* password) {
char* sourceFileName = malloc(sizeof(char) * MAX_FILE_LENGTH);
char* encryptedFileName;
setPassword(password);
setFileName(sourceFileName);
if((sourceFile = fopen(sourceFileName, "r")) != NULL) {
encryptedFileName = malloc(sizeof(char) * MAX_FILE_LENGTH);
setEncryptedFileName(encryptedFileName);
} else {
fileErrorHandler("notExist");
}
int index, indexPass;
char auxiliary, encryptedChar;
rewind(sourceFile);
fseek(sourceFile, 0L, SEEK_END);
printf("File chars: %d\n", ftell(sourceFile));
printf("Password chars: %d\n", sizeof(password));
if((encryptedFile = fopen(encryptedFileName, "w")) != NULL) {
indexPass = 0;
index = ftell(sourceFile);
while(index >= 0L){
fseek(sourceFile, index, SEEK_SET);
auxiliary = fgetc(sourceFile);
encryptedChar = 255 - (password[indexPass] + auxiliary);
printf("char: %c[%d] - encrypted: %c - password: %c[%d]\n", auxiliary, index, encryptedChar, password[indexPass], indexPass);
fprintf(encryptedFile, "%c", encryptedChar);
indexPass++;
if(indexPass == sizeof(password)) {
indexPass = 0;
}
index--;
}
printf("\n");
} else {
fileErrorHandler("fileName");
}
fclose(sourceFile);
fclose(encryptedFile);
free(sourceFileName);
free(encryptedFileName);
}
DECRYPTION
#include "decryptEngine_new.h"
void decrypt(FILE* encryptedFile, FILE* decryptedFile, char* password) {
char* encryptedFileName = malloc(sizeof(char*) * MAX_FILE_LENGTH);
char* decryptedFileName;
setPassword(password);
setFileName(encryptedFileName);
if((encryptedFile = fopen(encryptedFileName, "r")) != NULL) {
decryptedFileName = malloc(sizeof(char*) * MAX_FILE_LENGTH);
setDecryptedFileName(decryptedFileName);
} else {
fileErrorHandler("notExist");
}
int index, indexPass;
char auxiliary, decryptedChar;
rewind(encryptedFile);
fseek(encryptedFile, 0L, SEEK_END);
printf("File chars: %d\n", ftell(encryptedFile));
printf("Password chars: %d\n", sizeof(password));
if((decryptedFile = fopen(decryptedFileName, "w")) != NULL) {
indexPass = sizeof(password);
index = ftell(encryptedFile);
while(index >= 0L) {
fseek(encryptedFile, index, SEEK_END);
auxiliary = fgetc(encryptedFile);
decryptedChar = (255 - auxiliary) - password[indexPass];
printf("char: %c[%d] - decrypted: %c - password: %c[%d]\n", auxiliary, index, decryptedChar, password[indexPass], indexPass);
fprintf(decryptedFile, "%c", decryptedChar);
indexPass--;
if(indexPass == 0) {
indexPass = sizeof(password);
}
index--;
}
printf("\n");
} else {
fileErrorHandler("fileName");
}
fclose(encryptedFile);
fclose(decryptedFile);
free(encryptedFileName);
free(decryptedFileName);
}
I would be very happy with a help, or even a tip, I do not want the answer to the problem because I like to do it by myself, however, I am 3 days with the problem.
Next time try to create a reduced version of your problem instead of posting the entire file. With a smaller program it is much easier to debug. Not to mention that taking out all the file read details, etc, it is much easier for other people to reproduce your bug and that also I do not know if your teacher will find it cool that you are posting the homework code on the internet from where your colleagues can copy :)
– hugomg
Relax buddy, go no, that’s just a part of the code, and wrong by the way :)
– lambda