【问题标题】:Encryption/ Decryption of a file?文件的加密/解密?
【发布时间】:2013-05-07 23:14:03
【问题描述】:

我正在尝试加密,然后解密文件。当我尝试解密文件时,我想在屏幕上显示内容以确保解密过程没有问题。但是,我没有任何文件解密的显示。我不确定我的代码中缺少什么。我正在使用 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;

}

【问题讨论】:

  • 你正在写出随机字节,你期望会发生什么?
  • 您不会将密钥用于任何事情。
  • 注意:while(IS&gt;&gt;line) 读取的是一个单词而不是一行。因此,您将从文件中删除所有空格。
  • 注意:while(IS>>line)**;** 你有一个栏杆 ; 这意味着 while 循环没有主体。
  • 您的“加密”不保留格式。然而,它依赖于能够从 加密 数据中读取行,因为原始 plaintext 由行组成。这从根本上被打破了。

标签: c++ file encryption


【解决方案1】:

; 表示循环的主体为空。 所以你在这里逐字阅读整个文件。

while(IS>>line);

因此将上述更正为:
现在你一次读一个单词。但它正在删除单词之间的空格。

while(IS>>line)

这应该更符合您的预期。

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

但是在这里你丢弃了换行符。所以这可能不是你想要的。加密的重点是保留所有字符。

要获得所有字符,最简单的方法是一个一个地读取它们:

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

使用 std::noskipws(这样就不会丢失任何字符)。

您正在使用 rand 数字进行加密。
很好:但是您可能希望使用密钥为随机数生成器播种,以确保每次获得相同的随机数序列。 但是这仅适用于非常特定的 OS/Lib 组合。

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

您也可以将 rand() 替换为 key。

        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();
}

【讨论】:

  • 这无济于事。一旦任何输入字符加密到换行符,他的解密器和加密器就会不同步。
  • @DavidSchwartz:澄清大卫所说的话。如果任何字符被加密为换行符,那么使用std::getline(&lt;stream&gt;, &lt;string&gt;) 读回数据不会很好,因为它会过早停止(丢弃'\n')。这是rand() 技术的一个问题,因为它依赖于使用与输入完全相同的序列的输出。当然你可以解决这个问题,但这需要一些工作。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多