增加和包装密钥

3
 string text = GetString();

//enters length of argv string into q
//converts string argv[1] into string key

string key = argv[1];
int klen = strlen(key);
int kposition = 0;


    //loop through the characters in array "text"
    for (int tposition = 0, n = strlen(text); tposition < n; tposition ++)
    {
        if isupper(key[kposition])
        {
            key[kposition] = ((key[kposition] - 'A') % klen) + 'A';
        }
        else if islower(key[kposition])
        {
            key[kposition] = ((key[kposition] - 'a') % klen) + 'a';
        }

        //determine if character is alphabetical
        if (isalpha(text[tposition])) 
        {

            //encrypt upper case characters
            if (isupper(text[tposition]))
            {
                //modulo magic to loop to beginning of alphabet after 'Z'
                text[tposition] = (((text[tposition] - 'A') + key[kposition]) % 26) + 'A';
                printf("%c", text[tposition]);
            }        
                //encrypt lower case characters
            else
            {
                //modulo magic to loop to beginning of alphabet after 'z'
                text[tposition] = (((text[tposition] - 'a') + key[kposition]) % 26) + 'a';
                printf("%c", text[tposition]);
            }
        }
        //if the input isn't alphabetical, then just print the input (spaces)
        else 
        {
            printf("%c", text[tposition]); 
        }
    kposition ++;

    }    
    printf("\n");
    return 0;

我按照您的建议将变量更加具体化,这有助于我查看代码。

尽管程序编译和运行正常,但我仍然无法获得正确的输出。

例如,当我运行: vigenere.c -bacon

并输入:“Meet me at the park”, 我得到的是:“Gxzq mr ni kyr frea”,而不是正确的答案:“Negh zf av huf pcfx”。

因此,大小写字母和空格都没有问题。问题出在key [kposition]的增量上。问题的一部分是我不太理解模数,因此不清楚模算术在做什么(除了给出两个数字的余数)。

如何更好地安排我的key [kposition]增量器?

2个回答

0

要自行调试此问题,请打印出 key[kposition] 的值

您需要限制 kposition

kposition = (kposition+1)%klen;

仅修改字母字符。

将当前密钥作为临时密钥,不要重新添加“A”,因为它不能正确地进行模26运算。

key[kposition] = ....;

去到
Tkey = ((key[kposition] - 'a') );

或者

Tkey = ((key[kposition] - 'A') );

你不要对 klen 取模,也不要更新 key。把其他的 key[kposition] 改成 Tkey。

#include <stdio.h>
#include <string.h>

int main( int argc, char ** argv)
{
    char  text[] = "Meet me at the park";
    char * key = "bacon";

//enters length of argv string into q
//converts string argv[1] into string key

//string key = argv[1];
int klen = strlen( key);
int kposition = 0;


//loop through the characters in array "text"
for (int tposition = 0, n = strlen( text); tposition < n; tposition++)
{
    unsigned Tkey = 0;
    if( isupper(key[kposition]) )
    {
        Tkey = ((key[kposition] - 'A') );
    }
    else if( islower(key[kposition]) )
    {
        Tkey = ((key[kposition] - 'a') );
    }

    //determine if character is alphabetical
    if (isalpha(text[tposition]))
    {

        //encrypt upper case characters
        if (isupper(text[tposition]))
        {
            //modulo magic to loop to beginning of alphabet after 'Z'
            text[tposition] = (((text[tposition] - 'A') + Tkey) % 26) + 'A';
            kposition = (kposition + 1) % klen;

            printf("%c", text[tposition]);
        }
        //encrypt lower case characters
        else
        {
            //modulo magic to loop to beginning of alphabet after 'z'
            text[tposition] = (((text[tposition] - 'a') + Tkey) % 26) + 'a';
            kposition = (kposition + 1) % klen;
            printf("%c", text[tposition]);
        }
    }
    //if the input isn't alphabetical, then just print the input (spaces)
    else
    {
        printf("%c", text[tposition]);
    }

}
printf("\n");
return 0;
}

谢谢你的指导,mksteve!将tkey和kposition变量分开使事情更加清晰! - DavidH

0

那个内部的for循环,增加j,对我来说似乎完全不合适和不必要。

你现在做的看起来已经很不错了:定义两个索引进行迭代,一个是明文(i),一个是密钥(j)。

然后你只需要迭代明文。你用密钥位置j的字符加密明文位置i的字符。然后你将两个迭代索引都增加1,通过使用模运算来保持密钥索引在范围内。

所以,基本上你只需要摆脱那个内部的for循环,并添加逻辑来随着外部循环增加j。

关于你的代码:建议将其分解得更细一些:编写一个函数来加密单个字符,并在循环中使用它。为变量使用更好的名称。 i和j没有描述性。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接