-1

部分的に暗号化された文字列をファイルに保存しようとしています。私は何時間も立ち往生しており、間違いがどこにあるのか理解できません。関数は次のとおりです。

void cryptData(string & rawTxt, string & path) {

    // the rawTxt is OK. Path is too.

    int c(0), counta(0);
    while( rawTxt[c] != '\0') { // Reading every char from the string.

        if(c <= 122) {
            // First 123 char won't be crypted. 
            // Here I do nothing to rawTxt
        }
        else { // All remaining chars will be crypted

            rawTxt[c] = cryptByte(counta, rawTxt[c]); // Replace current char at position c
                                                      // by a crypted char.
            counta++;

            if(counta > 36) // counta used for crypting,
                counta = 0;
        }
        c++;
    } 

    ofstream toDat(path.c_str() ); // Save recrypted .dat file
    toDat << rawTxt.c_str();  // HERE the first 123 chars are not copied in my file... 
}


// Crypt chars
char cryptByte(int counta, char b) {
    string plainEncryptionKey("odBearBecauseHeIsVeryGoodSiuHungIsAGo");
    b+= (char) plainEncryptionKey[counta];
    return b;
}

最初の 123 文字がファイルに保存されない理由がわかりません。rawTxt 文字列は、プログラムの最初から最後まで同じ長さです。ください私は夢中になります !

編集:申し訳ありませんが、私の文字列 rawTxt は復号化されたファイルの結果であり、BEGINNING に 123 個のランダムな文字が含まれています。したがって、これらのランダムな文字は、復号化アルゴリズムで消去されます。私は、これらの 123 文字をあるべき場所に戻さずに、愚かにも暗号化アルゴリズムを作成しました。私のコードが別の文字列でうまく機能することに気付いたとき、ヒントを得ました。私の悪い。ご協力いただきありがとうございます!

4

3 に答える 3

0

を使用して多くを簡素化できますC++11

void cryptData(string & rawTxt, string & path) {
    string plainEncryptionKey("odBearBecauseHeIsVeryGoodSiuHungIsAGo");
    int count = 0;
    for (char & c : rawTxt)
        if (c > 122) {
            c = plainEncryptionKey[count];
            count = (count + 1) % 37;
        }
    ofstream toDat(path.c_str());
    toDat << rawTxt;
}
于 2015-07-07T08:51:14.300 に答える
0

コンパイルしようとしましたが、-Wall と gcc コンパイラで次のエラーが発生しました。

'std::ofstream toDat' には初期化子がありますが、型が不完全です`

fstreamライブラリを含めることで、なんとか機能させることができました。それが役立つことを願っています。std::coutで出力すると、実際に文字が出力されるためです。

于 2013-08-27T15:34:38.700 に答える