@@ -62,7 +62,7 @@ EVP_PKEY* Encryption::loadPublicKey(const std::string& filename) {
62
62
* @param publicKey The public key used for encryption
63
63
* @return std::string Encrypted session key
64
64
*/
65
- std::string Encryption::encryptSessionKey (const unsigned char * sessionKey, size_t keySize , EVP_PKEY* publicKey) {
65
+ std::string Encryption::encryptSessionKey (std::vector< unsigned char >& sessionKey, EVP_PKEY* publicKey) {
66
66
EVP_PKEY_CTX* ctx = EVP_PKEY_CTX_new (publicKey, NULL );
67
67
if (!ctx) {
68
68
std::cerr << " Failed to create EVP_PKEY_CTX" << std::endl;
@@ -82,14 +82,15 @@ std::string Encryption::encryptSessionKey(const unsigned char* sessionKey, size_
82
82
}
83
83
84
84
size_t outLen;
85
- if (EVP_PKEY_encrypt (ctx, NULL , &outLen, sessionKey, keySize) <= 0 ) {
85
+ size_t keySize = sessionKey.size ();
86
+ if (EVP_PKEY_encrypt (ctx, NULL , &outLen, sessionKey.data (), keySize) <= 0 ) {
86
87
std::cerr << " EVP_PKEY_encrypt (determine length) failed" << std::endl;
87
88
EVP_PKEY_CTX_free (ctx);
88
89
return " " ;
89
90
}
90
91
91
92
std::vector<unsigned char > out (outLen);
92
- if (EVP_PKEY_encrypt (ctx, out.data (), &outLen, sessionKey, keySize) <= 0 ) {
93
+ if (EVP_PKEY_encrypt (ctx, out.data (), &outLen, sessionKey. data () , keySize) <= 0 ) {
93
94
std::cerr << " EVP_PKEY_encrypt failed" << std::endl;
94
95
EVP_PKEY_CTX_free (ctx);
95
96
return " " ;
@@ -133,15 +134,15 @@ std::string Encryption::base64Encode(const unsigned char* buffer, size_t length)
133
134
* @return std::string The encrypted ciphertext.
134
135
* Returns an empty string if there is an error during encryption.
135
136
*/
136
- std::string Encryption::encryptData (const std::string& plaintext, const unsigned char * sessionKey, const unsigned char * iv) {
137
+ std::string Encryption::encryptData (const std::string& plaintext, std::vector< unsigned char >& sessionKey, const unsigned char * iv) {
137
138
EVP_CIPHER_CTX* ctx = EVP_CIPHER_CTX_new ();
138
139
if (!ctx) {
139
140
std::cerr << " Failed to create EVP_CIPHER_CTX" << std::endl;
140
141
return " " ;
141
142
}
142
143
143
144
// Initialize the encryption operation with AES-128-CBC
144
- if (EVP_EncryptInit_ex (ctx, EVP_aes_128_cbc (), NULL , sessionKey, iv) != 1 ) {
145
+ if (EVP_EncryptInit_ex (ctx, EVP_aes_128_cbc (), NULL , sessionKey. data () , iv) != 1 ) {
145
146
std::cerr << " EVP_EncryptInit_ex failed" << std::endl;
146
147
EVP_CIPHER_CTX_free (ctx);
147
148
return " " ;
@@ -189,8 +190,8 @@ bool Encryption::encryptFile(const std::string& publicKeyFile, std::string& file
189
190
190
191
OpenSSL_add_all_algorithms ();
191
192
ERR_load_crypto_strings ();
192
- unsigned char sessionKey[ sessionKeySize] ;
193
- generateSessionKey (sessionKey, sizeof ( sessionKey));
193
+ std::vector< unsigned char > sessionKey ( sessionKeySize) ;
194
+ generateSessionKey (sessionKey. data (), sessionKey. size ( ));
194
195
195
196
// load public key
196
197
EVP_PKEY* publicKey = loadPublicKey (publicKeyFile);
@@ -211,7 +212,7 @@ bool Encryption::encryptFile(const std::string& publicKeyFile, std::string& file
211
212
file.close ();
212
213
213
214
// Encrypt session key
214
- std::string encryptedSessionKey = encryptSessionKey (sessionKey, sizeof (sessionKey), publicKey);
215
+ std::string encryptedSessionKey = encryptSessionKey (sessionKey, publicKey);
215
216
if (encryptedSessionKey.empty ()) {
216
217
EVP_PKEY_free (publicKey);
217
218
return false ;
0 commit comments