我加密了一条消息,然后通过套接字将其发送到其他计算机。但是我无法解密密文。我在两台计算机上使用了相同的密钥和 iv。我尝试将密文发送到其他计算机,然后将密文发回。我可以在同一台计算机上解密密文。但我想在其他计算机上删除它。这是我的代码。
#include "cooloi_aes.h"
CooloiAES::CooloiAES(std::string aes_key)
{
//key = (unsigned char*)"01234567890123456789012345678901";
key = (unsigned char*)aes_key.c_str();
iv = (unsigned char*)"aabbccddeeffgghh";
ERR_load_crypto_strings();
OpenSSL_add_all_algorithms();
OPENSSL_config(NULL);
}
CooloiAES::~CooloiAES()
{
}
CooloiAES* CooloiAES::Create(std::string aes_key)
{
auto ret = new CooloiAES(aes_key);
return ret;
}
void CooloiAES::handleErrors(void)
{
ERR_print_errors_fp(stderr);
abort();
}
int CooloiAES::encrypt(unsigned char *plaintext, int plaintext_len, unsigned char *key,
unsigned char *iv, unsigned char *ciphertext)
{
EVP_CIPHER_CTX *ctx;
int len;
int ciphertext_len;
// Create and initialise the context
if(!(ctx = EVP_CIPHER_CTX_new()))
handleErrors();
// Initialise the encryption operation. IMPORTANT - ensure you use a key
// and IV size appropriate for your cipher
// In this example we are using 256 bit AES (i.e. a 256 bit key). The
// IV size for *most* modes is the same as the block size. For AES this
// is 128 bits
if(1 != EVP_EncryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, key, iv))
handleErrors();
// Provide the message to be encrypted, and obtain the encrypted output.
// EVP_EncryptUpdate can be called multiple times if necessary
if(1 != EVP_EncryptUpdate(ctx, ciphertext, &len, plaintext, plaintext_len))
handleErrors();
ciphertext_len = len;
// Finalise the encryption. Further ciphertext bytes may be written at
// this stage.
//
if(1 != EVP_EncryptFinal_ex(ctx, ciphertext + len, &len))
handleErrors();
ciphertext_len += len;
// Clean up
EVP_CIPHER_CTX_free(ctx);
return ciphertext_len;
}
int CooloiAES::decrypt(unsigned char *ciphertext, int ciphertext_len, unsigned char *key,
unsigned char *iv, unsigned char *plaintext)
{
EVP_CIPHER_CTX *ctx;
int len;
int plaintext_len;
// Create and initialise the context */
if(!(ctx = EVP_CIPHER_CTX_new()))
handleErrors();
// Initialise the decryption operation. IMPORTANT - ensure you use a key
// and IV size appropriate for your cipher
// In this example we are using 256 bit AES (i.e. a 256 bit key). The
// IV size for *most* modes is the same as the block size. For AES this
// is 128 bits
if(1 != EVP_DecryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, key, iv))
handleErrors();
// Provide the message to be decrypted, and obtain the plaintext output.
// EVP_DecryptUpdate can be called multiple times if necessary
if(1 != EVP_DecryptUpdate(ctx, plaintext, &len, ciphertext, ciphertext_len))
handleErrors();
plaintext_len = len;
// Finalise the decryption. Further plaintext bytes may be written at
// this stage.
if(1 != EVP_DecryptFinal_ex(ctx, plaintext + len, &len))
handleErrors();
plaintext_len += len;
// Clean up
EVP_CIPHER_CTX_free(ctx);
return plaintext_len;
}
std::string CooloiAES::aes_encrypt(std::string msg)
{
int MSG_LEN = ((msg.size() / 16) + 1) * 16;
unsigned char* plaintext = (unsigned char*)msg.c_str();
unsigned char ciphertext[MSG_LEN];
unsigned char decryptedtext[MSG_LEN];
unsigned char cipher[MSG_LEN];
int decryptedtext_len,ciphertext_len;
ciphertext_len = encrypt(plaintext,strlen((char*)plaintext),key,iv,ciphertext);
std::string str(ciphertext,ciphertext+ciphertext_len);
return str;
}
std::string CooloiAES::aes_decrypt(std::string msg)
{
int MSG_LEN = msg.size();
//unsigned char* plaintext = (unsigned char*)msg.c_str();
unsigned char ciphertext[MSG_LEN];
unsigned char decryptedtext[MSG_LEN];
unsigned char cipher[MSG_LEN];
int decryptedtext_len,ciphertext_len;
memcpy(cipher,msg.data(),msg.size());
int len = sizeof(cipher);
decryptedtext_len = decrypt(cipher,len,key,iv,decryptedtext);
decryptedtext[decryptedtext_len] = '\0';
std::string dec((char*)decryptedtext);
return dec;
}