/*************************************************************************
* > File Name: AES.h
* > Author:
* > Mail:
* > Created Time: 2017-03-09
* > statement: AES加密/解密类
*************************************************************************/
#ifndef _AES_H_
#define _AES_H_
#include <string.h>
#include <openssl/aes.h>
#include <string>
namespace aes
{
using namespace std;
class AES
{
private:
AES_KEY m_EncryptKey; // 加密密钥
AES_KEY m_DecryptKey; // 解密密钥
public:
/*
* @brief 初始化密钥
* @aeskey encryption key
* @return 返回true则初始化成功
* */
bool Init(const string &aeskey);
/*
* @brief 加密字符串
* @originstr 原文
* @encryptstr 密文
* */
void EncryptString(const string &originstr, string &encryptstr);
/*
* @brief 解密字符串
* @encryptstr 密文
* @originstr 原文
* */
void DecryptString(const string &encryptstr, string &originstr);
};
}
#endif
/*************************************************************************
* > File Name: AES.h
* > Author: wangbin
* > Mail:
* > Created Time:
* > statement: AES加密/解密类
**************************************************************************/
#include "AES.h"
namespace aes
{
/*
* @brief 初始化密钥
* @aeskey encryption key
* @return 返回true则初始化成功
* */
bool AES::Init(const string &aeskey)
{
static unsigned char AESKEY[AES_BLOCK_SIZE + 1] = ""; // 密钥
memcpy(AESKEY, (unsigned char *)aeskey.c_str(), sizeof(AESKEY) - 1);
memset(&m_EncryptKey, 0, sizeof(AES_KEY));
memset(&m_DecryptKey, 0, sizeof(AES_KEY));
// 设置加解密密钥
return !AES_set_encrypt_key(AESKEY, 128, &m_EncryptKey) && !AES_set_decrypt_key(AESKEY, 128, &m_DecryptKey);
}
/*
* @brief 加密字符串
* @originstr 原文
* @encryptstr 密文
* */
void AES::EncryptString(const string &originstr, string &encryptstr)
{
if(originstr.empty()) return;
int size = originstr.size();
int NewLen = size; // 加密后字符串长度
unsigned char ivec[AES_BLOCK_SIZE]; // 初始化向量
memset(ivec, 0, sizeof(ivec));
if(size % AES_BLOCK_SIZE) NewLen = ((size / AES_BLOCK_SIZE) + 1) * AES_BLOCK_SIZE; // 将NewLen设置为AES_BLOCK_SIZE的倍数
// 初始化输入字符串
char *pInputStr = new char[NewLen + 1];
if(!pInputStr) return;
memset(pInputStr, 0, NewLen + 1);
memcpy(pInputStr, originstr.c_str(), size);
// 初始化加密字符串
char *pEncryptStr = new char[NewLen + 1];
if(!pEncryptStr) return;
memset(pEncryptStr, 0, NewLen + 1);
/*
* void AES_cbc_encrypt(const unsigned char *in, unsigned char *out, size_t length, const AES_KEY *key, unsigned char *ivec, const int enc);
* AES加密/解密单个数据块,CBC模式
* in 需要加密/解密的数据;
* out 计算后输出的数据;
* length 数据长度
* key 密钥
* vec 初始向量
* enc AES_ENCRYPT 代表加密 AES_DECRYPT代表解密
* */
AES_cbc_encrypt((unsigned char *)pInputStr, (unsigned char *)pEncryptStr, NewLen, &m_EncryptKey, ivec, AES_ENCRYPT);
char buf[1024] = {0};
memcpy(buf, &NewLen, sizeof(int)); // 先将NewLen拷贝到头4个字节中 用作解密
memcpy(buf + sizeof(int), pEncryptStr, NewLen + 1); // 再将加密后的字符串拷贝buf剩余字节中
encryptstr.assign(buf, sizeof(int) + NewLen + 1); // 必须完整保留NewLen + 1长度的字符串
// 释放内存
delete []pInputStr;
pInputStr = NULL;
delete []pEncryptStr;
pEncryptStr = NULL;
}
/*
* @brief 解密字符串
* @encryptstr 密文
* @originstr 原文
* */
void AES::DecryptString(const string &encryptstr, string &originstr)
{
if(encryptstr.empty()) return;
unsigned char ivec[AES_BLOCK_SIZE]; // 初始化向量
memset(ivec, 0, sizeof(ivec));
int NewLen;
memcpy(&NewLen, encryptstr.c_str(), sizeof(int)); // 获取NewLen
int size = encryptstr.size();
if(NewLen != size - 5) return; // NewLen合法性判断
// 初始化解密字符串
char *pDecryptStr = new char[NewLen + 1];
memset(pDecryptStr, 0, NewLen + 1);
// 解密
AES_cbc_encrypt((unsigned char *)(encryptstr.c_str() + sizeof(int)), (unsigned char *)pDecryptStr, NewLen, &m_DecryptKey, ivec, AES_DECRYPT);
originstr.assign(pDecryptStr);
delete []pDecryptStr;
pDecryptStr = NULL;
}
}
/*************************************************************************
* > File Name: BASE64.h
* > Author:
* > Mail:
* > Created Time: 2017-03-09
* > statement: BASE64编码/解码类
*************************************************************************/
#ifndef _BASE64_H_
#define _BASE64_H_
#include "FCEncode.h"
#include <string>
namespace base64
{
using namespace std;
static const int BUFF_LEN = 1024;
class BASE64
{
public:
/*
* @brief 编码字符串
* @originstr 原字符串串
* @encodedstr 编码字符串
* */
static void EncodeString(const string &originstr, string &encodedstr);
/*
* @brief 解码字符串
* @encodedstr 编码字符串
* @originstr 原字符串
* */
static void DecodeString(const string &encodedstr, string &originstr);
};
}
#endif
/*************************************************************************
* > File Name: BASE64.cpp
* > Author:
* > Mail:
* > Created Time: 2017-03-09
* > statement: BASE64编码/解码类
**************************************************************************/
#include "BASE64.h"
namespace base64
{
/*
* @brief 编码字符串
* @originstr 原字符串串
* @encodedstr 编码字符串
* */
void BASE64::EncodeString(const string &originstr, string &encodedstr)
{
char encoded_buf[BUFF_LEN] = {0}; // 编码缓冲区
int encoded_len; // 编码缓冲区的长度
encoded_len = FCBase64Encode(originstr.c_str(), originstr.size(), encoded_buf, BUFF_LEN);
if(encoded_len > 0) encodedstr.assign(encoded_buf, encoded_len);
}
/*
* @brief 解码字符串
* @encodedstr 编码字符串
* @originstr 原字符串
* */
void BASE64::DecodeString(const string &encodedstr, string &originstr)
{
char decoded_buf[BUFF_LEN] = {0}; // 解码缓冲区
int decoded_len; // 解码缓冲区的长度
decoded_len = FCBase64Decode(encodedstr.c_str(), encodedstr.size(), decoded_buf, BUFF_LEN);
if(decoded_len > 0) originstr.assign(decoded_buf, decoded_len);
}
}
/****************************************************************************
@File Name: test.cpp
@Author:
@mail:
@Created Time: Thu 09 Mar 2017 01:38:38 PM CST
****************************************************************************/
#include "AES.h"
#include "BASE64.h"
#include <gflags/gflags.h>
#include <iostream>
using namespace aes;
using namespace base64;
using namespace google;
static const int MAXKEYLEN = 64;
DEFINE_string(key, "127.0.0.1", "aes key");
static bool ValidatePort(const char *key, const string& value)
{
if(value.size() > MAXKEYLEN) return false;
return true;
}
// it will call before main
static const bool key_dummy = RegisterFlagValidator(&FLAGS_key, &ValidatePort);
int main(int argc, char **argv)
{
ParseCommandLineFlags(&argc, &argv, true);
AES aes;
if(!aes.Init(FLAGS_key))
{
cerr << "AES init failed...!" << endl;
return -1;
}
string value;
string newvalue;
cout << "str = ";
cin >> value;
cout << "aes encrytion=====================================" << endl;
aes.EncryptString(value, newvalue);
cout << newvalue << endl;
cout << "base64 encode=====================================" << endl;
BASE64::EncodeString(newvalue, value);
cout << value << endl;
cout << "base64 decode=====================================" << endl;
BASE64::DecodeString(value, newvalue);
cout << newvalue << endl;
cout << "aes decrytion=====================================" << endl;
aes.DecryptString(newvalue, value);
cout << value << endl;
return 0;
}
CC=g++
all:
$(CC) -std=c++11 -g -o AESTest test.cpp AES.cpp AES.h BASE64.h BASE64.cpp FCEncode.h -lcrypto -lgflags -lFCEncode
export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH
openssl_aes
最新推荐文章于 2024-08-16 19:13:12 发布