首页
学习
活动
专区
圈层
工具
发布

CS50替代
EN

Stack Overflow用户
提问于 2022-08-03 06:06:58
回答 1查看 223关注 0票数 2

这个问题集要求我们编写一个程序,该程序从CLI中获取用户的密钥,然后输入(明文),然后返回基于提供的密钥被加扰的密文版本。

我的代码返回给定任何键和明文的正确的密文,但是,使用内置的check50模块cs50时的明显输出是"",这是一个空字符串。

代码语言:javascript
复制
#include <cs50.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>

string encrypt(string keys, string inputs);

char newkey;
string ciphertext;

int main(int argc, string argv[])
{
    if (argc == 1)
    {
        printf("Please enter a key!\n");
        return 1;
    }

    if (argc > 2)
    {
        printf("You can only have one key. The key must not have any spaces!\n");
        return 1;
    }

    if (0 < strlen(argv[1]) && strlen(argv[1]) < 26)
    {
        printf("Key must contain 26 characters!\n");
        return 1;
    }

    for (int j = 0; j < strlen(argv[1]); j++)
    {
        if (!((argv[1][j] >= 'a' && argv[1][j] <= 'z') || (argv[1][j] >= 'A' && argv[1][j] <= 'Z')))
        {
            printf("Key must contain alphabets only!");
            return 1;
        }
    }

    for (int k = 0; k < strlen(argv[1]); k++)
    {
        for (int l = (k + 1); l < strlen(argv[1]); l++)
        {
            if (argv[k] == argv[l])
            {
                printf("There can be no duplicate alphabets in the key!");
                return 1;
            }
        }
    }

    string key = argv[1];

    string input = get_string("plaintext: ");

    encrypt(key, input);

    printf("ciphertext: %s\n", ciphertext);

}

string encrypt(string keys, string inputs)
{
    char ciphertexts[strlen(inputs)];
    for (int i = 0; i < strlen(inputs); i++)
    {
        if (islower(inputs[i]))
        {
            int index = inputs[i] - 97;
            newkey = keys[index];
            ciphertexts[i] = tolower(newkey);
        }
        else if (isupper(inputs[i]))
        {
            int index = inputs[i] -65;
            newkey = keys[index];
            ciphertexts[i] = toupper(newkey);
        }
        else
        {
            ciphertexts[i] = inputs[i];
        }
    }
    ciphertext = ciphertexts;
    printf("%s\n",ciphertexts);
    printf("%s\n",ciphertext);
    return ciphertext;
}

这些错误如下:

代码语言:javascript
复制
:( encrypts "A" as "Z" using ZYXWVUTSRQPONMLKJIHGFEDCBA as key
    expected "ciphertext: Z\...", not ""

这意味着,预期使用ZYXWVUTSRQPONMLKJIHGFEDCBA键和"A“、"Z”明文,但我的方案产出“。

我有一个printf语句打印密码文本,但是它不知怎么没有被捕获。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-08-03 11:48:00

内存管理

与其返回指向局部变量的指针(在函数结束@kaylum后不再有效),不如将一个指向何处形成加密字符串的指针传递到encrypt()

'\0' 别忘了

在形成空字符的加密字符串帐户时。@kaylum

避免重复调用strlen(inputs)

ch高级:避免负与 is...(ch)

还有一些其他的想法,也许:

代码语言:javascript
复制
#include <ctype.h>

void encrypt(char *ciphertexts, const char *keys, char *inputs) {
  // Let's work with unsigned char to avoid trouble negative char
  const unsigned char *ukeys = (const unsigned char*) keys;
  const unsigned char *uinputs = (const unsigned char*) inputs;
  unsigned char *uciphertexts = (unsigned char*) ciphertexts;
  size_t i = 0;

  // Code uses a do loop to catch the null character.
  do {
    if (islower(uinputs[i])) {
      int index = uinputs[i] - 'a';
      unsigned char newkey = ukeys[index];
      uciphertexts[i] = tolower(newkey);
    } else if (isupper(uinputs[i])) {
      int index = uinputs[i] - 'A';
      unsigned char newkey = ukeys[index];
      uciphertexts[i] = toupper(newkey);
    } else {
      uciphertexts[i] = uinputs[i];
    }
  } while (uinputs[i++]);
}

当区域设置不是"C“或非ASCII时,代码仍然有问题,但我们可以将这些问题作为高级问题处理。

高级

由于要加密的字符串可能很长,请考虑预先形成对所有char有效的密钥.也就是说。

代码语言:javascript
复制
unsigned char keys256[UCHAR_MAX + 1] = { 0 };
for (i = 0; i <= UCHAR_MAX; i++) {
  keys256[i] = i;
}
for (i = 0; key[i]; i++) {
  keys256['a' + i] = tolower(key[i]);
  keys256['A' + i] = toupper(key[i]);
}

然后在encrypt(..., keys256, ...)中,使用一个大大简化的循环。

代码语言:javascript
复制
  ...
  do {
    uciphertexts[i] = keys256(uinputs[i]);
  } while (uinputs[i++]);
  ...
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73216933

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档