为什么 unsigned char 变量包含 EOF?
Why unsigned char variable contains EOF?
我开发了一种加密算法,它从 .txt 文件中逐个字符地提取文本并对其进行加密,然后将其写回另一个 .txt 文件。问题是当我读取加密文件时,像箭头符号这样的字符充当 EOF,并且我的循环在原始 EOF 之前终止。这是我的代码:
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);
}
那是因为加密的 ascii 字符可以得到任何二进制表示,包括 nul、EOF、^Z 等等。因此,您生成的加密文件不再是文本文件。它现在是一个二进制文件。
所以创建加密文件时,用"wb"
打开,读取时用"rb"
打开。另外,使用二进制函数写入(fwrite
)和读取(fread
),而不是fputc
和fegtc
。
使用 uint8_t c
而不是 int c
混淆了真正的问题:以二进制模式与文本模式打开文件。
int c
会更好。 OP 对 uint8_t; c = getc(f); if(feof(f))
的使用仍然 几乎 正确。发生读取错误时失败。
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);
}
我开发了一种加密算法,它从 .txt 文件中逐个字符地提取文本并对其进行加密,然后将其写回另一个 .txt 文件。问题是当我读取加密文件时,像箭头符号这样的字符充当 EOF,并且我的循环在原始 EOF 之前终止。这是我的代码:
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);
}
那是因为加密的 ascii 字符可以得到任何二进制表示,包括 nul、EOF、^Z 等等。因此,您生成的加密文件不再是文本文件。它现在是一个二进制文件。
所以创建加密文件时,用"wb"
打开,读取时用"rb"
打开。另外,使用二进制函数写入(fwrite
)和读取(fread
),而不是fputc
和fegtc
。
使用 uint8_t c
而不是 int c
混淆了真正的问题:以二进制模式与文本模式打开文件。
int c
会更好。 OP 对 uint8_t; c = getc(f); if(feof(f))
的使用仍然 几乎 正确。发生读取错误时失败。
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);
}