Шифрование Vigenere

Я написал некоторый код, и в нем используется шифрование Vigenere. Это простая программа для шифрования / дешифрования любых файлов.

#include<stdio.h>
/*
LANGUAGE: C.
STANDARD: C89.

ABOUT PROGRAM:
This is a simple program for encrypting/decrypting any files.
The size of source file coincide with size of result file.
For encryption of file are use any string key. For decrypting, 
you must to use the same key, which was used for encryption.

NOTES:
The Vigenere encryption are used in it. 
Info at the site: http://en.wikipedia.org/wiki/Vigen%C3%A8re_cipher.
This simple algorithm is often used at commercial products. The 
Vigenere's algorithm are using a string key, and 'XOR' for 
encrypting/decrypting information.

WARNING!
The use of this method, doesn't give 100% of a warranty 
for protection of your information. Don't create the keys, 
consisting of identical characters, for example" "aaaaa", 
"zzz", "xxxx" e.t.c. - it is very feeble protection!
Don't forget your encrypting keys... :)

SYNTAX OF USING:

vigenere StringKey SourceFileName ResultFileName

where:
vigenere - program name;
StringKey - string key for encrypting/decrypting;
SourceFileName - source file name;
ResultFileName - result file name;

EXAMPLE OF USING:
vigenere "G5$hj4*df7f3+x" "c:\temp\source.txt" "c:\temp\result.txt"

*/
int main(int argc, char *args[]){
    /****************************************************/
    /* All variables must be defined on top in function, otherwise 
    some compilers can't compile this code (for example - MS 
    Visual Studio 2012. */
    char ch; /* The next char for encrypting/decrypting. */
    char *x; /* String key. */
    FILE *srcFile; /* Source file. */
    FILE *trgFile; /* Result file. */   
    /****************************************************/

    /* The first argument always is a program file name. */
    if (4 != argc)
        return 1; /* Invalid arguments count. */

    if (!*args[1] || !*args[2] || !*args[3])
        return 2; /* Contains the empty argument. */

    x = args[1];
    if ((srcFile = fopen(args[2], "rb")) != NULL){
        if ((trgFile = fopen(args[3], "wb")) != NULL){          
            while((ch = getc(srcFile)) != EOF){
                if(!*x++)
                    x = args[1];
                putc((ch ^= *x), trgFile);
            }           
            fclose(trgFile);
        }
        else
            return 4;  /* Result file wasn't created. */
        fclose(srcFile);
    }
    else
        return 3; /* Source file wasn't opened. */
    return 0; /* Successful operation. */
}

Но этот код не всегда работает хорошо. Я не понимаю, почему это происходит. Я делаю XOR для каждого байта. Я проверил этот код на таких файлах TXT. Где моя ошибка?

2 ответа

Решение
 char ch;

 /* ...  */

 while((ch = getc(srcFile)) != EOF)

ch должен быть int, EOF определяется как отрицательный int,

В дополнение к ответу ouah, приращение значения указателя выглядит не так.

ваш if заявление, if(!*x++), это плохо по двум причинам:

  1. Делая шаг перед фактической операцией XOR, вы пропускаете первый символ вашего ключа в начальном цикле.
  2. Нет смысла увеличивать указатель, если вы уже достигли завершающего нуля символа.

Лучший код будет:

while((ch = getc(srcFile)) != EOF){
    putc((ch ^= *x), trgFile);
    if(!*++x)
        x = args[1];
}           
Другие вопросы по тегам