凯撒密码/解密程序。 C ++

时间:2014-10-02 15:54:26

标签: c++ algorithm encryption

我正在开展一个学校项目,我的项目是Cypher和Decypher程序,与Caesar算法一起使用。

我的程序必须具有以下特征:

  • 程序必须为用户提供再次启动的选项。 (完成。)
  • 如果用户输入错误,程序必须要求用户再次输入。 (做错了。)
  • 用户只能输入字母和 - 空格。 (我只需要添加太空物。)
  • 信件必须移动5个位置(即A = F)(完成。)
  • 该计划必须考虑大写字母。 (完成。)
  • 每次输入字母时,屏幕必须清除。 (完成。)
  • 在程序结束时,您必须能够看到加密或解密的文本。 (需要完成。)

我需要知道的是如何让程序向我显示加密文本,计算我在程序运行时到目前为止所做的所有输入。

还有一件事是我如何输入整个文本句子并应用Caesar Encryption,显然我必须更改几乎所有代码但我想要怎么做。

这是我到目前为止的代码,对不起,如果句子是西班牙语,但我现在住在墨西哥,我的课程是西班牙语。如果你需要我翻译文本,我很乐意为你做。

#include <iostream>
#include <stdlib.h>
#include <time.h>

using namespace std;

int main(int, char**) {
    // Variables.
    char l;
    int x = 0;
    char y = x + l;
    bool volverainiciar;
    volverainiciar = 1;

    while (volverainiciar == 1) {
        cout << "Favor de introducir la letra del mensaje que desea codificar."
             << endl;
        cin >> l;
        cout << " " << endl;
        cout << " " << endl;

        if ((l >= 'a' && l <= 'u') || (l >= 'A' && l <= 'U')) {
            cout << "El mensaje a codificar es: " << l;
            cout << " " << endl;
            cout << " " << endl;
            cout << "La letra " << l << " tiene un codigo ASCII de: " << x + l
                 << endl;

            y = (l + 5);

            cout << "La letra " << l << " encriptado tiene un valor de: " << y
                 << endl;

            cout << " " << endl;
            cout << " " << endl;
            cout << "Se limpiara la pantalla..." << endl;
            system("pause");
            system("cls");

            cout << "           Desea volver a iniciar?" << endl;
            cout << " " << endl;
            cout << "       Para volver a iniciar (1) , para no volver a "
                    "iniciar (0)" << endl;
            cout << " " << endl;
            cout << "Volver a iniciar: ";
            cin >> volverainiciar;
            cout << " " << endl;
            cout << " " << endl;
        } else if ((l >= 'v' && l <= 'z') || (l >= 'V' && l <= 'Z')) {
            switch (l) {
                case 'v':
                    cout << "El mensaje a codificar es: " << l;
                    cout << " " << endl;
                    cout << " " << endl;
                    cout << "La letra u tiene un codigo ASCII de: " << x + l
                         << endl;
                    cout << "La letra " << l
                         << " encriptado tiene un valor de: a" << endl;
                    cout << " " << endl;
                    cout << " " << endl;
                    cout << "Se limpiara la pantalla..." << endl;
                    system("pause");
                    system("cls");

                    cout << "           Desea volver a iniciar?" << endl;
                    cout << " " << endl;
                    cout << "       Para volver a iniciar (1) , para no volver "
                            "a iniciar (0)" << endl;
                    cout << " " << endl;
                    cout << "Volver a iniciar: ";
                    cin >> volverainiciar;
                    cout << " " << endl;
                    cout << " " << endl;
                    break;

                case 'w':
                    cout << "El mensaje a codificar es: " << l;
                    cout << " " << endl;
                    cout << " " << endl;
                    cout << "La letra u tiene un codigo ASCII de: " << x + l
                         << endl;
                    cout << "La letra " << l
                         << " encriptado tiene un valor de: b" << endl;
                    cout << " " << endl;
                    cout << " " << endl;
                    cout << "Se limpiara la pantalla..." << endl;
                    system("pause");
                    system("cls");

                    cout << "           Desea volver a iniciar?" << endl;
                    cout << " " << endl;
                    cout << "       Para volver a iniciar (1) , para no volver "
                            "a iniciar (0)" << endl;
                    cout << " " << endl;
                    cout << "Volver a iniciar: ";
                    cin >> volverainiciar;
                    cout << " " << endl;
                    cout << " " << endl;
                    break;

                case 'x':
                    cout << "El mensaje a codificar es: " << l;
                    cout << " " << endl;
                    cout << " " << endl;
                    cout << "La letra u tiene un codigo ASCII de: " << x + l
                         << endl;
                    cout << "La letra " << l
                         << " encriptado tiene un valor de: c" << endl;
                    cout << " " << endl;
                    cout << " " << endl;
                    cout << "Se limpiara la pantalla..." << endl;
                    system("pause");
                    system("cls");

                    cout << "           Desea volver a iniciar?" << endl;
                    cout << " " << endl;
                    cout << "       Para volver a iniciar (1) , para no volver "
                            "a iniciar (0)" << endl;
                    cout << " " << endl;
                    cout << "Volver a iniciar: ";
                    cin >> volverainiciar;
                    cout << " " << endl;
                    cout << " " << endl;
                    break;

                case 'y':
                    cout << "El mensaje a codificar es: " << l;
                    cout << " " << endl;
                    cout << " " << endl;
                    cout << "La letra u tiene un codigo ASCII de: " << x + l
                         << endl;
                    cout << "La letra " << l
                         << " encriptado tiene un valor de: d" << endl;
                    cout << " " << endl;
                    cout << " " << endl;
                    cout << "Se limpiara la pantalla..." << endl;
                    system("pause");
                    system("cls");

                    cout << "           Desea volver a iniciar?" << endl;
                    cout << " " << endl;
                    cout << "       Para volver a iniciar (1) , para no volver "
                            "a iniciar (0)" << endl;
                    cout << " " << endl;
                    cout << "Volver a iniciar: ";
                    cin >> volverainiciar;
                    cout << " " << endl;
                    cout << " " << endl;
                    break;

                case 'z':
                    cout << "El mensaje a codificar es: " << l;
                    cout << " " << endl;
                    cout << " " << endl;
                    cout << "La letra u tiene un codigo ASCII de: " << x + l
                         << endl;
                    cout << "La letra " << l
                         << " encriptado tiene un valor de: e" << endl;
                    cout << " " << endl;
                    cout << " " << endl;
                    cout << "Se limpiara la pantalla..." << endl;
                    system("pause");
                    system("cls");

                    cout << "           Desea volver a iniciar?" << endl;
                    cout << " " << endl;
                    cout << "       Para volver a iniciar (1) , para no volver "
                            "a iniciar (0)" << endl;
                    cout << " " << endl;
                    cout << "Volver a iniciar: ";
                    cin >> volverainiciar;
                    cout << " " << endl;
                    cout << " " << endl;
                    break;

                case 'V':
                    cout << "El mensaje a codificar es: " << l;
                    cout << " " << endl;
                    cout << " " << endl;
                    cout << "La letra u tiene un codigo ASCII de: " << x + l
                         << endl;
                    cout << "La letra " << l
                         << " encriptado tiene un valor de: A" << endl;
                    cout << " " << endl;
                    cout << " " << endl;
                    cout << "Se limpiara la pantalla..." << endl;
                    system("pause");
                    system("cls");

                    cout << "           Desea volver a iniciar?" << endl;
                    cout << " " << endl;
                    cout << "       Para volver a iniciar (1) , para no volver "
                            "a iniciar (0)" << endl;
                    cout << " " << endl;
                    cout << "Volver a iniciar: ";
                    cin >> volverainiciar;
                    cout << " " << endl;
                    cout << " " << endl;
                    break;

                case 'W':
                    cout << "El mensaje a codificar es: " << l;
                    cout << " " << endl;
                    cout << " " << endl;
                    cout << "La letra u tiene un codigo ASCII de: " << x + l
                         << endl;
                    cout << "La letra " << l
                         << " encriptado tiene un valor de: B" << endl;
                    cout << " " << endl;
                    cout << " " << endl;
                    cout << "Se limpiara la pantalla..." << endl;
                    system("pause");
                    system("cls");

                    cout << "           Desea volver a iniciar?" << endl;
                    cout << " " << endl;
                    cout << "       Para volver a iniciar (1) , para no volver "
                            "a iniciar (0)" << endl;
                    cout << " " << endl;
                    cout << "Volver a iniciar: ";
                    cin >> volverainiciar;
                    cout << " " << endl;
                    cout << " " << endl;
                    break;

                case 'X':
                    cout << "El mensaje a codificar es: " << l;
                    cout << " " << endl;
                    cout << " " << endl;
                    cout << "La letra u tiene un codigo ASCII de: " << x + l
                         << endl;
                    cout << "La letra " << l
                         << " encriptado tiene un valor de: C" << endl;
                    cout << " " << endl;
                    cout << " " << endl;
                    cout << "Se limpiara la pantalla..." << endl;
                    system("pause");
                    system("cls");

                    cout << "           Desea volver a iniciar?" << endl;
                    cout << " " << endl;
                    cout << "       Para volver a iniciar (1) , para no volver "
                            "a iniciar (0)" << endl;
                    cout << " " << endl;
                    cout << "Volver a iniciar: ";
                    cin >> volverainiciar;
                    cout << " " << endl;
                    cout << " " << endl;
                    break;

                case 'Y':
                    cout << "El mensaje a codificar es: " << l;
                    cout << " " << endl;
                    cout << " " << endl;
                    cout << "La letra u tiene un codigo ASCII de: " << x + l
                         << endl;
                    cout << "La letra " << l
                         << " encriptado tiene un valor de: D" << endl;
                    cout << " " << endl;
                    cout << " " << endl;
                    cout << "Se limpiara la pantalla..." << endl;
                    system("pause");
                    system("cls");

                    cout << "           Desea volver a iniciar?" << endl;
                    cout << " " << endl;
                    cout << "       Para volver a iniciar (1) , para no volver "
                            "a iniciar (0)" << endl;
                    cout << " " << endl;
                    cout << "Volver a iniciar: ";
                    cin >> volverainiciar;
                    cout << " " << endl;
                    cout << " " << endl;
                    break;

                case 'Z':
                    cout << "El mensaje a codificar es: " << l;
                    cout << " " << endl;
                    cout << " " << endl;
                    cout << "La letra u tiene un codigo ASCII de: " << x + l
                         << endl;
                    cout << "La letra " << l
                         << " encriptado tiene un valor de: E" << endl;
                    cout << " " << endl;
                    cout << " " << endl;
                    cout << "Se limpiara la pantalla..." << endl;
                    system("pause");
                    system("cls");

                    cout << "           Desea volver a iniciar?" << endl;
                    cout << " " << endl;
                    cout << "       Para volver a iniciar (1) , para no volver "
                            "a iniciar (0)" << endl;
                    cout << " " << endl;
                    cout << "Volver a iniciar: ";
                    cin >> volverainiciar;
                    cout << " " << endl;
                    cout << " " << endl;
                    break;
            }
        } else {
            cout << "Ese simbolo no es valido, favor de introducir uno valido."
                 << endl;
            cin >> l;
            cout << " " << endl;
            cout << " " << endl;
            continue;
        }
    }

    cout << "Gracias por usar el encriptador/desencriptador." << endl;
    exit;

    return 0;
}

如果您有任何建议,我也很乐意阅读它们:)

对不起任何拼写错误我的英语并不完美,因为几乎我一生都住在这里......

1 个答案:

答案 0 :(得分:1)

我讨厌废弃你真正漫长的节目,但这很烦人。

试试这个:

// Given the input character is in x.
if (std::isalpha(x))
{
  if (std::islower(x))
  {
    y = (x - 'a'); // Convert to a number.
    y = y + 5;     // Left shift by 5
    y = y % 26;    // Modulo arithmetic for all letters in alphabet
    y = y + 'a';   // Convert back to character.
  }
  else
  {
    y = (x - 'A'); // Convert to a number.
    y = y + 5;     // Left shift by 5
    y = y % 26;    // Modulo arithmetic for all letters in alphabet
    y = y + 'A';   // Convert back to character.
  }
}

我的一个烦恼是重复的代码。虽然上面有重复的代码,但我减少了代码中的重复数量。您可以在上面的代码中添加所有cout语句。

顺便说一句,如果数据不使用变量,您可以将数据“阻止写入”cout。 示例:

  static const char answer_text[] =
  "\n"
  "\n"
  "La letra u tiene un codigo ASCII de: ";
  //...
  cout.write(answer_text, sizeof(answer_text) - 1);

这将允许您在需要的任何地方使用cout.write语句来使用相同的文本。打字少,行数少,注入缺陷的可能性更小。