【发布时间】:2019-08-07 16:27:25
【问题描述】:
我目前正在研究 CS50x 2019 的 pset2,特别是 Vigenère。 CS50 成绩册显示我上传到 GitHub 后完成了 93%。
我已经尝试了其他一些可以在网上找到的代码 sn-ps,但它们似乎不起作用。
这是我的程序中创建密文的部分:
string k = argv[1];
// Get the plaintext and print out the ciphertext
string s = get_string("plaintext: ");
printf("ciphertext: ");
// Iterate through plaintext letter by letter
for (int i = 0, n = strlen(s) ; i < n; i++)
{
int key = tolower(k[i % strlen(k)]) - 'a';
// Check if the letter is lowercase, uppercase or neither and print out the rotated character
if (islower(s[i]))
{
printf("%c", (((s[i] - 'a') + key) % 26) + 'a');
}
else if (isupper(s[i]))
{
printf("%c", (((s[i] - 'A') + key) % 26) + 'A');
}
else
{
printf("%c", s[i]);
}
}
printf("\n");
return 0;
文档中有一些示例,您可以使用代码进行测试。
以下示例不适用于我的代码:
$ ./vigenere bacon
plaintext: Meet me at the park at eleven am
ciphertext: Negh zf av huf pcfx bt gzrwep oz
我的输出是:
$ ./vigenere bacon
plaintext: Meet me at the park at eleven am
ciphertext: Negh ne og tjs qaty bt syfvgb bm
如您所见,前 4 个字符是正确的,但其余字符不正确。
【问题讨论】:
-
这个键是假的:int key = tolower(k[i % strlen(k)]) - 'a';因为,当明文字符等于 m 时, k[5%5]= k[0] = b 。所以 m+b = m+1 = n 。