简体   繁体   English

C语言环绕中的Vigenere密码

[英]Vigenere cipher in C wraparound

I'm having trouble writing the last part of my code for an assignment which involves writing a Vigenere cipher. 我在编写涉及编写Vigenere密码的作业的代码的最后部分时遇到麻烦。 The encryption part is working fine, but I'm having trouble figuring out how to repeat the encryption word/keyword. 加密部分工作正常,但是我很难弄清楚如何重复加密字/关键字。 So it works fine if the message that needs to be encrypted is smaller or equal to the keyword and otherwise it puts out another couple of characters, that seem encrypted, but aren't. 因此,如果需要加密的消息小于或等于关键字,则效果很好,否则它会输出另外两个看起来似乎已加密但不是的字符。

This is the code so far: 到目前为止,这是代码:

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

int main(int argc, string argv[])
{
    if (argc != 2)
    {
        printf("YELL!\n");
        return 1;
    }
    //check if the number of command line arguments is correct, if not, YELL!

    string keyword = (argv[1]);
    //get keyword

    for (int j = 0, n = strlen(keyword); j < n; j++)
    {
    if(!isalpha(keyword[j]))
        {   
        printf("YELL!\n");
        return 1;
        }
    }
    //check if the keyword is only alphabetical, if not, YELL!

    string message = GetString();
    //get plaintext

    for (int j = 0, n = strlen(keyword); j < n; j++)
    {
        if (isupper(keyword[j]))
        {
            keyword[j] = (keyword[j] - 'A');
        }

        if (islower(keyword[j]))
        {
            keyword[j] = (keyword[j] - 'a');
        }
    }
    //this is to get the numerical values from the ascii values of the keyword.

    for (int i = 0, j = 0, n = strlen(message); i < n; i++, j++)
    //counting through the message & the cypher
    {
        if (isalpha(message[i]))
        {
            if (isupper(message[i]))
            {
            message[i] = (((message[i] - 'A') + keyword[j]) % 26 + 'A');
            }

            if (islower(message[i]))
            {
            message[i] = (((message[i] - 'a') + keyword[j]) % 26 + 'a');
            }

            //adding a keyword value [j] to message [i] and converting back to ascii value, 
            //individually for upper and lowercase characters.
        }
    printf("%c", message[i]);
    } 
}

It's probably an easy solution, but I just can't figure it out. 这可能是一个简单的解决方案,但我只是想不通。 Any help would be vastly appreciated! 任何帮助将不胜感激!

It's a miracle encryption is working for you. 这是一个奇迹加密正在为您工作。 I think is is not, as your loop is clearly might get j past the keyword length and then keyword[j] will be out of bounds and exhibit undefined behavior. 我认为不是,因为您的循环显然可以使j超出关键字长度,然后keyword[j]将超出范围并表现出未定义的行为。 You need only to iterate on i over the message length and index the keyword with keyword[i % strlen(keyword)] , such that the index will go cyclically from 0 to the length of the keyword minus one. 您只需要在消息长度上对i进行迭代,并用keyword[i % strlen(keyword)]索引keyword[i % strlen(keyword)] ,以便索引从0到keyword的长度减一循环地循环。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM