解密算法C ++

时间:2015-06-26 15:46:25

标签: c++ encryption

所以我想创建一个加密/解密程序。我通过让用户输入种子进行加密,然后使用提供的种子使用srand()为随机生成器播种。然后,我根据种子创建一个数字列表,并按顺序使用这些数字在每个隐含字符上应用基本的ceasar密码。 encrypt()函数工作正常,但是当我尝试解密时,我没有得到相同的文本,这是代码:

#include <iostream>
#include <string>
#include <random>
#include <vector>
using namespace std;

int encrypt();
int decrypt();

int main() {
    int choice;
    bool quit = false;

    while (quit == false) {
        cout << "(1 Encrypt" << endl;
        cout << "(2 Decrypt" << endl;
        cout << "(3 Quit" << endl;

        cin >> choice;

        switch (choice) {
        case 1:
            encrypt();
            break;
        case 2:
            decrypt();
            continue;
        case 3:
            quit = true;
            continue;
        default:
            cout << "Thats not a choice." << endl;
            continue;
        }
    }
    return 0;
}

int encrypt() {
//encryption method OK
    string input;
    int seed;
    char alphabeta[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' };
    cout << "Enter the string you would like to encrypt in all lowercase: " << endl;
    cin >> input;
    cout << input << endl;
    //cout << endl;

    vector <int> randList;
    cout << input << endl;
    cout << "Enter your encryption seed: " << endl;
    cin >> seed;

    srand(seed);
    string outputStr;
    for (int i = 0; i < input.length(); i++) {
        int tempRand = rand() % 26;
        randList.insert(randList.end(), tempRand);
        cout << tempRand << endl;
    }
    int count = 0;
    for (int i = 0; i < input.length(); i++) {
        for (int j = 0; j < 26; j++) {
            if (input[i] == alphabeta[j]) {
                int j1 = j;
                if (j + randList[count] > 25) {
                    j1 = 0;
                }
                outputStr += alphabeta[j1 + randList[count]]; //+ randList[count]];
                count++;
            }
        }
    }
    cout << "Here is your encrypted string :" << endl;
    cout << outputStr;
    cout << endl;
    return 0;
}

int decrypt() {
//decryption method
    string input;
    int seed;
    char alphabeta[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' };
    cout << "Enter the string you would like to decrypt in all lowercase: " << endl;
    cin >> input;
    cout << input << endl;
    //cout << endl;

    vector <int> randList;
    //cout << input << endl;
    cout << "Enter your encryption seed: " << endl;
    cin >> seed;

    srand(seed);
    string outputStr;
    for (int i = 0; i < input.length(); i++) {
        int tempRand = rand() % 26;
        randList.insert(randList.end(), tempRand);
        //cout << tempRand << endl;
    }
    int count = 0;
    for (int i = 0; i < input.length(); i++) {
        for (int j = 0; j < 26; j++) {
            if (input[i] == alphabeta[j]) {
                int j1 = j;
                //if (j - randList[count] <= 0) {
                    //j1 = 25;
                //}

                outputStr += alphabeta[j1 - randList[count]];
                if (j - randList[count] <= 0) {
                    j1 = 25;
                }
                cout << j1 - randList[count]<< endl;
                count++;

            }
        }
    }
    cout << "Here is your decrypted string :" << endl;
    cout << outputStr << endl;
    return 0;
}

这是我的解密方法:

int decrypt() {
    //decryption method
        string input;
        int seed;
        char alphabeta[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' };
        cout << "Enter the string you would like to decrypt in all lowercase: " << endl;
        cin >> input;
        cout << input << endl;
        //cout << endl;

        vector <int> randList;
        //cout << input << endl;
        cout << "Enter your encryption seed: " << endl;
        cin >> seed;

        srand(seed);
        string outputStr;
        for (int i = 0; i < input.length(); i++) {
            int tempRand = rand() % 26;
            randList.insert(randList.end(), tempRand);
            //cout << tempRand << endl;
        }
        int count = 0;
        for (int i = 0; i < input.length(); i++) {
            for (int j = 0; j < 26; j++) {
                if (input[i] == alphabeta[j]) {
                    int j1 = j;
                    //if (j - randList[count] <= 0) {
                        //j1 = 25;
                    //}

                    outputStr += alphabeta[j1 - randList[count]];
                    if (j - randList[count] <= 0) {
                        j1 = 25;
                    }
                    cout << j1 - randList[count]<< endl;
                    count++;

                }
            }
        }
        cout << "Here is your decrypted string :" << endl;
        cout << outputStr << endl;
        return 0;
    }

为什么解密后的结果与加密前的起始字符串不匹配?

1 个答案:

答案 0 :(得分:0)

您正在获取可重复的随机数组,添加它们并稍后减去它们,例如,如果输入为7(将字符转换为索引后)并且随机数为10,则加密:

7 + 10 = 17

以后,解密:

17 - 10 = 7

但是在你的逻辑中,如果加密加起来大于25,你就会抛弃输入。例如,如果输入为17:

17 + 10 = 27

但是,27&gt; 25,所以......

0 + 10 = 10

你永远失去了输入。它将始终“解密”为0。

10 - 10 = 0

在解密输出中你会得到很多'a',一个用于你丢失的每一个输入。找另一种算法。