64,682
社区成员
发帖
与我相关
我的任务
分享
// 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;
}
int aes_encrypt(char* in, char* key, char* out)//, int olen)可能会设置buf长度
{
if(!in || !key || !out) return 0;
unsigned char iv[AES_BLOCK_SIZE];//加密的初始化向量
for(int i=0; i<AES_BLOCK_SIZE; ++i)//iv一般设置为全0,可以设置其他,但是加密解密要一样就行
iv[i]=0;
AES_KEY aes;
if(AES_set_encrypt_key((unsigned char*)key, 128, &aes) < 0)
{
return 0;
}
int len=strlen(in);
AES_cbc_encrypt((unsigned char*)in, (unsigned char*)out, len, &aes, iv, AES_ENCRYPT);
return 1;
}
int aes_decrypt(char* in, char* key, char* out)
{
if(!in || !key || !out) return 0;
unsigned char iv[AES_BLOCK_SIZE];//加密的初始化向量
for(int i=0; i<AES_BLOCK_SIZE; ++i)//iv一般设置为全0,可以设置其他,但是加密解密要一样就行
iv[i]=0;
AES_KEY aes;
if(AES_set_decrypt_key((unsigned char*)key, 128, &aes) < 0)
{
return 0;
}
int len=strlen(in);
AES_cbc_encrypt((unsigned char*)in, (unsigned char*)out, len, &aes, iv, AES_DECRYPT);
return 1;
}
如果你真的要用AES_encrypt这个加密的话,看下面的代码
int aes_encrypt(char* in, char* key, char* out)//, int olen)
{
if(!in || !key || !out) return 0;
AES_KEY aes;
if(AES_set_encrypt_key((unsigned char*)key, 128, &aes) < 0)
{
return 0;
}
int len=strlen(in), en_len=0;
while(en_len<len)//输入输出字符串够长,并且是AES_BLOCK_SIZE的整数倍,需要严格限制
{
AES_encrypt((unsigned char*)in, (unsigned char*)out, &aes);
in+=AES_BLOCK_SIZE;
out+=AES_BLOCK_SIZE;
en_len+=AES_BLOCK_SIZE;
}
return 1;
}
int aes_decrypt(char* in, char* key, char* out)
{
if(!in || !key || !out) return 0;
AES_KEY aes;
if(AES_set_decrypt_key((unsigned char*)key, 128, &aes) < 0)
{
return 0;
}
int len=strlen(in), en_len=0;
while(en_len<len)
{
AES_decrypt((unsigned char*)in, (unsigned char*)out, &aes);
in+=AES_BLOCK_SIZE;
out+=AES_BLOCK_SIZE;
en_len+=AES_BLOCK_SIZE;
}
return 1;
}
建议你看看下面两个链接
http://yuanshuilee.blog.163.com/blog/static/21769727520140942826137/
http://yuanshuilee.blog.163.com/blog/static/217697275201311314185639/
// 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::binary | ios::in);
ofstream fout(out_file_path.c_str(), ios::binary | 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[in_data_chunk_size + 1];
memset(in_data, 0, in_data_chunk_size + 1);
char *out_data = new char[in_data_chunk_size + 1];
memset(out_data, 0, in_data_chunk_size + 1);
while(!fin.eof())
{
fin.read(in_data, in_data_chunk_size);
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::binary | ios::in);
ofstream fout(out_file_path.c_str(), ios::binary | 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[in_data_chunk_size + 1];
memset(in_data, 0, in_data_chunk_size + 1);
char *out_data = new char[in_data_chunk_size + 1];
memset(out_data, 0, in_data_chunk_size + 1);
while(!fin.eof())
{
fin.read(in_data, in_data_chunk_size);
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;
}
// 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::binary | ios::in);
ofstream fout(out_file_path.c_str(), ios::binary | 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::binary | ios::in);
ofstream fout(out_file_path.c_str(), ios::binary | 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;
}