【问题标题】:vigenere cipher c languagevigenere密码c语言
【发布时间】:2016-11-30 04:23:24
【问题描述】:

我正在尝试用 C 语言制作 vigenere 密码。输入只能是字母字符(a->z)。

目前我的问题是输出仅输出 4 个字符并输出字母表之外的奇怪字符。我创建了 if 语句来防止这种情况,但似乎它们不起作用。有什么建议吗?

#include <stdio.h>


int main(){
    int i=0;
//Vigenere Cipher-- keyword is "apple"
//a = 1  value shift
//p = 16 value shift
//p = 16 value shift
//l = 17 value shift
//e = 5  value shift
 //cleaning out string array
 char guy[100];

printf("Enter the plain text: ");
fgets(guy, 100, stdin);//takes user's input

while (guy[i] != '\0'){ //while loop that runs until it reaches the end of the string

    if ((i%5==0) || i==0){ //checks to see which character it is in the string, for instance the numbers 0,5,10,15,20 should all be added by 1
    guy[i] = guy[i]+1;
    if (guy[i]>'z' && guy[i]<'A'){
            guy[i]-25;
    }
    if (guy[i]>'Z' && guy[i]>'A'){
        guy[i]-25;
    }
    }

    if (((i-1)%5==0) || i==1){ //all numbers that are second in the key word 'apple', such as 1,6,11,16
        guy[i]=guy[i]+16;
        if (guy[i]>'z' && guy[i]<'A'){
            guy[i]-25;
        }
        if (guy[i]>'Z' && guy[i]>'A'){
            guy[i]-25;
        }
    }
    if (((i-2)%5==0) || i==2){// all numbers that are third to the key word 'apple' , such as 2,7,12,17,22
        guy[i]=guy[i]+16;
        if (guy[i]>'z'&& guy[i]<'A'){
            guy[i]-25;
        }
        if (guy[i]>'Z'&& guy[i]>'A'){
            guy[i]-25;
        }
    }
    if(((i-3)%5==0) || i==3){// all numbers that are fourth to the key word 'apple', such as 3,8,13,18
        guy[i]=guy[i]+17;
        if (guy[i]>'z'&&guy[i]<'A'){//takes care of z
            guy[i]-25;
    }
        if (guy[i]>'Z' && guy[i]>'A'){//takes care of Z
            guy[i]-25;
        }
    }
    if(((i-4)%5==0) || i==4){// all numbers that are fifth in the key word 'apple', such as 4,9,14,19
        guy[i]=guy[i]+5;
        if (guy[i]>'z'&& guy[i]<'A'){
            guy[i]-25;
        }
        if (guy[i]>'Z' && guy[i]>'A'){
            guy[i]-25;
        }
    }
    else {
    i++;
    }
    }
printf("Encrypted text is: %s\n",guy);
}

【问题讨论】:

  • 你真的需要写一个函数char encrypt_char(char plain_text, char key)然后用guy[i] = encrypt_char(guy[i], "apple"[i%5]);调用它。此外,if (guy[i]&gt;'Z'&amp;&amp; guy[i]&gt;'A')if (guy[i]&gt;'Z') 相同。您还应该学习如何使用调试器,这样您就可以逐步了解正在发生的事情,还应该考虑应该如何加密 SPACE 或数字等(如果有的话)。

标签: c if-statement encryption vigenere


【解决方案1】:

有一个函数 encrypt_char() 为你做加密。

void encrypt_char(char *character, unsigned int offset)
{
    if('a' <= *character && 'z' >= *character)
    {
        *character = ((*character + offset - 'a') % 26) + 'a';
    } 
    else if('A' <= *character && 'Z' >= *character)
    {
        *character = ((*character + offset - 'A') % 26) + 'A';
    }
}

用你的偏移值调用它。

if (i%5==0){
    encrypt_char(&(guy[i]), 1);
} else if ((i-1)%5==0){
     encrypt_char(&(guy[i]), 16);
} else...

【讨论】:

  • || i==0 等当然不需要0%5 == 0 等。
  • 是的!我刚刚从问题中复制了代码。删除它。
【解决方案2】:

与其放弃整个游戏,这里有一些代码(未经测试!)基于我的评论:

char encrypt_char(char plain_text, char key)
{
    char offset = tolower(key) - 'a' + 1;
    if (islower(plain_text))
        return (plain_text - 'a' + 1 + offset) % 26 + 'a';
    else if (isupper(plain_text))
        return (plain_text - 'A' + 1 + offset) % 26 + 'A';
    else
        return plain_text;
}

这可能会在路上的某个地方偏离,所以祝你调试愉快!

【讨论】:

  • 偏移量计算看起来错误(或者有问题!)。根据问题,“p”应该是 16,“l”应该是 17。我认为应该有一个查找表。
  • 我在上面提到它被encrypt_char(guy[i], "apple"[i%5])调用。
  • 我明白这一点。我要说的是关键不是字母集中字符的索引。如果正确理解您的逻辑,{a,p,p,l,e} 的偏移值是 {1, 16, 16, 12, 5} 这是错误的。它应该是 {1, 16, 16, 17, 5}。希望我说得通!
  • @MayurK 我怀疑17 是一个错误,因为它声明apple,而不是appqe 是关键......
猜你喜欢
  • 1970-01-01
  • 2016-10-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多