CS50 Vigenere - 奇怪的图案

标签 c encryption vigenere cs50

我在使用 Vigenere 时遇到了一些问题,需要一些帮助。

/*
This program is a Vigenere Cipher.
I am Daniel of Asguard.
*/

#include <cs50.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, string argv[])
{
string cipher; //this is a placeholder for the ciphered message.
char * key = argv[1];
int i = 0;


if (argc != 2) //this is meant to trigger if you don't enter the right call. So 
{
    printf("Please enter the cipher key when you call the program, such as './CaesarCipher 7'.\n"); //
    return 1;
}

if (!isalpha(key[i])) //this is meant to trigger if you don't enter the right call. So 
{
    printf("Please only enter a word, no numerical numbers please."); //
    return 1;
}

do
{
    //printf("Please enter the message you would like to have converted, please. \n");
    cipher =  GetString(); 
}
while (cipher == NULL);

for (int i = 0, k = 0, n = strlen(cipher); i < n; i++, k++) //this is so the code knows to change only the characters in the sting cipher.
    {
        if (k >= strlen(key))
        {
            k = 0;
        }
            {
            if (isupper(cipher[i]))
                {
                    //cipher[i] = 'A' + (((cipher[i] - 'A') + (key[k]) - 'A')  % 26);
                    cipher[i] = ((key[k] - 65) + (cipher[i] - 65)) % 26;
                    printf("%s\n", cipher);
                }
            else (islower(cipher[i]));
                {
                    //cipher[i] = 'a' + (((cipher[i] - 'a') + (key[i]) - 'a') % 26);
                    cipher[i] = ((key[k] - 97) + (cipher[i] - 97)) % 26;
                    printf("%s\n", cipher);
                }
        }
    }
printf("%s\n", cipher);
return 0;
}

当我这样做时,我的结果会出现奇怪的字符:⎽c▒⎺e┼├⎼▒┤└e⎼@☃de5▮:·/┬⎺⎼┐⎽⎻▒ce/⎻⎽e├2 $ └▒ ┐e ┴☃±e┼e⎼e 代表完成后我终端中的所有字母。

对于 BaZ,我的结果最终如下所示:

  • 任何值得注意的事情
  • 有什么值得注意的
  • 注意事项
  • 注意事项
  • 注意事项
  • 注释
  • 注释
  • 值得注意
  • 值得注意
  • 值得注意
  • f 注意
  • f 注意
  • 注意
  • 注意
  • 注意
  • 注意
  • ├e
  • e

最佳答案

下面的代码按照 https://en.wikipedia.org/wiki/Vigen%C3%A8re_cipher 中的示例按预期工作

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

int main (void)
{ 
    const char  *cipher = "ATTACKATDAWN";
    const char  *key = "LEMON";
    char *newkey;
    char *p, *q;
    int i =0;
    int col, row;
    if (strlen (key) < strlen (cipher))
    {
printf ("key %s \n", key);
printf ("cipher \t%s \n", cipher);
newkey = malloc ( strlen (cipher) +1);
strcpy ( newkey, key);
p = (char *) (newkey + strlen (newkey)) ;
q = (char *) key; 
i =strlen (key);
while (i < strlen (cipher) )
{
        i++;
        if (*q == 0)
    q = (char *) key;
        *p = *q;
        p++;
        q++;
}
    *p = 0;
    printf ("newk \t%s \n", newkey);
    }
    p = (char *) newkey;
    q= (char *) cipher;
    int a[1] ;
    a[1] =0;
    for (i =0  ; i < strlen(newkey); i++)
    {
row = *p -65;
col = *q -65;
if (col+row > 26)
        a [0] = 65 + col+row -26;
else
        a [0] = 65 +col+row;
printf ("%s",(char *)  &a[0]);
p++;
q++;
    }
    printf ("\n");
    free( newkey);
/*L X F O P V E F R N H R*/
return 0;
}

关于CS50 Vigenere - 奇怪的图案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33459917/

相关文章:

c++ - 使用 C 调用 PostgreSQL 存储过程

c - 结构不返回正确的值

c# 格式保留整数加密

java - 充气城堡 "encoded key spec not recognised"

c - cs50 的 Vigenere 密码程序错误

python - ASCII 维吉尼亚密码无法正确解密

c - C 中的结构、城市和距离、图表和 Dijkstras

c - 实现系统调用时,如何将系统调用号暴露给用户空间?

mysql - 如果有非法字符,如何将密文保存到数据库?

c - C 语言的维吉尼亚密码 : Space not being ignored