简体   繁体   English

为什么两个不相等的字母在 C 中显示为等价?

[英]Why do 2 letters not equal to each other show up as equivalent in C?

I am making a program that does a simple encryption to a plaintext and outputs an encrypted ciphertext.我正在制作一个对明文进行简单加密并输出加密密文的程序。 Here is an example with YTNSHKVEFXRBAUQZCLWDMIPGJO being the key.这是一个以YTNSHKVEFXRBAUQZCLWDMIPGJO为关键的示例。

$ ./substitution YTNSHKVEFXRBAUQZCLWDMIPGJO
plaintext:  HELLO
ciphertext: EHBBQ

You can read the directions here if you would like to know more.如果您想了解更多信息,可以阅读此处的说明。

Here is my code:这是我的代码:

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

string ciphertext_generator(string key,string plaintext);
bool alphabetic_char_checker(string word);
bool alphabetic_char_repeat_checker(string word);


string alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

int main(int argc, string argv[])
{
    if (argc != 2)
    {
        printf("Please enter a key\n ");
        return 1;
    }
    if (strlen(argv[1]) != 26)
    {
        printf("The key must contain 26 characters.\n ");
        return 1;
    }
    if (alphabetic_char_checker(argv[1]) == false)
    {
        printf("The key must only contain alphabetic characters.\n ");
        return 1;
    }
    else if (alphabetic_char_repeat_checker(argv[1]))
    {
        printf("The key must not contain repeated characters.");
        return 1;
    }


    string plaintext = get_string("plaintext: ");
    string ciphertext = ciphertext_generator(argv[1],plaintext);
    printf("ciphertext: %s",ciphertext);
    return 0;
}
string ciphertext_generator(string key,string plaintext)
{
    string ciphertext = plaintext;
    for (int i = 0, k = strlen(ciphertext); i < k; i++)
    {
        bool capital = isupper(ciphertext[i]);
        ciphertext[i] = toupper(ciphertext[i]);
        for (int i2 = 0, a = strlen(alphabet); i2 < a; i2++)
        {
            if (plaintext[i] == alphabet[i2])
            {
                if (capital == true)
                {
                    ciphertext[i] = key[i2];
                }
                else
                {
                    ciphertext[i] = tolower(key[i2]);
                }
            }
        }
    }
    return ciphertext;
}

bool alphabetic_char_checker(string word)
{
    for (int i = 0, w = strlen(word); i < w; i++)
    {
        word[i] = toupper(word[i]);
        if (word[i] < 'A' || word[i] > 'Z')
        {
            return false;
        }
    }
    return true;
}
bool alphabetic_char_repeat_checker(string word)
{
    for (int i = 0, w = strlen(word); i < w; i++)
    {
        word[i] = toupper(word[i]);
        for (int i2 = 0; i2 < w; i2++)
        {
            if (word[i] == word[i2])
            {
                return false;
            }
        }
    }
    return true;
}

My question lies in the ciphertext_generator function that takes in a key and a plaintext and outputs an encrypted ciphertext.我的问题在于ciphertext_generator function 接受密钥和明文并输出加密的密文。 What happens here is that it iterates over ever letter in the plaintext and then for each letter, it iterates over the entire alphabet checking to see if the letter in the plaintext is in the alphabet and counts which index it is in the alphabet which then is then used as the index for the key which is then replaced correspondingly in the ciphertext string.这里发生的是它遍历明文中的所有字母,然后对于每个字母,它遍历整个字母表,检查明文中的字母是否在字母表中,并计算它在字母表中的哪个索引,然后是然后用作密钥的索引,然后在密文字符串中相应地替换。

The problem here is that on the first letter of the plaintext is iterating over every letter of the alphabet and it correctly finds and replaces the correct letter at the correct index which is N but it replaces the plaintext too and not just the ciphertext.这里的问题是,在明文的第一个字母上迭代字母表的每个字母,它在正确的索引 N 处正确找到并替换正确的字母,但它也替换了明文,而不仅仅是密文。

I am not trying to solve this problem just trying to understand what is going on as I have already debugged this.我不是想解决这个问题,只是想了解发生了什么,因为我已经调试过了。 What happens next is that the first letter keeps iterating through the rest of the alphabet and until it finds N and then replaces that with correct letter from the alphabet but with the plaintext too.接下来发生的事情是,第一个字母不断迭代字母表的 rest,直到找到 N,然后用字母表中的正确字母替换它,但也用明文替换。

** **

The Questions问题

Why does plaintext pick up letters when I only set the ciphertext to do so?当我只设置密文时,为什么明文会捡起字母?

To answer the question回答问题

Why does plaintext pick up letters when I only set the ciphertext to do so?当我只设置密文时,为什么明文会捡起字母?

On the first line of your function ciphertext_generator it says string ciphertext = plaintext;在您的 function ciphertext_generator的第一行它说string ciphertext = plaintext; This lines states that the ciphertext variable will be equal to the plaintext variable meaning the memory of ciphertext points to the same memory as plaintext so any changes made to ciphertext will be applied to plaintext.此行表明密文变量将等于明文变量,这意味着密文的 memory 指向与明文相同的 memory,因此对密文所做的任何更改都将应用于明文。

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

相关问题 我从不说 2 arrays 彼此相等,但在 output 他们在 C - I never say 2 arrays equal to each other but in output they do in C opencv:在c / c ++中检查两个iplimages是否彼此相等? - opencv: check if two iplimages is equal to each other in c/c++? 有没有办法在C中将文件指针设置为彼此相等? - Is there a way to set file pointers equal to each other in C? 为什么这两个代码段彼此等效(指针) - Why these two code snippets equivalent to each other (Pointers) C静态库不能互相识别 - 为什么我得到“未定义的对`function`的引用”? (链接器错误) - C static libraries do not recognize each other - why do I get “undefined reference to `function`”? (linker error) 为什么Lua C函数中的参数值显示在堆栈的底部? - Why do parameter values in Lua C functions show up at the bottom of the stack? 如果有两个相同的字母彼此相邻,则在字符串中添加其他字母 - add additional letters in a string if there are two same letters beside each other 三个不相等的随机数? - three random numbers which are not equal to each other? 设置不同的结构成员彼此相等 - Setting different struct members equal to each other 试图在 C 中获得一个等于其他变量的变量 - Trying to get a variable equal to other variables in C
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM