Encryption in ASCII in C

1

I need help with this, I already did the functions that encryption and decryption, it encrypts normal, but at the time of decryption it certainly does not do the process correctly.

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 at the time of decrypting it eats a letter of the password, and says has one more character in the file, besides the password index go from 1 to 8 being that 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 should prompt the user for the file name with the text to be encrypted, the name of the destination file of the encrypted text, and a password for the encryption. With this information the program should create the second file containing the encrypted text. This same program should do the decryption of the text file. The program should prompt the user for the name of the source files (encrypted text) and destination (plain text) and the keyword that was used in the encryption, the program should generate the destination file containing the pure (decrypted) text. / p>

Encryption protocol: Given any keyword (and any size), the program should read from the text file text blocks (vector) of the size of that keyword, the vector must be reversed and calculated a shift over each element of the text block, the amount to be moved must be the ASCII number of the corresponding element in the keyword, only the printable characters of the ASCII tables, ranging from -127 +127 (or 0-255).

Example: Sample text: This is an example text any! Keyword: "Password123"

Keyword size: 8, soon to be read data blocks of 8 by 8. eg: "This I", the block should be inverted, eg "ue etsE", and calculated the keyword offset , in the case: 83, 101, 110, 104, 97, 49, 50, 51.

From this information, you should apply the offset to each element of the text block, for example, the (already inverted) text block "ue etsE" has the following ASCII codes: 117, 32, 101, 32, 101, 116, 115, 69. Now simply add the offset with the codes of the text block, for example: 83 + 117, 101 + 32, 110 + 101, 104 + 32, 97 + 101, 49 + 116, 50 +115, 51 + 69. This is equal to: -55, -122, -44, -119, -57, -90, -90, 120. Then the encrypted text should look like this: "ɆԉǦ \ A6x]". Note that I said similar, not identical, because as the characters presented are not defaults, their graphical display should vary from operating system to operating system.

This operation must be undone using the decrypt function in the same program, which will basically do the reverse process.

Here are the codes for the functions that encrypt and decrypt:

ENCRIPTA

    #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);
    }

DESIGN

    #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 myself, but I'm 3 days away from the problem.

    
asked by anonymous 25.03.2015 / 15:02

2 answers

3
  

First of all, see the hugomg remark for how to calculate the password size. It should be strlen(password) and not sizeof(password) .

Your problem lies in how you are handling the boundaries of the ranges (in the case of the password for sure, and most likely in the case of the file as well). When you encrypt:

indexPass++;
if(indexPass == sizeof(password)) {
    indexPass = 0;
}

You ensure that indexPass is never equal sizeof(password) [when it is used], at most 1 less than it. And as it starts (and resumes) from zero, then the range being used is actually:

[0, sizeof(password)[

Already when you decrypt:

indexPass--;
if(indexPass == 0) {
    indexPass = sizeof(password);
}

You guarantee that it will never be equal to zero, at most 1 more, and you start and restart from sizeof(password) :

]0, sizeof(password)]

The consequence of this is that the first character of the password is never used, and the null terminator ( fseek(encryptedFile, index, SEEK_END) ) at the end of the password is sometimes used (which in the case of a Vigenère cipher means that the character will unchanged).

In any case, this is irrelevant, because by the way you are implementing the algorithm it is not necessary to go through the password "backwards" - if the first character of the message was combined with the first character of the key, the first character character of the cipher must be combined also with the first character of the key ... So in your deciphering function you should use the same loop to traverse the password you used in the encryption process. / p>

(and if you were to go all the way back, there would still be the problem of padding - unless the file size was multiple of the password size, / em> would be used near the end of the file. Complicating your implementation even more ...)

As for the file problem, I do not have enough experience with C to say for sure, but most likely is of the same nature: SEEK_END puts the position - in my understanding - not in its last byte, but 1 beyond it (again, by the convention of the intervals closed at the beginning and opened at the end). Also, a quick look at their codes showed inconsistent use of SEEK_SET and %code% (I do not know if it's purposeful, as I said I have little practical experience with C).

Encrypt:

  while(index >= 0L){
      fseek(sourceFile, index, SEEK_SET);

Decrypts:

  while(index >= 0L) {
      fseek(encryptedFile, index, SEEK_END);

I suggest checking this out.

    
25.03.2015 / 18:07
2

An error you have in your code is that sizeof(password) does not contain the number of letters of the password. It will be the size of the pointer, which will probably always be 4.

In C you should always pass the size of your vectors as a separate parameter.

    
25.03.2015 / 18:14