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