我試圖製作一個程序,即使用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。最好有實際的輸入/輸出示例比猜測:) –