CS50 2019 Vigenere-第二循环

问题描述 投票:0回答:1

首先,我得到了执行以下操作的代码:

key:ab明文:测试我密码:Tfsu ne

我当然是想让我的键跳过空间,(Tfsu mf),所以我尝试为我的键添加一个额外的计数器(m-counter),以确保它不会在每次遍历明文之后增加。结果是,现在我得到:

key:ab明文:测试我密文:Uftu nf

所以现在它的行为就像我的钥匙是bb。

有人可以解释“逻辑”,为什么第二个循环会导致输出发生这种变化,而不是仅在字符被加密(即按字母顺序)时才递增?

            for (int m = 0; m<l; m++)
                {
                   for(int e = 0; e<z; e++)
                       {
                          if (islower(plaintext[e]))
                           {                                         
                           ciphertext [e] = (plaintext [e] + shift `(argv[1][m%l]) - 'a') %26 + 'a';`
                            }
                           if (isupper(plaintext[e]))
                            {    

                            ciphertext [e] = (plaintext [e] + shift (argv[1][m%l]) - 'A') %26 + 'A';      
                            }  
                       }
                 }

             for (int q = 0; q<z;q++)
                  {          
                     if (!isalpha(plaintext[q]) )
                          {    
                           ciphertext [q] = (plaintext [q]);
                           } 
                  } 


printf ("ciphertext: ");
for (int i = 0; i<z; i++)   
    { 
    printf ("%c", ciphertext [i]);
    }
 printf("\n");
c cs50 vigenere
1个回答
0
投票

此循环for (int m = 0; m<l; m++)告诉程序使用密钥[0]加密整个纯文本,然后使用密钥[1]加密整个纯文本,依此类推,直到l。准确描述您看到的结果(即看起来像是用“ bb”加密,因为实际上是!)

通常,在这种情况下,不应使用循环来控制键索引。使用时应增加它的长度(当然也要“绕”长)。

© www.soinside.com 2019 - 2024. All rights reserved.