[英]What isn't working properly with my Vigenere cipher code?
I'm trying to make a Vigenere cipher code in C and I have done something that is wrong and I can't fix it... How do understand that something goes wrong? 我正在尝试用C语言编写Vigenere密码,但我做错了什么,我无法解决。。。如何理解出问题了? Well I have some examples with keyword and result cipher with Vigenere cipher like 好吧,我有一些关键字示例和Vigenere密码的结果密码,例如
bacon
关键字: bacon
Meet me at the park at eleven am
文字: Meet me at the park at eleven am
Negh zf av huf pcfx bt gzrwep oz
正确的结果: Negh zf av huf pcfx bt gzrwep oz
Tegh ne og tjs qaty bt syfvgb bm
我的代码结果具有相同的文本和关键字: Tegh ne og tjs qaty bt syfvgb bm
Code: 码:
int main(int argc, string argv[])
{
string keyWord;
if( argc != 2 )
{
printf("Wrong Argument");
return 1;
}
else
{
keyWord = argv[1];
//check if argument is
//only alphabetical characters
for(int i = 0; i < strlen(keyWord); i++)
{
char c = keyWord[i];
if( !isalpha(c) )
{
printf("Your Keyword Must Contain Only alphabetical characters\n");
return 1;
}
}
}
//todo
printf("Enter Plain Text\n");
string plainText = GetString();
for(int i = 0; i < strlen(plainText); i++)
{
char c = plainText[i];
int keyWordWrapper;
char keyC;
if(isalpha(c))
{
keyWordWrapper = i % strlen(keyWord);
keyC = keyWord[keyWordWrapper];
if(islower(c))
{
int key = keyC - 'a';
c = (c - 'a' + key) % 26 + 'a';
}
if(isupper(c))
{
int key = keyC - 'A';
c = (c - 'A' + key) % 26 + 'A';
}
}
printf("%c",c);
}
printf("\n");
return 0;
}
GetString()
is declared in a header and defined in a library that I'm using (it's like scanf
). GetString()
在标头中声明,并在我正在使用的库中定义(就像scanf
一样)。
this is the updated code 这是更新的代码
int main(int argc, string argv[])
{ string keyWord; {string keyWord;
if( argc != 2 )
{
printf("Wrong Argument");
return 1;
}
else
{
keyWord = argv[1];
//check if argument is
//only alphabetical characters
for(int i = 0; i < strlen(keyWord); i++)
{
char c = keyWord[i];
if( !isalpha(c) )
{
printf("Your Keyword Must Contain Only alphabetical characters\n");
return 1;
}
}
}
string plainText = GetString();
int j;
for(int i = 0; i < strlen(plainText); i++)
{
j++;
char c = plainText[i];
int keyWordWrapper;
char keyC;
if(j > strlen(keyWord))
j = 0;
if(isalpha(c))
{
keyWordWrapper = i % strlen(keyWord);
keyC = keyWord[keyWordWrapper];
int key;
tolower(c);
if(islower(keyC))
key = keyC - 'a';
if(isupper(keyC))
key = keyC - 'A';
c = (c - 'a' + key) % 26 + 'a';
}
printf("%c",c);
}
printf("\\n"); printf(“ \\ n”);
return 0; 返回0; } }
There are two problems in the code. 代码中有两个问题。
First is the treatment of upper case letters in the keyword. 首先是关键字中大写字母的处理。 Note that in one case, the code subtracts a
from keyC, and in the other A
is subtracted. 请注意,在一种情况下,代码从keyC中减去a
,而在另A
情况中, A
被减去。 But that's based on the case of the plain text character. 但这是基于纯文本字符的情况。 That subtraction needs to be based on the case of the letter in the keyword . 该减法需要基于关键字中字母的大小写 。
Second, the code advances to the next character in the keyword for every character in the plain text. 其次,对于纯文本中的每个字符,代码都会前进到关键字中的下一个字符。 The "correct result" doesn't advance to the next character of the keyword if the plain text character is a space
character. 如果纯文本字符为space
字符,则“正确结果”不会前进到关键字的下一个字符。
Here's an example of what I'm talking about for the second problem 这是我在谈论第二个问题的示例
text Meet me at
keyC baco nb ac
i 0123456789 i must always increment to the next char in plain text
k 0123 40 12 index into the keyword does not increment on non-alpha
Therefore k
cannot be computed directly from i
with the line 因此,不能用直线直接从i
计算k
keyWordWrapper = i % strlen(keyWord);
Instead k
needs to be initialized to 0
and then incremented only when the plain text contains an alpha character. 相反,需要将k
初始化为0
,然后仅在纯文本包含字母字符时才将其递增。 The following line will compute the correct index into the keyword. 下一行将计算正确的关键字索引。
keyWordWrapper = k % strlen(keyWord);
The only difference is that i
is replaced by k
and k
only increments when the plain text has an alpha character. 唯一的区别是, i
被k
替换,并且k
仅在纯文本具有字母字符时递增。
You should convert the key to all lower case (or all upper case) and then use the same expression in both shift blocks: 您应该将键转换为所有小写(或全部大写),然后在两个移位块中使用相同的表达式:
int key = keyC - 'a'; // Or 'A' if you convert to upper
You should remove the strlen(plainText)
from the condition of the for
loop; 您应该从for
循环的条件中删除strlen(plainText)
; it converts a linear algorithm into a quadratic one. 它将线性算法转换为二次算法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.