简单密码学 - 循环问题

时间:2016-11-22 05:36:47

标签: c++ encryption

我正在尝试创建一个简单的加密程序,允许用户输入一个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

查看代码和注释也会有所帮助。我的问题是,应该实际加密消息的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;
}

1 个答案:

答案 0 :(得分:-2)

下面是用C ++编写的代码,它完全符合您的要求。如果有什么不起作用,你可以在评论中告诉我。

我使用 find()函数以最小化代码长度并提高可读性。我主要修改了加密消息的 for()循环(如你所提到的那样)。

编辑 - 加密时不要寻找空格。除了字母之外的任何其他内容都应该打印在加密消息中。我已使用 return(-1)执行相同的操作,并在加密循环中的 if condition 中进行了检查。

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;
}