@@ -57,15 +57,13 @@ bool CCrypter::Encrypt(const CKeyingMaterial& vchPlaintext, std::vector<unsigned
57
57
int nCLen = nLen + AES_BLOCK_SIZE, nFLen = 0 ;
58
58
vchCiphertext = std::vector<unsigned char >(nCLen);
59
59
60
- EVP_CIPHER_CTX ctx;
61
-
62
60
bool fOk = true ;
63
61
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);
69
67
70
68
if (!fOk ) return false ;
71
69
@@ -84,15 +82,13 @@ bool CCrypter::Decrypt(const std::vector<unsigned char>& vchCiphertext, CKeyingM
84
82
85
83
vchPlaintext = CKeyingMaterial (nPLen);
86
84
87
- EVP_CIPHER_CTX ctx;
88
-
89
85
bool fOk = true ;
90
86
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);
96
92
97
93
if (!fOk ) return false ;
98
94
@@ -131,15 +127,15 @@ bool EncryptAES256(const SecureString& sKey, const SecureString& sPlaintext, con
131
127
sCiphertext .resize (nCLen);
132
128
133
129
// Perform the encryption
134
- EVP_CIPHER_CTX ctx;
130
+ EVP_CIPHER_CTX* ctx;
135
131
136
132
bool fOk = true ;
137
133
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);
143
139
144
140
if (!fOk ) return false ;
145
141
@@ -172,15 +168,15 @@ bool DecryptAES256(const SecureString& sKey, const std::string& sCiphertext, con
172
168
173
169
sPlaintext .resize (nPLen);
174
170
175
- EVP_CIPHER_CTX ctx;
171
+ EVP_CIPHER_CTX* ctx;
176
172
177
173
bool fOk = true ;
178
174
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);
184
180
185
181
if (!fOk ) return false ;
186
182
0 commit comments