C编程-Ceaser密码

时间:2019-03-13 15:18:43

标签: c vigenere

我正在尝试回答问题 ”,请执行Vigenère密码。该程序必须使用标准的gcc编译器进行编译。您的程序应为用户提供加密或解密消息的选项。应提示用户输入要输入的密码和关键字be在密码中使用”

# include <stdio.h>

main()
{
  int select; // variable declairation
  int i, j;
  char passphrase[256];
  char keyword[33];
  int value;

  while (1) // infinite loop
  {
    printf("n1.Encrypt n"); // Display options for the user
    printf("2. Decryptn"); // Encrypt or Decrypt
    scanf(" % d", & select); // read the option selected

    if (select == 1)
    {
      printf("Please Enter Message to be encrypted"); //text to be encrypted
      scanf(" % s", & passphrase);

      printf("Please Enter keyword"); // key word
      scanf(" % s", & keyword);

      for (i = 0, j = 0; i)
        {
          if (j >= strlen(keyword)) // repeat the key word
          {
            j = 0;
          }
          value = (((passphrase[i]) - 97) + ((keyword[j]) - 97)); //logic (passphrase+key)%26

          printf(" % c", 97 + (value % 26)); // display Encrypted text
        }
      }
      else if (select == 2)
      {
        printf("Please Enter Encrypted Message to be Decrypted"); //text to be decrypted
        scanf(" % s", & passphrase);

        printf("Please Enter key"); //key
        scanf(" % s", & keyword);

        for (i = 0, j = 0; i)
          {
            if (j >= strlen(keyword))
            {
              j = 0; // repeate the key
            }
            value = ((passphrase[i]) - 96) - (keyword[j] - 96); //logic (passphrase-key)%26
            if (value < 0)
            {
              value = value * -1; //make the value positive
            }
            printf(" % c", 97 + (value % 26)); // display dencrypted message
          }
        }
        else
          printf("Please Choose a correct option");
      }
    }

错误列表如下:

Executing  gcc.exe...
gcc.exe "..c" -o "..exe"    -I"D:\Dev-Cpp\include"   -L"D:\Dev-Cpp\lib" 
..c: In function `main':
..c:38: error: syntax error before ')' token

..c: At top level:
 error: syntax error before "else"
.error: syntax error before string constant
.error: conflicting types for 'scanf'
.note: a parameter list with an ellipsis can't match an empty parameter name list declaration
.error: conflicting types for 'scanf'
.note: a parameter list with an ellipsis can't match an empty parameter name list declaration
.warning: data definition has no type or storage class
.error: syntax error before string constant
.error: conflicting types for 'printf'
.note: a parameter list with an ellipsis can't match an empty parameter name list declaration
.error: conflicting types for 'printf'
.note: a parameter list with an ellipsis can't match an empty parameter name list declaration
.warning: data definition has no type or storage class
error: syntax error before string constant

 warning: data definition has no type or storage class
 error: `passphrase' undeclared here (not in a function)
 error: `i' undeclared here (not in a function)
error: `keyword' undeclared here (not in a function)
 error: `j' undeclared here (not in a function)
 warning: data definition has no type or storage class
 error: syntax error before "if"
 error: syntax error before string constant

有人可以帮忙吗 谢谢你。

引用:https://www.ukessays.com/essays/information-technology/vigenre-cipher-program-7373.php

1 个答案:

答案 0 :(得分:0)

您遇到了编译器错误,因为for(i=0, j=0; i) is not the proper for loop syntax

首先,解决此问题,然后您的代码可能编译。然后修正您的警告。然后,您将能够运行和调试代码。

我还建议使用using the proper signature for main

这里还有其他与密码问题无关的问题,例如使用scanf读取字符串而不指定最大字符数。