维格纳密码只有在处理C中的一个空格(“空格”)时才起作用-为什么? #包括 #包括 #包括 #包括 #包括 int main(int argc,字符串argv[]) { 字符串k=argv[1]; 字符串s=GetString(); int l=strlen(k); 对于(int i=0,n=strlen(s);i=65&&s[i]=97&&s[i]

维格纳密码只有在处理C中的一个空格(“空格”)时才起作用-为什么? #包括 #包括 #包括 #包括 #包括 int main(int argc,字符串argv[]) { 字符串k=argv[1]; 字符串s=GetString(); int l=strlen(k); 对于(int i=0,n=strlen(s);i=65&&s[i]=97&&s[i],c,cs50,vigenere,C,Cs50,Vigenere,这里是实现我所做的“字符串和键的单独计数器”注释的代码。它还使用字母代码'a'和'a'(并且避免使用'z'或'z')它不使用数字。它确实假设您处理的是一个单字节代码集(不是UTF-8,除非您在ASCII范围内工作),其中小写字母和大写字母都在一个连续的范围内(因此它不能可靠地与EBCDIC一起工作,但会与大多数其他代码集一起工作),并且它也会忽略重音字符。(它必须执行setlocale(“”才能获得特定于语言环境的字符解释,其中字符是字母。) 建议使用'a'和'z'(和'a'和'z')代替数字。

这里是实现我所做的“字符串和键的单独计数器”注释的代码。它还使用字母代码
'a'
'a'
(并且避免使用
'z'
'z'
)它不使用数字。它确实假设您处理的是一个单字节代码集(不是UTF-8,除非您在ASCII范围内工作),其中小写字母和大写字母都在一个连续的范围内(因此它不能可靠地与EBCDIC一起工作,但会与大多数其他代码集一起工作),并且它也会忽略重音字符。(它必须执行
setlocale(“”
才能获得特定于语言环境的字符解释,其中字符是字母。)


建议使用
'a'
'z'
(和
'a'
'z'
)代替数字。你的
GetString
函数是如何工作的?当“s”由空格(“”)组成时,“s”是什么意思“?处理非字母字符时,跳过密钥中的一个字母,因为您正在使用
i
逐步遍历密钥和要加密的数据。您需要使用单独的索引。@nsilent22:
GetString()当包含
标题时,
是一个半标准函数。您可以很容易地在网上找到源代码;它经常出现在这里。编程的一般规则:不要使用幻数。
 #include <stdio.h>
 #include <cs50.h>
 #include <string.h>
 #include <stdlib.h>
 #include <ctype.h>

 int main(int argc, string argv[])
 {
      string k = argv[1];
      string s = GetString();
      int l = strlen(k);

      for(int i = 0, n = strlen(s); i < n; i++)
      {
          if(s[i] >= 65 && s[i] <= 90)
          {
              int i2 = ((s[i]-65) + (k[i%l]-97)) % 26;
              printf("%c", i2+65);
          } else if(s[i] >= 97 && s[i] <= 122)
          {
              int i2 = ((s[i]-97) + (k[i%l]-97)) % 26;
              printf("%c", i2+97);
          } else
          {
              printf("%c", s[i]);
          }
      }
      printf("\n");
      return 0;
 }
#include <cs50.h>
#include <ctype.h>
#include <stdio.h>
#include <string.h>

int main(int argc, string argv[])
{
    if (argc != 2)
    {
        fprintf(stderr, "Usage: %s key\n", argv[0]);
        return 1;
    }

    string k = argv[1];
    int l = strlen(k);

    for (int i = 0; i < l; i++)
    {
        int c = k[i];
        if (!isalpha(c))
        {
            fprintf(stderr, "%s: non-alpha character %c in key string\n", argv[0], c);
            return 1;
        }
        k[i] = tolower(c);
    }

    printf("Enter a string to be encrypted:\n");
    string s = GetString();
    int n = strlen(s);

    for (int i = 0, j = 0; i < n; i++)
    {
        int c = (unsigned char)s[i];
        if (isupper(c))
            c = ((c - 'A') + (k[j++ % l] - 'a')) % 26 + 'A';
        else if (islower(c))
            c = ((c - 'a') + (k[j++ % l] - 'a')) % 26 + 'a';
        putchar(c);
    }
    putchar('\n');

    return 0;
}
./vc caesArandAbrAcaDabRa
Enter a string to be encrypted: 
It is reported that Caesar said "Veni, vidi, vici" when he conquered Britain.
Kt mk rvpbutfu tjaw Cbvscr wsiu "Vrqi, wzdk, vlcj" nhgn lw cfndxesvd Drltbzn.