2016-11-30 5 views
0

シーザーとビジェーレ暗号を作る次のプログラムを作成しました。私はシーザー暗号が正しく動作するようにしていますが、Vigenereを正しく動作させることはできません。C言語-if文の発行

私のif文には、間隔が5のさまざまな数値がすべて「catch」されます。プログラムを実行するたびに、Viegenere暗号は全く同じ入力を出力します。これは私がif文で間違いを犯しているからだと信じていますが、私はそれが何であるか分かりません。

Viegenere暗号の始まりは// Vigenere Cipher--キーワードとしてコードにコメントしていると、再びデータを暗号化する前に、「りんご」

#include <stdio.h> 


int main(){ 
int i=0; 
//setting the individual slot number for the array-- later used in the while loop 
char guy[100]; 
printf("Enter the plain text:"); 
fgets(guy,100,stdin); //takes user's input-- such as "abc" and puts it into its respective slot in the array guy[10] r-right? 

while (guy[i] != '\0'){ //while loop that runs until it reaches the end of the string 


    if ((guy[i]) >= 'A' && (guy[i]<= 'Z')){ //moves capital letter values up 1 
     if (guy[i]=='Z'){ 
      guy[i]=guy[i]-25; 
     } 
     else { 
     guy[i]=guy[i]+1; //makes the current "slot" number go up 1 value. Example: a = 97 + 1 -> b = 98 
     } 
     } 
    if ((guy[i]) >= 'a' && (guy[i]) <= 'z'){// moves lower case letter values up 1 
     if (guy[i]=='z'){ 
      guy[i]=guy[i]-25; 
     } 
     else{ 
     guy[i]=guy[i]+1; 
     } 
    } 
    i++; //moves the array's interval up to the next "slot" 

} 
printf("Encrypted text is: %s\n",guy); 

//Vigenere Cipher-- keyword is "apple" 
//a = 1 value shift 
//p = 16 value shift 
//p = 16 value shift 
//l = 17 value shift 
//e = 5 value shift 

printf("Enter the plain text: "); 
fgets(guy,100,stdin);//takes user's input 

while (guy[i] != '\0'){ //while loop that runs until it reaches the end of the string 

    if (i%5==0 || i==0){ //checks to see which character it is in the string, for instance the numbers 0,5,10,15,20 should all be added by 1 
    guy[i] = guy[i]+1; 
    } 

    if ((i-1)%5==0 || i==1){ //all numbers that are second in the key word 'apple', such as 1,6,11,16 
     guy[i]=guy[i]+16; 
    } 
    if ((i-2)%5==0 || i==2){// all numbers that are third to the key word 'apple' , such as 2,7,12,17,22 
     guy[i]=guy[i]+16; 
    } 
    if((i-3)%5==0 || i==3){// all numbers that are fourth to the key word 'apple', such as 3,8,13,18 
     guy[i]=guy[i]+17; 
    } 
    if((i-4)%5==0 || i==4){// all numbers that are fifth in the key word 'apple', such as 4,9,14,19 
     guy[i]=guy[i]+5; 
    } 
    else { 
    i++; 
    } 
    } 
printf("Encrypted text is: %s\n",guy); 
} 
+0

私はあなたのエラーを再現することはできません。しかし、私はstdioで練習していないので、未定義の動作を引き起こすバグがあるかもしれません。クリアテキストをハードコードし、問題が解決しないかどうかを確認することをお勧めします。 – Beta

+0

l = 12値シフト。 '% 'を使用する – BLUEPIXY

+0

モジュロ演算子についてもう少し詳しくお読みください。 (i%5 == 3 || i == 3) 'と同じです。これは'(i-3)%5 == 0 || i == 3) 'と同じです。 5 == 3) '。 – Gerhardh

答えて

4

です - あなたは "の値を再初期化する必要がありますi」の変数

#include <stdio.h> 


    int main(){ 
    int i = 0; 
    //setting the individual slot number for the array-- later used in the while loop 
    char guy[100]; 
    printf("Enter the plain text:"); 
    fgets(guy, 100, stdin); //takes user's input-- such as "abc" and puts it into its respective slot in the array guy[10] r-right? 

    while (guy[i] != '\0'){ //while loop that runs until it reaches the end of the string 


     if ((guy[i]) >= 'A' && (guy[i] <= 'Z')){ //moves capital letter values up 1 
      if (guy[i] == 'Z'){ 
       guy[i] = guy[i] - 25; 
      } 
      else { 
       guy[i] = guy[i] + 1; //makes the current "slot" number go up 1 value. Example: a = 97 + 1 -> b = 98 
      } 
     } 
     if ((guy[i]) >= 'a' && (guy[i]) <= 'z'){// moves lower case letter values up 1 
      if (guy[i] == 'z'){ 
       guy[i] = guy[i] - 25; 
      } 
      else{ 
       guy[i] = guy[i] + 1; 
      } 
     } 
     i++; //moves the array's interval up to the next "slot" 

    } 
    printf("Encrypted text is: %s\n", guy); 

    //Vigenere Cipher-- keyword is "apple" 
    //a = 1 value shift 
    //p = 16 value shift 
    //p = 16 value shift 
    //l = 17 value shift 
    //e = 5 value shift 
    printf("The value of i = %d\n", &i); 
    i = 0; 
    printf("Enter the plain text: "); 
    fgets(guy, 100, stdin);//takes user's input 

    while (guy[i] != '\0'){ //while loop that runs until it reaches the end of the string 

     if (i % 5 == 0 || i == 0){ //checks to see which character it is in the string, for instance the numbers 0,5,10,15,20 should all be added by 1 
      guy[i] = guy[i] + 1; 
     } 

     if ((i - 1) % 5 == 0 || i == 1){ //all numbers that are second in the key word 'apple', such as 1,6,11,16 
      guy[i] = guy[i] + 16; 
     } 
     if ((i - 2) % 5 == 0 || i == 2){// all numbers that are third to the key word 'apple' , such as 2,7,12,17,22 
      guy[i] = guy[i] + 16; 
     } 
     if ((i - 3) % 5 == 0 || i == 3){// all numbers that are fourth to the key word 'apple', such as 3,8,13,18 
      guy[i] = guy[i] + 17; 
     } 
     if ((i - 4) % 5 == 0 || i == 4){// all numbers that are fifth in the key word 'apple', such as 4,9,14,19 
      guy[i] = guy[i] + 5; 
     } 
     else { 
      i++; 
     } 
    } 
    printf("Encrypted text is: %s\n", guy); 
    getchar(); 
} 

出力: -

は、プレーンテキストを入力します:リンゴ 暗号化されたテキストは、次のとおりです。

bqqmf

i = 1900200の値 - 再初期化前のiの値。

は、プレーンテキストを入力します:リンゴ 暗号化されたテキストは、次のとおりです。BCC}

+0

しかし、それはzを超えてはならない。 –