Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
CS50维格纳-奇怪模式_C_Encryption_Vigenere_Cs50 - Fatal编程技术网

CS50维格纳-奇怪模式

CS50维格纳-奇怪模式,c,encryption,vigenere,cs50,C,Encryption,Vigenere,Cs50,我和维格纳之间有点问题,希望得到一些帮助 /* 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[]) {

我和维格纳之间有点问题,希望得到一些帮助

/*
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⎼@☃德5▮:·/┬⎺⎼┐⎽⎻▒行政长官/⎻⎽E├2 $ └▒┐E┴☃±e┼E⎼完成后,我的终端中的所有字母均为e

对于BaZ,我的结果是这样的:

有什么值得注意的吗 值得注意的事 值得注意的事情 值得注意的事情 值得注意的事 附注 附注 值得注意的 值得注意 值得注意 f注 f注 笔记 注意 注意 注意 ├E E
根据中的示例,下面的代码按预期工作

在线:

cipher[i] = ((key[k] - 65) + (cipher[i] - 65)) % 26;
您忘记了将A的ASCII码添加回结果。因此,密码[i]将是一个从0到25的字符,所有这些字符都是不可打印的控制代码,特别是null/0字符被C字符串函数视为字符串结束标记

该行:

cipher[i] = ((key[k] - 97) + (cipher[i] - 97)) % 26;
有同样的错误

另外,在这两行中,您都假设密钥字符的大小写与要加密的消息字符的大小写相同。如果不是这样,您的加密结果将不正确。为了解决这个问题,我建议在主加密循环之前将整个密钥转换为所有大写或小写

您还可以更进一步,在循环之前将键转换为0到25之间的数字。但是,执行此操作后,您将无法在键上使用strlen,因为strlen查找字符串中第一个null=0字符的位置。相反,您必须在转换密钥之前运行strlen,并将其结果保存在变量中供以后使用。实际上,这在任何情况下都是一个有用的优化,您也应该为消息进行优化

cipher[i] = ((key[k] - 97) + (cipher[i] - 97)) % 26;