@@ -291,22 +291,33 @@ CryptHelper::AES128Encrypt(const unsigned char *keyData, unsigned keySize, const
|
|||||||
outCipher.resize(cipherSize);
|
outCipher.resize(cipherSize);
|
||||||
|
|
||||||
#ifdef HAVE_OPENSSL
|
#ifdef HAVE_OPENSSL
|
||||||
EVP_CIPHER_CTX encryptCtx;
|
#if OPENSSL_VERSION_NUMBER >= 0x10100000L
|
||||||
EVP_CIPHER_CTX_init(&encryptCtx);
|
EVP_CIPHER_CTX *encryptCtx = EVP_CIPHER_CTX_new();
|
||||||
|
#else
|
||||||
|
EVP_CIPHER_CTX _encryptCtx;
|
||||||
|
EVP_CIPHER_CTX *encryptCtx;
|
||||||
|
encryptCtx = &_encryptCtx;
|
||||||
|
#endif // OPENSSL_VERSION_NUMBER >= 0x10100000L
|
||||||
|
|
||||||
|
EVP_CIPHER_CTX_init(encryptCtx);
|
||||||
int outCipherSize = cipherSize;
|
int outCipherSize = cipherSize;
|
||||||
|
|
||||||
int success = EVP_EncryptInit(&encryptCtx, EVP_aes_128_cbc(), key, iv);
|
int success = EVP_EncryptInit(encryptCtx, EVP_aes_128_cbc(), key, iv);
|
||||||
EVP_CIPHER_CTX_set_padding(&encryptCtx, 0);
|
EVP_CIPHER_CTX_set_padding(encryptCtx, 0);
|
||||||
if (success) {
|
if (success) {
|
||||||
success = EVP_EncryptUpdate(&encryptCtx, &outCipher[0], &outCipherSize, paddedPlainStr, paddedPlainSize);
|
success = EVP_EncryptUpdate(encryptCtx, &outCipher[0], &outCipherSize, paddedPlainStr, paddedPlainSize);
|
||||||
|
|
||||||
if (success && outCipherSize) {
|
if (success && outCipherSize) {
|
||||||
// Since padding is off, this will not modify the cipher. However, parameters need to be set.
|
// Since padding is off, this will not modify the cipher. However, parameters need to be set.
|
||||||
EVP_EncryptFinal(&encryptCtx, &outCipher[0], &outCipherSize);
|
EVP_EncryptFinal(encryptCtx, &outCipher[0], &outCipherSize);
|
||||||
retVal = true;
|
retVal = true;
|
||||||
}
|
}
|
||||||
} else
|
} else
|
||||||
outCipher.clear();
|
outCipher.clear();
|
||||||
|
|
||||||
|
#if OPENSSL_VERSION_NUMBER >= 0x10100000L
|
||||||
|
EVP_CIPHER_CTX_free(encryptCtx);
|
||||||
|
#endif // OPENSSL_VERSION_NUMBER >= 0x10100000L
|
||||||
#else
|
#else
|
||||||
gcry_cipher_hd_t hd;
|
gcry_cipher_hd_t hd;
|
||||||
gcry_error_t err = gcry_cipher_open(&hd, GCRY_CIPHER_AES128, GCRY_CIPHER_MODE_CBC, 0);
|
gcry_error_t err = gcry_cipher_open(&hd, GCRY_CIPHER_AES128, GCRY_CIPHER_MODE_CBC, 0);
|
||||||
@@ -338,22 +349,33 @@ CryptHelper::AES128Decrypt(const unsigned char *keyData, unsigned keySize, const
|
|||||||
BytesToKey(keyData, keySize, key, iv);
|
BytesToKey(keyData, keySize, key, iv);
|
||||||
outPlain.resize(cipherSize);
|
outPlain.resize(cipherSize);
|
||||||
#ifdef HAVE_OPENSSL
|
#ifdef HAVE_OPENSSL
|
||||||
EVP_CIPHER_CTX decryptCtx;
|
#if OPENSSL_VERSION_NUMBER >= 0x10100000L
|
||||||
EVP_CIPHER_CTX_init(&decryptCtx);
|
EVP_CIPHER_CTX *decryptCtx = EVP_CIPHER_CTX_new();
|
||||||
|
#else
|
||||||
|
EVP_CIPHER_CTX _decryptCtx;
|
||||||
|
EVP_CIPHER_CTX *decryptCtx;
|
||||||
|
decryptCtx = &_decryptCtx;
|
||||||
|
#endif // OPENSSL_VERSION_NUMBER >= 0x10100000L
|
||||||
|
|
||||||
|
EVP_CIPHER_CTX_init(decryptCtx);
|
||||||
int outPlainSize = cipherSize;
|
int outPlainSize = cipherSize;
|
||||||
|
|
||||||
int success = EVP_DecryptInit(&decryptCtx, EVP_aes_128_cbc(), key, iv);
|
int success = EVP_DecryptInit(decryptCtx, EVP_aes_128_cbc(), key, iv);
|
||||||
EVP_CIPHER_CTX_set_padding(&decryptCtx, 0);
|
EVP_CIPHER_CTX_set_padding(decryptCtx, 0);
|
||||||
if (success) {
|
if (success) {
|
||||||
success = EVP_DecryptUpdate(&decryptCtx, (unsigned char *)&outPlain[0], &outPlainSize, cipher, cipherSize);
|
success = EVP_DecryptUpdate(decryptCtx, (unsigned char *)&outPlain[0], &outPlainSize, cipher, cipherSize);
|
||||||
|
|
||||||
if (success && outPlainSize) {
|
if (success && outPlainSize) {
|
||||||
// Since padding is off, this will not modify the plain text. However, parameters need to be set.
|
// Since padding is off, this will not modify the plain text. However, parameters need to be set.
|
||||||
EVP_DecryptFinal(&decryptCtx, (unsigned char *)outPlain.c_str(), &outPlainSize);
|
EVP_DecryptFinal(decryptCtx, (unsigned char *)outPlain.c_str(), &outPlainSize);
|
||||||
retVal = true;
|
retVal = true;
|
||||||
}
|
}
|
||||||
} else
|
} else
|
||||||
outPlain.clear();
|
outPlain.clear();
|
||||||
|
|
||||||
|
#if OPENSSL_VERSION_NUMBER >= 0x10100000L
|
||||||
|
EVP_CIPHER_CTX_free(decryptCtx);
|
||||||
|
#endif // OPENSSL_VERSION_NUMBER >= 0x10100000L
|
||||||
#else
|
#else
|
||||||
gcry_cipher_hd_t hd;
|
gcry_cipher_hd_t hd;
|
||||||
gcry_error_t err = gcry_cipher_open(&hd, GCRY_CIPHER_AES128, GCRY_CIPHER_MODE_CBC, 0);
|
gcry_error_t err = gcry_cipher_open(&hd, GCRY_CIPHER_AES128, GCRY_CIPHER_MODE_CBC, 0);
|
||||||
|
|||||||
Reference in New Issue
Block a user