文件的加密/解密
Encryption/ Decryption of a file?
我正在尝试加密,然后解密文件。当我尝试解密文件时,我想在屏幕上显示内容,以确保解密过程没有问题。但是,我没有任何文件解密的显示。我不确定我的代码中缺少什么。我正在使用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();
}
相关文章:
- AES加密到解密未正确输出
- 通过 Openssl 命令行加密,通过 c++ 解密
- 将加密消息从 php 发送到 C++ 应用程序,然后使用 CryptoPP 进行解密
- POCO C++加密/解密
- 从加密项目向量中解密任意选择的元素会导致无效的 PKCS #7 块错误
- 尝试从 Visual Studios C++ 中的加密字符串输出解密字符串时出现逻辑错误
- 压缩天然气.AES.错误解密(可能加密)的文件
- 如何制作一个程序,它使用特定单词加密和解密消息?
- 基本加密和解密方法不起作用 C++ 11.
- 异或字符串加密/解密输出错误
- 简单仿射密码加密解密
- QT:AES-256-CBC 根据 PHP 代码在C++中加密/解密
- AES加密/解密接收所有明文
- 从或写入boost :: asio :: ssl :: stream :: next_layer()旁路SSL解密/加密
- AES_cfb128_encrypt不会解密加密文件中的所有字节
- AES 加密C++(无法解密加密文件)
- 在libgcrypt的Crypto++中解密加密数据的步骤
- 使用RSA公钥解密/加密大数据
- 如何解密加密文本
- 解密加密字符串