我一直坚持这一段时间了,我以为我有几次工作,只是为了找到更多的东西,我忘了。基本上问题集是创建使用C的Vigenere密码,规则可以发现hereVigenere的密码在C(CS50)
我基本上有它的工作,它只会允许正确的字符,如果关键字是相同的长度作为消息一切都很好。
我遇到的问题是想办法在消息被加密时重置关键字的循环,因此如果我输入关键字'A'和消息'This is a test',则应该读取加密的消息'这是一个测试',因为'A'在ASCII表中值得零移。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <stdbool.h>
int main(int argc, char * keyWord[]) {
int cipher[64], i, j, k, l;
char message[128];
// Validation - using alphaCheck Function
if (argc != 2 || alphaCheck(keyWord) == 2) {
return 1;
}
// For loop to convert to upper and set value ie A = 0 B = 1
for (i = 0, j = strlen(keyWord[1]); i < j; i++) {
cipher[i] = (toupper(keyWord[1][i]) - 65);
printf("%i\n", cipher[i]);
}
// Prompt the user for the message to encrypt
printf("Enter your secret message: ");
fgets(message, 128, stdin);
int keyCount = 0;
int p = strlen(keyWord[1]);
if (keyCount < p) {
keyCount++;
} else {
keyCount = 0;
}
for (i = 0, k = strlen(message); i < k; i++) {
if (isspace(message[i])) {
printf(" ");
} else if (isupper(message[i])) {
char c = (message[i] - 65 + cipher[i]) % 26 + 65;
printf("%c", c);
}
else {
char d = (message[i] - 97 + cipher[i]) % 26 + 97;
printf("%c", d);
}
}
}
// Function to check if alphabet characters.
int alphaCheck(char * argv[]) {
int length = strlen(argv[1]), n;
for (n = 0; n < length; n++) {
if (!isalpha(argv[1][n])) {
printf("Characters 'A-Z' for Keyword.\n");
return 2;
}
}
}
以下部分在我将其留在此处显示了我是如何尝试解决问题但失败的。
int keyCount = 0;
int p = strlen(keyWord[1]);
if (keyCount < p) {
keyCount++;
} else {
keyCount = 0;
}
感谢。这很好,当我运行gcc时,我肯定会添加这些标志。我目前只使用-lm。我将执行您所建议的更改,这些提示将有望用于未来的问题集。 –
添加了更改并可在此处查看http://codereview.stackexchange.com/questions/102737/vigeneres-cipher-in-c/104099#104099 –