@@ -57,15 +57,13 @@ bool CCrypter::Encrypt(const CKeyingMaterial& vchPlaintext, std::vector<unsigned
5757 int nCLen = nLen + AES_BLOCK_SIZE, nFLen = 0 ;
5858 vchCiphertext = std::vector<unsigned char >(nCLen);
5959
60- EVP_CIPHER_CTX ctx;
61-
6260 bool fOk = true ;
6361
64- EVP_CIPHER_CTX_init (& ctx);
65- if (fOk ) fOk = EVP_EncryptInit_ex (& ctx, EVP_aes_256_cbc (), NULL , chKey, chIV) != 0 ;
66- if (fOk ) fOk = EVP_EncryptUpdate (& ctx, &vchCiphertext[0 ], &nCLen, &vchPlaintext[0 ], nLen) != 0 ;
67- if (fOk ) fOk = EVP_EncryptFinal_ex (& ctx, (&vchCiphertext[0 ]) + nCLen, &nFLen) != 0 ;
68- EVP_CIPHER_CTX_cleanup (& ctx);
62+ EVP_CIPHER_CTX* ctx = EVP_CIPHER_CTX_new ( );
63+ if (fOk ) fOk = EVP_EncryptInit_ex (ctx, EVP_aes_256_cbc (), NULL , chKey, chIV) != 0 ;
64+ if (fOk ) fOk = EVP_EncryptUpdate (ctx, &vchCiphertext[0 ], &nCLen, &vchPlaintext[0 ], nLen) != 0 ;
65+ if (fOk ) fOk = EVP_EncryptFinal_ex (ctx, (&vchCiphertext[0 ]) + nCLen, &nFLen) != 0 ;
66+ EVP_CIPHER_CTX_free ( ctx);
6967
7068 if (!fOk ) return false ;
7169
@@ -84,15 +82,13 @@ bool CCrypter::Decrypt(const std::vector<unsigned char>& vchCiphertext, CKeyingM
8482
8583 vchPlaintext = CKeyingMaterial (nPLen);
8684
87- EVP_CIPHER_CTX ctx;
88-
8985 bool fOk = true ;
9086
91- EVP_CIPHER_CTX_init (& ctx);
92- if (fOk ) fOk = EVP_DecryptInit_ex (& ctx, EVP_aes_256_cbc (), NULL , chKey, chIV) != 0 ;
93- if (fOk ) fOk = EVP_DecryptUpdate (& ctx, &vchPlaintext[0 ], &nPLen, &vchCiphertext[0 ], nLen) != 0 ;
94- if (fOk ) fOk = EVP_DecryptFinal_ex (& ctx, (&vchPlaintext[0 ]) + nPLen, &nFLen) != 0 ;
95- EVP_CIPHER_CTX_cleanup (& ctx);
87+ EVP_CIPHER_CTX* ctx = EVP_CIPHER_CTX_new ( );
88+ if (fOk ) fOk = EVP_DecryptInit_ex (ctx, EVP_aes_256_cbc (), NULL , chKey, chIV) != 0 ;
89+ if (fOk ) fOk = EVP_DecryptUpdate (ctx, &vchPlaintext[0 ], &nPLen, &vchCiphertext[0 ], nLen) != 0 ;
90+ if (fOk ) fOk = EVP_DecryptFinal_ex (ctx, (&vchPlaintext[0 ]) + nPLen, &nFLen) != 0 ;
91+ EVP_CIPHER_CTX_free ( ctx);
9692
9793 if (!fOk ) return false ;
9894
@@ -131,15 +127,15 @@ bool EncryptAES256(const SecureString& sKey, const SecureString& sPlaintext, con
131127 sCiphertext .resize (nCLen);
132128
133129 // Perform the encryption
134- EVP_CIPHER_CTX ctx;
130+ EVP_CIPHER_CTX* ctx;
135131
136132 bool fOk = true ;
137133
138- EVP_CIPHER_CTX_init (& ctx);
139- if (fOk ) fOk = EVP_EncryptInit_ex (& ctx, EVP_aes_256_cbc (), NULL , (const unsigned char *)&sKey [0 ], (const unsigned char *)&sIV [0 ]);
140- if (fOk ) fOk = EVP_EncryptUpdate (& ctx, (unsigned char *)&sCiphertext [0 ], &nCLen, (const unsigned char *)&sPlaintext [0 ], nLen);
141- if (fOk ) fOk = EVP_EncryptFinal_ex (& ctx, (unsigned char *)(&sCiphertext [0 ]) + nCLen, &nFLen);
142- EVP_CIPHER_CTX_cleanup (& ctx);
134+ ctx = EVP_CIPHER_CTX_new ( );
135+ if (fOk ) fOk = EVP_EncryptInit_ex (ctx, EVP_aes_256_cbc (), NULL , (const unsigned char *)&sKey [0 ], (const unsigned char *)&sIV [0 ]);
136+ if (fOk ) fOk = EVP_EncryptUpdate (ctx, (unsigned char *)&sCiphertext [0 ], &nCLen, (const unsigned char *)&sPlaintext [0 ], nLen);
137+ if (fOk ) fOk = EVP_EncryptFinal_ex (ctx, (unsigned char *)(&sCiphertext [0 ]) + nCLen, &nFLen);
138+ EVP_CIPHER_CTX_free ( ctx);
143139
144140 if (!fOk ) return false ;
145141
@@ -172,15 +168,15 @@ bool DecryptAES256(const SecureString& sKey, const std::string& sCiphertext, con
172168
173169 sPlaintext .resize (nPLen);
174170
175- EVP_CIPHER_CTX ctx;
171+ EVP_CIPHER_CTX* ctx;
176172
177173 bool fOk = true ;
178174
179- EVP_CIPHER_CTX_init (& ctx);
180- if (fOk ) fOk = EVP_DecryptInit_ex (& ctx, EVP_aes_256_cbc (), NULL , (const unsigned char *)&sKey [0 ], (const unsigned char *)&sIV [0 ]);
181- if (fOk ) fOk = EVP_DecryptUpdate (& ctx, (unsigned char *)&sPlaintext [0 ], &nPLen, (const unsigned char *)&sCiphertext [0 ], nLen);
182- if (fOk ) fOk = EVP_DecryptFinal_ex (& ctx, (unsigned char *)(&sPlaintext [0 ]) + nPLen, &nFLen);
183- EVP_CIPHER_CTX_cleanup (& ctx);
175+ ctx = EVP_CIPHER_CTX_new ( );
176+ if (fOk ) fOk = EVP_DecryptInit_ex (ctx, EVP_aes_256_cbc (), NULL , (const unsigned char *)&sKey [0 ], (const unsigned char *)&sIV [0 ]);
177+ if (fOk ) fOk = EVP_DecryptUpdate (ctx, (unsigned char *)&sPlaintext [0 ], &nPLen, (const unsigned char *)&sCiphertext [0 ], nLen);
178+ if (fOk ) fOk = EVP_DecryptFinal_ex (ctx, (unsigned char *)(&sPlaintext [0 ]) + nPLen, &nFLen);
179+ EVP_CIPHER_CTX_free ( ctx);
184180
185181 if (!fOk ) return false ;
186182
0 commit comments