【问题标题】:Cipher text in C,How to repeat key charactersC中的密文,如何重复关键字符
【发布时间】:2015-08-18 22:25:06
【问题描述】:
  • 解释 密文是通过将明文和密钥的相应字符“相加”在一起而从明文生成的。如果明文比密钥短,则只使用部分密钥。同样,如果明文比密钥短,则密钥将被多次使用。

    例如,用密钥“CAT”对明文“HELLO”进行编码:

明文:你好

密钥:CATCA

密文:KFFOP

并使用密钥“FIDO”对明文“DOG”进行编码:

明文:狗

密钥:FID

密文:JXK

要将两个字母相加,请使用以下约定:A=1、B=2、...、Z=26。如果两个字母的总和大于 26,则从总和中减去 26。例如:A + E = 1 + 5 = 6 = F,D + X = 4 + 24 = 28 = 2 = B。

  • 现在我的代码的问题是,如果关键字符较少,我无法重复关键字符以进一步编码纯文本,如何重复关键字符,以便进一步编码?

帮帮我。

这是我的代码:

#include<stdio.h>
    #include<string.h>
    int main()
        {
            char str[100],k[50],str1[100];
            int i,n;
            gets(str);// Input plain text. 
            gets(str1);//Input key.
             for(i=0;str[i]!='\0';i++)
                    {
                         n=(str[i]-65+1)+(str1[i]-65+1);//Extracting the numerical position and adding them.
                         if(n>26) //if numerical value exceeds 26 then subtracting 26 from it and getting the numerical value.
                          {
                             n=n-26;
                          }
                         str[i]=n+64;//storing the ciphered character.
                    }

              for(i=0;str[i]!='\0';i++)//printing the ciphered characters.
              printf("%c",str[i]);
              return 0;

        }

【问题讨论】:

    标签: c encryption vigenere


    【解决方案1】:

    您可以使用另一个循环变量来使键的索引每次达到其长度时为 0。在这种情况下,我使用了变量 j。试试这个代码:

    #include<stdio.h>
    #include<string.h>
    int main()
        {
            char str[100],k[50],str1[100];
            int i,n;
            gets(str);// Input plain text. 
            gets(str1);//Input key.
            int lenk=strlen(str1),j;   //calculate length of key
             for(i=0,j=0;str[i]!='\0';i++,j++)
        {
             if(j==lenk) j=j-lenk;      //make j=0
             n=(str[i]-65+1)+(str1[j]-65+1);    // add str1[j] instead
             if(n>26) 
              {
                 n=n-26;
              }
             str[i]=n+64;//storing the ciphered character.
    
        }
    
              for(i=0;str[i]!='\0';i++)
              printf("%c",str[i]);
              return 0;
    
        }
    

    请注意,这仅适用于大写字母,您必须更改小写字母的代码

    【讨论】:

    • 现在我的目标是处理大写字母,现在我将对其进行修改并尝试包含小写字母。
    • @Sourav 解释我这一行 if(j==lenk) j=j-lenk;
    • 每次 j 等于它的长度,即密钥结束时,我们从中减去密钥的长度,因此它再次变为 0,从而从头开始重复密钥
    【解决方案2】:

    在编写循环时,用于索引键的变量应重置为 0 以重复键(如果原始文本长度较大)。

    for(int i = 0, j = 0; input[i] != '\0'; ++i, ++j) {
        new_char = (input[i] - 64) + (key[j] - 64);
        new_char = adjust(new_char);
        cipher[i] = new_char + 64 ;
        if(j == (key_length - 2)) // if j is at the end, excluding null character, then make j = -1, which gets incremented to j = 0 in the next loop iteration
            j = -1;
    }
    

    还可以使用fgets 输入字符串,不要 使用gets。 C 中的字符串可以使用%s 格式说明符打印,而无需编写显式循环来输出字符。为此,将密码字符数组的最后一个元素设为\0 字符。

    【讨论】:

    • @Akashthanks 提供帮助和代码并建议我使用 fgets。
    【解决方案3】:

    您也可以使用模运算来重复字符。

    for(i=0;str[i]='\0';i++)
    {
        n = (str[i]-65+1 + (str1[i % lenk]-65 +1);    
        n = (n % 26) + 1;
        str[i] = n+64;
        //storing the ciphered character.
    
    }
    

    表达式i % 26 自动将值从 0 旋转到 25。

    同样可以将 n 从 1 旋转到 26。

    【讨论】:

      猜你喜欢
      • 2011-04-03
      • 1970-01-01
      • 1970-01-01
      • 2017-01-06
      • 1970-01-01
      • 2010-11-11
      • 1970-01-01
      • 1970-01-01
      • 2021-10-06
      相关资源
      最近更新 更多