From 07193f5ac199b73f2e910c8ed652a72e2d3af680 Mon Sep 17 00:00:00 2001 From: srdusr Date: Tue, 14 Mar 2023 07:32:01 +0200 Subject: Fixed deprecated 'AES_set_encrypt_key' and etc with EVP functions --- main.c | 34 +++++++++++++++++++++++++--------- 1 file changed, 25 insertions(+), 9 deletions(-) (limited to 'main.c') diff --git a/main.c b/main.c index 69f2eb9..0d8c426 100644 --- a/main.c +++ b/main.c @@ -3,6 +3,7 @@ #include #include #include +#include #define MAX_PASSWORD_LENGTH 128 #define AES_KEY_SIZE 256 / 8 @@ -37,14 +38,20 @@ void encrypt_password(struct Password *password, unsigned char *key) { RAND_bytes(iv, AES_BLOCK_SIZE); // Initialize the encryption key - AES_set_encrypt_key(key, AES_KEY_SIZE, &aes_key); + EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new(); + EVP_EncryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, key, iv); // Encrypt the password using AES-256 encryption - AES_cbc_encrypt(password->password, encrypted_password, AES_BLOCK_SIZE, &aes_key, iv, AES_ENCRYPT); + int outlen, tmplen; + EVP_EncryptUpdate(ctx, encrypted_password, &outlen, (unsigned char*)password->password, AES_BLOCK_SIZE); + EVP_EncryptFinal_ex(ctx, encrypted_password + outlen, &tmplen); // Copy the encrypted password and IV back into the password struct memcpy(password->password, encrypted_password, AES_BLOCK_SIZE); memcpy(password->notes, iv, AES_BLOCK_SIZE); + + // Clean up + EVP_CIPHER_CTX_free(ctx); } void decrypt_password(struct Password *password, unsigned char *key) { @@ -60,14 +67,14 @@ void decrypt_password(struct Password *password, unsigned char *key) { EVP_DecryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, key, iv); // Decrypt the password using AES-256 decryption - int outlen; - EVP_DecryptUpdate(ctx, decrypted_password, &outlen, password->password, AES_BLOCK_SIZE); - int finallen; - EVP_DecryptFinal_ex(ctx, decrypted_password + outlen, &finallen); + int outlen, tmplen; + EVP_DecryptUpdate(ctx, decrypted_password, &outlen, (unsigned char*)password->password, AES_BLOCK_SIZE); + EVP_DecryptFinal_ex(ctx, decrypted_password + outlen, &tmplen); // Copy the decrypted password back into the password struct memcpy(password->password, decrypted_password, MAX_PASSWORD_LENGTH); + // Clean up EVP_CIPHER_CTX_free(ctx); } @@ -105,8 +112,12 @@ void load_password(struct Password *password, char *filename, unsigned char *key // Decrypt the password information AES_KEY aes_key; - AES_set_decrypt_key(key, 256, &aes_key); - AES_cbc_encrypt(encrypted, (unsigned char*)password, sizeof(struct Password), &aes_key, iv, AES_DECRYPT); + EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new(); + EVP_DecryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, key, iv); + int outlen, finallen; + EVP_DecryptUpdate(ctx, (unsigned char*)password, &outlen, encrypted, sizeof(struct Password)); + EVP_DecryptFinal_ex(ctx, ((unsigned char*)password) + outlen, &finallen); + EVP_CIPHER_CTX_free(ctx); // Close the file fclose(file); @@ -120,7 +131,12 @@ int main() { // Get the encryption key from the user printf("Enter the encryption key: "); - fgets(key, AES_BLOCK_SIZE, stdin); + char key_str[AES_BLOCK_SIZE]; + printf("Enter the encryption key: "); + fgets(key_str, AES_BLOCK_SIZE, stdin); + for (int i = 0; i < AES_BLOCK_SIZE; i++) { + key[i] = (unsigned char) key_str[i]; + } // Loop until the user chooses to exit the program do { -- cgit v1.2.3