我需要加密我的數據,所以我使用AES加密它們。我可以加密短數據。但我需要加密長數據,它不能工作。我可以怎樣解決這個問題。這是我的代碼。如何使用AES(openssl)加密數據?
#include "cooloi_aes.h"
CooloiAES::CooloiAES()
: MSG_LEN(0)
{
for(int i = 0; i < AES_BLOCK_SIZE; i++)
{
key[i] = 32 + i;
}
}
CooloiAES::~CooloiAES()
{
}
std::string CooloiAES::aes_encrypt(std::string msg)
{
int i = msg.size()/1024;
MSG_LEN = (i + 1) * 1024;
char in[MSG_LEN];
char out[MSG_LEN];
memset((char*)in,0,MSG_LEN);
memset((char*)out,0,MSG_LEN);
strncpy((char*)in,msg.c_str(),msg.size());
unsigned char iv[AES_BLOCK_SIZE];
for(int j = 0; j < AES_BLOCK_SIZE; ++j)
{
iv[j] = 0;
}
AES_KEY aes;
if(AES_set_encrypt_key((unsigned char*)key, 128, &aes) < 0)
{
return NULL;
}
int len = msg.size();
AES_cbc_encrypt((unsigned char*)in,(unsigned char*)out,len,&aes,iv,AES_ENCRYPT);
std::string encrypt_msg(&out[0],&out[MSG_LEN+16]);
std::cout << std::endl;
return encrypt_msg;
}
std::string CooloiAES::aes_decrypt(std::string msg)
{
MSG_LEN = msg.size();
char in[MSG_LEN];
char out[MSG_LEN+16];
memset((char*)in,0,MSG_LEN);
memset((char*)out,0,MSG_LEN+16);
strncpy((char*)in,msg.c_str(),msg.size());
std::cout << std::endl;
unsigned char iv[AES_BLOCK_SIZE];
for(int j = 0; j < AES_BLOCK_SIZE; ++j)
{
iv[j] = 0;
}
AES_KEY aes;
if(AES_set_decrypt_key((unsigned char*)key, 128, &aes) < 0)
{
return NULL;
}
int len = msg.size();
AES_cbc_encrypt((unsigned char*)in,(unsigned char*)out,len,&aes,iv,AES_DECRYPT);
std::string decrypt_msg = out;
return decrypt_msg;
}
當我加密具有96字節的數據,它將failed.I得到這個錯誤 「終止叫做拋出 '的std :: length_error' 的實例以後有什麼():basic_string的:: _ S_create 」。但我不認爲這個字符串比最大長度長,而且我也不會出錯。
我加密了96個字節的數據,然後我解密它,我弄亂了代碼,所以我說它不能工作。 – Nicholas
其實,我沒有分割我的字節,我第一次使用這種模式,所以我不知道該怎麼做。 – Nicholas
這對你有幫助嗎? http://stackoverflow.com/questions/5132939/how-to-do-aes-decryption-using-openssl?rq=1 – Madness