Sunday, October 30, 2022

[FIXED] Why unsigned char variable contains EOF?

Issue

I developed an encryption algorithm which takes character by character texts from a .txt file and encrypts it, and then writes it back to another .txt file. The problem is when I read the encrypted file, a character like arrow sign acts as EOF and my loop terminates before the original EOF. Here is my code:

static void ECB_ENCRYPTION(void)
{
    uint8_t i = 0, j = 0, c, buf1[16]


    uint8_t plain_text[16];

    // File pointers for file operations.
    FILE *f, *f1;


    // Encrypts the file [plaintext.txt].
    f = fopen("plaintext.txt", "r");
    f1 = fopen("ciphertext.txt", "w");
    while(1)
    {
        i = 0;
        while(i < 16)
        {
            c = getc(f);
            if(feof(f))
            {
                break;
            }
            else
            {
                plain_text[i] = c;
                ++i;
            }
        }

        if(i != 16)
        {
            while(i < 16)
            {
                plain_text[i] = ' ';
                ++i;
            }
        }

        // Encrypts plain text.
        AES128_ENCRYPT(plain_text, buf1);

        i = 0;
        while(i < 16)
        {
            putc(buf1[i], f1);
            ++i;
        }

        if(feof(f))
            break;

    }

    fclose(f);
    fclose(f1);
}

static void ECB_DECRYPTION(void)
{

    uint8_t i = 0, j = 0, c, buf1[16];

    uint8_t cipher_text[16];

    // File pointers for file operations.
    FILE *f, *f1;

    // Encrypts the file [plaintext.txt].
    f = fopen("ciphertext.txt", "r");
    f1 = fopen("decryptedtext.txt", "w");
    while(1)
    {
        i = 0;
        while(i < 16)
        {
            c = getc(f);
            if(feof(f))
            {
                break;
            }
            else
            {
                cipher_text[i] = c;
                ++i;
            }
        }

        if(feof(f))
            break;

        // Decrypts cipher text.
        AES128_DECRYPT(cipher_text, buf1);

        i = 0;
        while(i < 16)
        {
            putc(buf1[i], f1);
            ++i;
        }

    }

    fclose(f);
    fclose(f1);
}

Solution

The use of uint8_t c rather than int c obfuscated the true issue: opening the file in binary vs. text mode. @Klas Lindbäck

int c would have been better. Still OP's use of uint8_t; c = getc(f); if(feof(f)) was almost correct. It fails when an read error occurs.

static void ECB_DECRYPTION(void) {
    uint8_t cipher_text[16];
    uint8_t buf1[16];

    FILE *f = fopen("ciphertext.txt", "rb");  // add 'b'
    assert(f);
    FILE *f1 = fopen("decryptedtext.txt", "wb"); // add 'b'
    assert(f1);
    while(1)
    {
        int c;
        for (unsigned i=0; i<16; i++) {
            c = getc(f);
            if (c == EOF) break;
            cipher_text[i] = (uint8_t) c;
        }

        if(c == EOF) break;  // exit loop on end-of-file or input error

        AES128_DECRYPT(cipher_text, buf1);

        for (unsigned i=0; i<16; i++) {
            putc(buf1[i], f1);
        }
    }

    fclose(f);
    fclose(f1);
}


Answered By - chux - Reinstate Monica
Answer Checked By - Dawn Plyler (PHPFixing Volunteer)

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.