Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ssis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 简单加密-循环有问题_C++_Encryption - Fatal编程技术网

C++ 简单加密-循环有问题

C++ 简单加密-循环有问题,c++,encryption,C++,Encryption,我正在尝试创建一个简单的加密程序,允许用户输入一个5个字母的单词作为密钥。然后,程序使用该密钥的每个字符对用户输入的消息的字母进行加密。这有点难解释,但举个例子可以让它更清楚一点 所以,假设你希望你的关键词是柠檬l'是字母表中的第12个字母,但我们从0开始计数,所以l'是我们称之为字母表数组的第11个字母。'l'=11,'e'将=4,'m'=12,'o'=14,'n'=13。因此,如果您想在拂晓时对消息攻击进行加密,则每个字符都有其对应密钥中的字母。然后,它将密钥字母对应的数字添加到消息字母中,

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

所以,假设你希望你的关键词是柠檬l'是字母表中的第12个字母,但我们从0开始计数,所以l'是我们称之为字母表数组的第11个字母。'l'=11,'e'将=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;
}

下面是C++编写的代码,它可以精确地执行你正在寻找的代码。如果有什么不起作用,你可以在评论中告诉我

我使用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;
}