如何在C中调试Vigenere密码?

问题描述 投票:1回答:1

我正在尝试制作一个Vigenere密码。我的问题是我没有得到预期的输出。运行程序时,它会输出:HFNLP WPTLE。正确的输出应为:HFNLP YOSND。

我认为问题在于模数(mod)的使用不当。当我尝试用变量i环绕键(ABC)时,plainText中的空格(“”)也会包装,直接影响包装的结果。我不知道该怎么做才能获得正确的输出。

string plainText = "HELLO WORLD";   
string keyword =   "ABC";

 for(int i = 0; i < strlen(plainText);i++)
 {  

    int wrap =  (int) strlen( keyword) % (int) strlen(plainText);

     if(isalpha(plainText[i]))
     {

     int upper = 'A' + (plainText[i] + (toupper(keyword[i % wrap]))) % 26;
     printf("%c", upper);

     }
c vigenere
1个回答
3
投票

非字母字符上的键索引不得增加。

修复的一个例子:

char *keyp = keyword;
char ch;
for(int i = 0; ch = plainText[i]; i++){  
    if(isalpha(ch)){
        putchar('A' + (toupper(ch) - 'A' + toupper(*keyp++) - 'A') % 26);
        if(!*keyp)
            keyp = keyword;
    } else
        putchar(ch);
}
© www.soinside.com 2019 - 2024. All rights reserved.