-1

I'm trying to make a Vigenere cipher code in C and I have done something that is wrong and I can't fix it... How do understand that something goes wrong? Well I have some examples with keyword and result cipher with Vigenere cipher like

  • keyword: bacon
  • text: Meet me at the park at eleven am
  • correct result: Negh zf av huf pcfx bt gzrwep oz
  • my code result with same text and keyword: Tegh ne og tjs qaty bt syfvgb bm

Code:

int main(int argc, string argv[])
{
    string keyWord;

    if( argc != 2  )
    {
        printf("Wrong Argument");
        return 1;
    }
    else
    {
        keyWord = argv[1]; 
        //check if argument is 
        //only alphabetical characters
        for(int i = 0; i < strlen(keyWord); i++)     
        {
            char c = keyWord[i];
            if( !isalpha(c) )
            {
                printf("Your Keyword Must Contain Only alphabetical characters\n");
                return 1;
            }
        }
    }
    //todo
    printf("Enter Plain Text\n");
    string plainText = GetString(); 
    for(int i = 0; i < strlen(plainText); i++) 
    {
        char c = plainText[i];
        int keyWordWrapper;
        char keyC;
        if(isalpha(c))
        {
            keyWordWrapper = i % strlen(keyWord);
            keyC = keyWord[keyWordWrapper];

            if(islower(c))
            {
                int key = keyC - 'a';
                c = (c - 'a'  + key) % 26 + 'a'; 
            }
            if(isupper(c))
            {
                int key = keyC - 'A';
                c = (c - 'A'  + key) % 26 + 'A'; 
            }
        }
        printf("%c",c);
    }
    printf("\n");
    return 0;
}

GetString() is declared in a header and defined in a library that I'm using (it's like scanf).

this is the updated code

int main(int argc, string argv[])

{ string keyWord;

 if( argc != 2  )
{
    printf("Wrong Argument");
    return 1;

}
else
{
    keyWord = argv[1]; 

    //check if argument is 
    //only alphabetical characters
    for(int i = 0; i < strlen(keyWord); i++)     
    {
        char c = keyWord[i];
        if( !isalpha(c) )
        {
            printf("Your Keyword Must Contain Only alphabetical characters\n");
            return 1;
        }  


    }
}

string plainText = GetString(); 

int j;
for(int i = 0; i < strlen(plainText); i++) 
{
    j++;
    char c = plainText[i];
    int keyWordWrapper;
    char keyC;

    if(j > strlen(keyWord))
        j = 0;


    if(isalpha(c))
        {
            keyWordWrapper = i % strlen(keyWord);
            keyC = keyWord[keyWordWrapper];
            int key;
            tolower(c);

            if(islower(keyC))
             key = keyC - 'a';

            if(isupper(keyC))
             key = keyC - 'A';


            c = (c - 'a'  + key) % 26 + 'a'; 




        } 

    printf("%c",c);

}

printf("\n");

return 0; }

4

2 回答 2

2

代码中有两个问题。

首先是关键字中大写字母的处理。请注意,在一种情况下,代码a从 keyC 中减去,在另一种情况下A被减去。但这是基于纯文本字符的情况。该减法需要基于关键字中字母的大小写

其次,对于纯文本中的每个字符,代码都会前进到关键字中的下一个字符。如果纯文本字符是字符,则“正确结果”不会前进到关键字space的下一个字符。

这是我正在谈论的第二个问题的示例

text  Meet me at
keyC  baco nb ac
i     0123456789    i must always increment to the next char in plain text
k     0123 40 12    index into the keyword does not increment on non-alpha

因此k不能直接i用线计算

keyWordWrapper = i % strlen(keyWord);

相反,仅当纯文本包含字母字符时才k需要初始化0然后递增。以下行将计算正确的关键字索引。

keyWordWrapper = k % strlen(keyWord);

唯一的区别是它i被替换k并且k仅在纯文本具有字母字符时增加。

于 2014-04-04T01:11:26.110 回答
0

您应该将键转换为全部小写(或全部大写),然后在两个移位块中使用相同的表达式:

int key = keyC - 'a';  // Or 'A' if you convert to upper

您应该从循环strlen(plainText)条件中删除;for它将线性算法转换为二次算法。

于 2014-04-04T00:48:36.630 回答