2013-05-13 161 views
-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; 

}

+0

您正在寫出隨機字節,您預計會發生什麼? – PlasmaHH 2013-05-13 20:05:21

+0

你不用任何東西的鑰匙。 – 2013-05-13 20:05:23

+0

注意:'while(IS >> line)'讀一個字不是一行。所以你會從你的文件中刪除所有的空格。 – 2013-05-13 20:19:58

回答

3

;意味着環路有一個空體。 所以你在這裏逐字閱讀整個文件。

while(IS>>line); 

因此修正以上:
現在你每次讀一個字。但它正在放棄單詞之間的空格。

while(IS>>line) 

這應該更符合您的預期。

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

但是,在這裏您將丟棄新的行字符。所以這可能不是你想要的。加密的重點是保留所有的字符。

爲了獲得最簡單的是所有字符由一個讀他們一個:

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

使用std :: noskipws(這樣你就不會丟失任何字符)。

您正在使用一個隨機數進行加密。
好的:但是你可能想要用隨機數發生器生成關鍵字,以確保每次獲得相同的rands序列。 但是這隻適用於非常特定的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(); 
} 
+0

這不會有幫助。只要任何輸入字符加密到換行符,解密器和加密器就會不同步。 – 2013-05-13 21:53:09

+0

@DavidSchwartz:澄清大衛說的話。如果任何字符加密到一個新的行字符,然後讀取數據回來了與'的std ::函數getline()'不會很好地工作,因爲它會提前停止(拋棄「\ n」)。這對於rand()技術來說是個問題,因爲它依賴於輸出,使用與輸入完全相同的序列。當然你可以解決這個問題,但需要一些工作。 – 2013-05-14 02:09:45