【问题标题】:Simple Cryptography - Problematic For Loops简单密码学 - 有问题的 For 循环
【发布时间】:2016-11-22 15:18:40
【问题描述】:

我正在尝试创建一个简单的加密程序,该程序允许用户输入 5 个字母的单词作为密钥。然后,该程序使用该密钥的每个字符来加密他们输入的用户消息的字母。这有点难以解释,但举个例子会让它更清楚。

所以,假设您希望关键字是“柠檬”。 'l' 是字母表中的第 12 个字母,但我们从 0 开始计数,所以 'l' 是我们称之为字母表数组的第 11 个字母。'l' = 11, 'e' would = 4, 'm' = 12, 'o' = 14, 'n' = 13。因此,如果您想加密“黎明攻击”消息,每个字符都会有自己对应的密钥中的字母。然后它将与密钥字母相对应的数字添加到消息字母中,然后您就对其进行了加密。下面的例子。

                  plain text:       attack at dawn
                  key:              lemonl em onle
                  encrypted:        lxfopv ef rnhr

查看代码和 cmets 也会有所帮助。我的问题是应该实际加密消息的 for 循环无法正常工作。我看不出代码有任何问题,但输出不正确,所以可能是逻辑错误?

#include <iostream>
#include <string>

using namespace std;

int main() {
    string key;
    int keySize;
    char keyChar[5];
    int keyInteger[5];
    bool keyLoop = true;
    //makes sure key is 5 characters longs.
    while (keyLoop) {
        cout << "enter in the 5 letter key: ";
        cin >> key;
        keySize = key.size();
        if (keySize < 5 || keySize > 5) {
            cout << "Invalid key." << endl;
    }
    else {
        //obtains each individual character and places it into the array keyChar.
        for (int i = 0; i < 5; i++) {
            keyChar[i] = key[i];
        }
        keyLoop = false;
    }
}

char alphabet[52]{ 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
    'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' };
//finds the place of the element in keyChar and stores it as an int in the array keyInteger. This is setting up the key for encryption.
for (int x = 0; x < 5; x++) {
    for (int i = 0; i < 26; i++) {
        if (keyChar[x] == alphabet[i]) {
            keyInteger[x] = i;
        }
    }
}
//This just displays the values of keyInt. This code was included to make sure the program was storing the right values.
for (int i = 0; i < 5; i++) {
    cout << keyInteger[i] << endl;
}

string secretMessage;

cin.ignore();
//user input their secret message
cout << "Enter your secret message: ";
getline(cin, secretMessage);
//gets size of message and creates variable to modify when we're ready to encrypt the message. 
int secretMessageSize = secretMessage.size();
cout << secretMessageSize << endl;
string newMessage = secretMessage;
int temp;

// This first loop goes through each character of the message. 
for (static int y = 0; y < secretMessageSize; y++) {
    // This loop keeps track of which keyInteger will be used for the encryption formula. 
    for (int a = 0; a < 5; a++) {
        // The if statement makes sure that the keyInteger isn't used until there is actually a character to be modified.
        if (secretMessage[y] == ' ') {
            //Just outputting the new message right now to make sure the program is running.
            cout << " ";
            a--;
        }
        else {
            //this loop goes runs until the character of secretMessage[y] is the same as the character at alphabet[i].
            //then it assigns temp to the value of i. This will be used in the encryption formula.
            for (int i = 0; i < 26; i++) {
                if (secretMessage[y] == alphabet[i]) {
                    temp = i;
                    break;
                }
                //this is all for testing purposes. LETTER should have the same letter in a row.
                cout << "LETTER " << secretMessage[y] << "           " << alphabet[i] << endl;
                //number should have the what number character the loop is on of the secretMessage. The number across should be the where that character was found in the alphabet array.
                cout << "NUMBER " << y << "           " << i << endl;
                //Key should only have one integer next to it. This should be whichever instance the loop that has a in it is at.
                cout << "KEY " << keyInteger[a] << endl;
                //This should give the actual character that should be replacing the character in secretMessage at whatever iteration it is at.
                //The encryption formula takes the integer that is used to address the character in the alphabet array. So if it were an 'a', then that integer would == 0;
                //Then it adds the keyInteger at whatever iteration the loop is at. If the keyword were lemon, and it was in its first iteration, a would = 0, and keyInteger[0] would equal 11.
                //The sum of these two values will give you the new character from the alphabet array. So alphabet[0+11] would = l. <-- this is a lower case L.
                cout << "CRYPT " << alphabet[keyInteger[a] + i] << endl;

            }
        }
        //this adds to y so that it keeps track of each character in the message, but we are also able to keep track of which keyInteger to use. 
        y++;
    }
}
return 0;
}

【问题讨论】:

  • 请定义“无法正常工作”。您应该添加一个您在问题中得到的输出示例。

标签: c++ encryption


【解决方案1】:

下面是用 C++ 编写的代码,完全符合您的要求。如果有任何问题,您可以在 cmets 中告诉我。

我使用了 find() 函数来最小化代码长度并提高可读性。我主要修改了加密消息的 for() 循环(您也提到过)。

编辑 - 不要只在加密时寻找空格。除字母表之外的任何内容都应按照加密消息中的内容打印。我已经使用 return(-1) 来做同样的事情,并在 加密循环if 条件 中检查它。

char alphabet[26]{ 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};

#include <iostream>
#include <string>

using namespace std;

int find(char character)
{
    for (int i = 0; i < 26; i++) 
    {
        if (character == alphabet[i]) 
        {
            return (i);
        }
    }
    return (-1);
}

int main() 
{
    string key;
    int keySize;
    char keyChar[5];
    bool keyLoop = true;
    int keyInteger[5];

    while (keyLoop) 
    {
        cout << "enter in the 5 letter key: ";
        cin >> key;
        keySize = key.size();
        if (keySize < 5 || keySize > 5) 
        {
            cout << "Invalid key." << endl;
        }
        else 
        {
            for (int i = 0; i < 5; i++)    
            {
                keyChar[i] = key[i];
            }
            keyLoop = false;
        }
    }

    for (int x = 0; x < 5; x++) 
    {
        keyInteger[x] = find(keyChar[x]);
    }

    string secretMessage;
    cin.ignore();
    cout << "Enter your secret message: ";
    getline(cin, secretMessage);
    int secretMessageSize = secretMessage.size();
    string newMessage = secretMessage;
    int temp;

    int x = 0;
    for (int i = 0; i < secretMessageSize; i++)
    {
        if (x == 5)
            x = 0;

        if (find(secretMessage[i]) == -1)
            newMessage[i] = secretMessage[i];
        else
        {
            temp = (find(secretMessage[i]) + keyInteger[x])%26;
            newMessage[i] = alphabet[temp];
            x++;
        }
    }

    for (int i = 0; i < secretMessageSize; i++)
    {
        cout << newMessage[i];
    }
    cout << endl;
}

【讨论】:

    猜你喜欢
    • 2022-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-22
    • 2023-03-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多