V @ genere加密只能直到处理空间(QUOT;" c)中 - 为什么? [英] Vigenere Cipher only works up until dealing with a space(" ") in C - why?

查看:174
本文介绍了V @ genere加密只能直到处理空间(QUOT;" c)中 - 为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 #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;
 }

我已经为许多部分除去,我可以为了使code更相关的问题。基本上,为什么中,并没有在执行此code工作时的s没有任何空间()S由空格()?

I have removed as many parts as I can in order to make the code more relevant to the question. Basically why does this code work when "s" does not have any space(" ") in it and doesn't when "s" consists of space(" ")?

由于大多数人可能都知道这个想法是参数在ARGV进入[1]是关键词的密码。然后用户输入一个纯文本,以密码(S)。它的工作原理,当我与各单词或句子尝试,如果它不包含任何空间,。我只是不明白这背后的逻辑。为什么周期休息,如果 S [I] 不是前两个条件之一 - 我本来以为别人的条件将工作

As most of you may know the idea is the argument entered at argv[1] is the "keyword" for the cipher. User then inputs a "plain-text" to cipher (s). It works when I try with various words or sentences if it doesn't include any space, " ". I just don't understand the logic behind this. Why does the cycle break if s[i] is not one of the first two conditions - I would have thought that "else" condition would work.

我真的AP preciate,如果有人可以提供一些线索在这 - !非常感谢提前

I would really appreciate it if someone can shed some light on this - many thanks in advance!

PS:我知道有在顶部一些额外的库,在用户输入的argv [1] 通过未经过验证因而isalpha() 。我只是想了解的循环过程,现在好,我有这些检查在另一个文件准备好了。

ps: I know there are some extra libraries at the top and the user input at argv[1] is not verified via isalpha(). I just want to understand the cycle process better for now, I have those checks in another file ready.

推荐答案

下面是code实现评论说我做了'字符串和按键独立的计数器。它还使用字母codeS 'A''A'(以及避免使用'Z''Z'),而不是数字。它假定你正在处理一个单字节code组(不是UTF-8,除非你在ASCII范围内工作),其中的小写和大写字母分别在一个连续的范围内(所以它不会与EBCDIC可靠地工作,但会与其他大多数code套),并且它也忽略了重音符号。 (它必须做的的setlocale()来获取特定语言环境间pretations其中的字符是字母)。

Here is code that implements the 'separate counters for string and key' comment that I made. It also uses the letter codes 'a' and 'A' (and avoids needing to use 'z' or 'Z') instead of using numbers. It does assume that you are dealing with a single-byte code set (not UTF-8 unless you're working in the ASCII range) where the lower-case and upper-case letters are each in a contiguous range (so it won't work reliably with EBCDIC, but will with most other code sets), and it also ignores accented characters. (It would have to do setlocale("") to get locale-specific interpretations of which characters are letters.)

#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;
}

下面是一个演示使用的弱点运行示例A在这个V @ genere加密密钥的字母之一:

Here is a sample run that demonstrates the weakness of using 'a' as one of the letters in the key for this Vigenere cipher:

./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.

这篇关于V @ genere加密只能直到处理空间(QUOT;&QUOT; c)中 - 为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆