我试图制作一个程序,即使用Vigenere的密码。每第三个字母都是错的。有人可以给我一个提示吗?感谢您的帮助。实现一个使用Vigenère密码加密消息的程序,C
#include <stdio.h>
#include <cs50.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
int main(int argc, string argv[])
{
int n = 0;
int j = 0;
int p;
int l;
int x = 0;
if(argc != 2)
{
printf("missing command-line argument\n");
return 1;
} else
{
printf("plaintext: ");
}
string text = get_string();
string key = (argv[1]);
printf("ciphertext: ");
for (j = 0, x = 0, n = strlen(text); j < n; j++)
{
p = (((text[j] - 65) + ((key[x] - 65) % strlen(key))) % 26) + 65;
l = (((text[j] - 97) + ((key[x] - 97) % strlen(key))) % 26) + 97;
if(isalpha(text[j]))
{
if(isupper (text[j]))
{
printf("%c", p);
x++;
}
if(islower (text[j]))
{
printf("%c", l);
x++;
}
}else
{
printf("%c", text[j]);
}
if(x == strlen(key))
{
x = 0;
}
}
printf("\n");
return 0;
}
如果int p,int l错误或者是其他问题,我无法确定。
听到的是输入/输出exempel
:) “一” “一个” 使用 “a” 作为关键字
加密:(加密 “barfoo” 为 “caqgon” 使用 “baz” 的作为关键字 \预期的输出,但不是 “密文:casgop \ n” 个
:(加密 “BaRFoo” 为 “CaQGon” 使用 “巴兹” 作为关键字 \预期的输出,但不是 “密文:CaSGoo \ n” 个
:(使用“CAQGON”加密“BARFOO” “BAZ” 作为关键字 \预期的输出,而不是 “密文:CASGOP \ n” 个
:(加密为 “xoqmd $!?” 使用 “巴兹” 作为关键字 \预期的输出, “世界$!?”但不是 “密文:xosmd $ \ n吗?”
:(加密 “的世界里,打个招呼!” 为使用 “巴兹” 作为关键字 \预期的输出,而不是“密文 “xoqmd,RBY gflkp!”: \ n“
您提供什么输入以及您期望输出什么?代码也可以做更好的缩进,因为它不是可读的。 –
@ChrisTurner我的猜测是作为参数传递的键(argv [1])是一个由3个字符组成的字符串。 acipi9尝试使用if(x == strlen(key)+1) –
@ J.A.I.L。最好有实际的输入/输出示例比猜测:) –