-1

ファイルを暗号化してから復号化しようとしています。ファイルの復号化を試みるとき、コンテンツを画面に表示して、復号化のプロセスが問題なく行われることを確認したいと考えています。しかし、ファイルの復号化の表示はありません。コードに何が欠けているのかわかりません。Dev_C++ を使用しています。あなたの助けは非常に高く評価されます。コードは以下です。

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>



using namespace std;


int main()
{ 
    string line;

    string file, encrfile;
    int i, key_length, longueur;
    unsigned int key=0;
    char ch[100];

    cout<<"enter a secret key: ";
    cin.getline(ch, 100);

    for (i=0;ch[i];i++)
    key=(key+3)*ch[i];

    cout<<"Password generated: "<<key;

    cout<<"\n\nEnter the name of the input file: ";
    getline(cin,file);

    cout<<"\nEnter the name of the output file: ";
    getline(cin,encrfile);

    ifstream IS;
    IS.open(file.c_str() );  
    ofstream OS;
    OS.open(encrfile.c_str());

    while(IS>>line);
    {

        //encrypting each character
        for (i=0;i<line.length();i++)
        {
            line[i]^=rand()>>8;
            OS<<line[i];  //writing the character in the output file            
        }
    }

    IS.close();
    OS.close(); 

    cout<<"File "<<encrfile<<" has been encrypted"<<endl;

    cout<<"\nEnter the name of the file to decrypt: ";
    getline(cin,encrfile);  

    cout<<"\n\nDecryption of file:  "<<endl;
    ifstream IS2;
    IS2.open(encrfile.c_str()); 

    while(IS2>>line);
    {

        for (i=0;i<line.length();i++)
        {
            line[i]^=rand()>>8;
            cout<<line[i];
       }
    }
    IS2.close();



return 0;

}

4

1 に答える 1

3

;は、ループの本体が空であることを意味します。したがって、ここでファイル全体を単語ごとに読み取ります。

while(IS>>line);

したがって、上記を次のように修正します。
これで、一度に単語を読んでいます。しかし、単語間のスペースを削除しています。

while(IS>>line)

これは、期待どおりに機能するはずです。

while(std::getline(IS, line))

しかし、ここでは改行文字を破棄しています。繰り返しますが、これはおそらくあなたが望むものではありません。暗号化のポイントは、すべての文字を保持することです。

すべての文字を取得するには、1 つずつ読むのが最も簡単です。

char c;
while(IS >> std::noskipws >> c)

std::noskipws を使用します (したがって、文字が失われることはありません)。

乱数を使用して暗号化しています。
良い: ただし、乱数ジェネレーターにキーをシードして、毎回同じ一連のランドを確実に取得することをお勧めします。ただし、これは非常に特定の OS/Lib の組み合わせでのみ機能します。

        line[i]^=rand()>>8;

または、rand() をキーに置き換えることもできます。

        line[i]^=key>>8;

上記と同じ問題

while(IS2>>line);

上記と同じ問題

        line[i]^=rand()>>8;

rand() を暗号化キーとして使用:

テストされていません: ただし、出発点にする必要があります:

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>

int main()
{ 
    std::cout<<"enter a secret key: ";
    std::string ch; 
    std::getline(std::cin,ch);
    unsigned int key = 0;

    for (int i=0;i < ch.size();i++)
        key=(key+3)*ch[i];

    std::cout << "Password generated: "<<key << "\n"
              << "\nEnter the name of the input file:\n";

    std::string file;
    std::getline(std::cin,file);
    std::ifstream IS(file.c_str());  

    std::cout<<"Enter the name of the output file:\n";
    std::string encrfile;
    std::getline(std::cin,encrfile);
    std::ofstream OS(encrfile.c_str());

    std::string line;

    char c;

    srand(key);  // Reset the random number sequence.
    while(IS >> std::noskipws >> c)
    {   
        c ^= (rand() >> 8); 
        OS << c;
    }   
    IS.close();
    OS.close();

    std::cout << "File " << encrfile << " has been encrypted\n"
              << "Enter the name of the file to decrypt:\n";

    std::getline(std::cin,encrfile);
    std::cout<<"\nDecryption of file:\n";

    std::ifstream IS2(encrfile.c_str());

    srand(key);  // Reset the random number sequence.
    while(IS >> std::noskipws >> c)
    {
        c ^= (rand()>>8);
        std::cout << c;
    }
    IS2.close();
}
于 2013-05-13T20:28:27.767 に答える