Hi, this is a two part question. First in the cipherEncrypt16b function below I try to encrypt a signle block with aes128, but I cant really use EVP_CipherFinal_ex since it seems to put out and extra block of data even tho its only 16bytes put in.. This part works tho when EVP_CipherFinal_ex is commented out. The part that I find really strange here is in the secound function called cipherDecrypt16b. When the EVP_CipherUpdate is run the data is correctly added to the char array but the data length is set to 0.
#include <openssl/rsa.h> #include <openssl/evp.h> #include <openssl/objects.h> #include <openssl/x509.h> #include <openssl/err.h> #include <openssl/pem.h> #include <openssl/ssl.h> #include <openssl/rand.h> #include <string> using namespace std; bool cipherEncrypt16b( const EVP_CIPHER *pCipherType, const string &rSalt, const string &rUnencryptedData, string &rEncryptedData, const string &rKey, const string &rIv ) { if (rUnencryptedData.size() == 16) { EVP_CIPHER_CTX ectx; EVP_CIPHER_CTX_init(&ectx); EVP_CipherInit_ex(&ectx, pCipherType, NULL, reinterpret_cast<const unsigned char*>(rKey.c_str()), reinterpret_cast<const unsigned char*>(rIv.c_str()), 1); unsigned char *pOutBuffer = new unsigned char[rUnencryptedData.size() + EVP_MAX_KEY_LENGTH +1]; memset(pOutBuffer, 0, rUnencryptedData.size() + EVP_MAX_KEY_LENGTH +1); int outLength; EVP_CipherUpdate(&ectx, pOutBuffer, &outLength, reinterpret_cast<const unsigned char*>(rUnencryptedData.c_str()), static_cast<int>(rUnencryptedData.size()) ); rEncryptedData.append(reinterpret_cast<char*>(pOutBuffer), outLength); // memset(pOutBuffer, 0, rEncryptedData.size() + EVP_MAX_KEY_LENGTH +1); // outLength = 0; // // EVP_CipherFinal_ex(&ectx, pOutBuffer, &outLength); // rUncryptedData.append(reinterpret_cast<char*>(pOutBuffer), outLength); EVP_CIPHER_CTX_cleanup(&ectx); if( pOutBuffer ) { delete[] pOutBuffer; } pOutBuffer = NULL; return true; } return false; } bool cipherDecrypt16b( const EVP_CIPHER *pCipherType, const string &rSalt, const string &rEncryptedData, string &rUncryptedData, const string &rKey, const string &rIv ) { if (rEncryptedData.size() == 16) { EVP_CIPHER_CTX ectx; EVP_CIPHER_CTX_init(&ectx); EVP_CipherInit_ex(&ectx, pCipherType, NULL, reinterpret_cast<const unsigned char*>(rKey.c_str()), reinterpret_cast<const unsigned char*>(rIv.c_str()), 0); unsigned char *pOutBuffer = new unsigned char[rEncryptedData.size() + EVP_MAX_KEY_LENGTH +1]; memset(pOutBuffer, 0, rEncryptedData.size() + EVP_MAX_KEY_LENGTH +1); int outLength = 0; EVP_CipherUpdate(&ectx, pOutBuffer, &outLength, reinterpret_cast<const unsigned char*>(rEncryptedData.c_str()), static_cast<int>(rEncryptedData.size()) ); rUncryptedData.append(reinterpret_cast<char*>(pOutBuffer), outLength); memset(pOutBuffer, 0, rEncryptedData.size() + EVP_MAX_KEY_LENGTH +1); outLength = 0; EVP_CipherFinal_ex(&ectx, pOutBuffer, &outLength); rUncryptedData.append(reinterpret_cast<char*>(pOutBuffer), outLength); EVP_CIPHER_CTX_cleanup(&ectx); if( pOutBuffer ) { delete[] pOutBuffer; } pOutBuffer = NULL; return true; } return false; } int _tmain(int argc, _TCHAR* argv[]) { string in_d = "0123456789012345"; string ut_d; string salt = ""; string key = "5432109876543210"; string iv = "5432109876543210"; string unenc; bool res = cipherEncrypt16b(EVP_aes_128_cbc(), "", in_d, ut_d, key, iv); bool res2 = cipherDecrypt16b(EVP_aes_128_cbc(), "", ut_d, unenc, key, iv); return 0; } /regards Johan Foglemark