0

我一直在研究个人序列化过程,但在保存/加载后我一直无法取回解密的向量。它可以很好地保存和加载数据,但是向量清单在转换中失去了它的定义,所以我必须重新制作它,这消除了稍后在程序中使用它的能力。下面的代码片段显示了我如何加密/解密。

struct bin{
    map<string, int>::iterator it;
    int modify_string;
    vector<string> uncrypt_vec;
    map<string, int> encryption;
    void set_up_code(){
        string alphabet[] = {"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"};
        int letter_num=1;
        for(int i = 0; i < 27; ++i){
            string letter = alphabet[i]; //Assign each letter to a number
            encryption.insert(pair<string,int>(letter,letter_num));
            letter_num++;
        }
        encryption.insert(pair<string,int>(" ", -1));
    }
    int encrypt_vector(vector<string>& crypt){
        stringstream ssv;
        for(unsigned int i = 0; i < crypt.size(); ++i){
            string word = crypt[i]; //Grab each string
            int mod_word[word.size()];
            for(unsigned int x = 0; x < word.size(); ++x){
                stringstream ssl;
                string letter;
                char let = word[x]; //Convert each letter of the string into an integer
                ssl << let;
                ssl >> letter;
                int num = encryption[letter];
                mod_word[x] = num;
            }
            mod_word[word.size()] = 0; //Include a zero for each word
            copy(mod_word, mod_word+word.size()+1, ostream_iterator<int>(ssv));
        }
        ssv >> modify_string; //Put into int modify_string and return
        return modify_string;
    }
    vector<string> decrypt_vector(int& uncrypt){
        string num;
        stringstream ssn;
        ssn << uncrypt; //Store the numbers in a string
        ssn >> num;
        string mod_word;
        string word;
        for(unsigned int i=0;i<num.size();++i){
            int number;
            char check_num = num[i];
            stringstream ssctn;
            ssctn << check_num; //Kinda stupid way of doing it
            ssctn >> number; //Pulls int from num[i], then checks it against 0
            if(number==0){ //If break point
                uncrypt_vec.push_back(mod_word); //Push back the word
                mod_word.clear();
            }
            int letter;
            stringstream sscn;
            char let = num[i];
            sscn << let; //Pull each letter from uncrypt, change back to letter based on encryption
            sscn >> letter;
            for(it = encryption.begin(); it != encryption.end(); ++it){
                if(it->second == letter){
                    mod_word.append(it->first);
                    break;
                }
            }
        }
        cout << "Done" << endl;
        return uncrypt_vec;
    }
}binary;

struct player{
    vector<string> inventory;
    int inventory_en; //en for encrypted
    void save(){
        inventory_en = binary.encrypt_vector(inventory);
    }

    void load(){
        vector<string> inventory; //Must recreate, type is lost otherwise, crashes
        inventory = binary.decrypt_vector(inventory_en);
        vector<string>::iterator i;
        for(i=inventory.begin();i!=inventory.end();++i){
            cout << *i << endl; //Prints correctly here
        }
    }
}character;

int main(){
    character.inventory.push_back("abc");
    character.inventory.push_back("edf");
    binary.set_up_code();
    character.save();
    character.load();
}

当我稍后尝试打印 character.inventory 时,它崩溃了。如果我没有在 character.load() 函数中将 character.inventory 定义为字符串,它也会崩溃。那么如何将 character.inventory 定义为稍后在程序中可以调用(从结构)的向量?

我使用相同的方式将数据写入文件output_file.write((char*)&structObj, sizeof(structObj));并读出(因此需要加密/解密,不能以这种方式保存字符串)。

请不要让我去图书馆为我做这件事。

4

1 回答 1

4

您正在索引字母 [26],但最后一个有效索引是 25。

    for(int i = 0; i < 27; ++i){
        string letter = alphabet[i];
于 2012-08-02T04:38:49.357 回答