用openssl里带的AES加密函数对txt文件进行加密出现的有关问题
用openssl里带的AES加密函数对txt文件进行加密出现的问题
先来说下出现的问题:
我直接复制的cpp里面的文件内容保存成1.txt,然后出来的最终的decrypt.txt文件的内存缺只有
“// Cons ”
下面是测试代码:
先来说下出现的问题:
我直接复制的cpp里面的文件内容保存成1.txt,然后出来的最终的decrypt.txt文件的内存缺只有
“// Cons ”
下面是测试代码:
// ConsoleApplication1.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include "stdafx.h"
#include <openssl/ssl.h>
#include <openssl/aes.h>
#include <openssl/rsa.h>
#include <openssl/rc4.h>
#pragma comment(lib,"libeay32.lib")
#pragma comment(lib,"ssleay32.lib")
#include "iostream"
using namespace std;
#include "string"
#include "fstream"
#define AES_KEY_BUF_LENGTH 128
#define TEST_KEY "123456789"
#define RELESE(P) if (P) \
{ \
delete P; \
P = NULL; \
}
#define RELESE_ARRAY(P) if (P) \
{ \
delete[] P; \
P = NULL; \
}
// 测试使用aes加密文件算法的例子
int TestAesEncryptTxtFile(std::string in_file_path, std::string out_file_path,
const char *aes_encrypt_key, int in_data_chunk_size = 16)
{
ifstream fin(in_file_path.c_str(), ios::in);
ofstream fout(out_file_path.c_str(), ios::out);
if(!fin)
{
cout << "Can not open fin file." << endl;
return 1;
}
if(!fout)
{
cout << "Can not open fout file." << endl;
return 1;
}
// 得到文件长度
int file_length;
fin.seekg(0, ios::end);
file_length = fin.tellg();
fin.seekg(0, ios::beg);
//用指定密钥对一段内存进行加密,结果放在outbuffer中
unsigned char aes_keybuf[AES_KEY_BUF_LENGTH];
memset(aes_keybuf,0,sizeof(aes_keybuf));
strcpy((char *)aes_keybuf, aes_encrypt_key);
AES_KEY aeskey;
AES_set_encrypt_key(aes_keybuf, 256, &aeskey);
file_length += 1;;
char *in_data = new char[file_length];
memset(in_data, 0, file_length);
char *out_data = new char[file_length];
memset(out_data, 0, file_length);
// while(!fin.eof())
{
fin.read(in_data, file_length);
int test = fin.gcount();
AES_encrypt((const unsigned char *)in_data, (unsigned char *)out_data, &aeskey);
fout.write(out_data, fin.gcount());
};
fout.close();
fin.close();
RELESE_ARRAY(in_data);
RELESE_ARRAY(out_data);
return 0;
}
// 测试使用aes解密文件算法的例子
int TestAesDecryptTxtFile(std::string in_file_path, std::string out_file_path,
const char *rc4_dencrypt_key, int in_data_chunk_size = 16)
{
ifstream fin(in_file_path.c_str(), ios::in);
ofstream fout(out_file_path.c_str(), ios::out);
if(!fin)
{
cout << "Can not open fin file." << endl;
return 1;
}
if(!fout)
{
cout << "Can not open fout file." << endl;
return 1;
}
// 得到文件长度
int file_length;
fin.seekg(0, ios::end);
file_length = fin.tellg();
fin.seekg(0, ios::beg);
//用指定密钥对一段内存进行加密,结果放在outbuffer中
unsigned char aes_keybuf[AES_KEY_BUF_LENGTH];
memset(aes_keybuf,0,sizeof(aes_keybuf));
strcpy((char *)aes_keybuf, rc4_dencrypt_key);
AES_KEY aeskey;
AES_set_decrypt_key(aes_keybuf, 256, &aeskey);
file_length += 1;;
char *in_data = new char[file_length];
memset(in_data, 0, file_length);
char *out_data = new char[file_length];
memset(out_data, 0, file_length);
// while( ! fin.eof() )
{
fin.read(in_data, file_length);
AES_decrypt((unsigned char *)in_data, (unsigned char *)out_data, &aeskey);
fout.write(out_data, fin.gcount());
};
fout.close();
fin.close();
RELESE_ARRAY(in_data);
RELESE_ARRAY(out_data);
return 0;
}
int _tmain(int argc, _TCHAR* argv[])
{
TestAesEncryptTxtFile("1.txt", "2.txt", TEST_KEY);
TestAesDecryptTxtFile("2.txt", "decrypt.txt", TEST_KEY);
return 0;
}
文章评论
相关解决方案