文件的加密/解密

Encryption/ Decryption of a file?

本文关键字:解密 加密 文件      更新时间:2023-10-16

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