Good morning everyone,
Thanks to this conversation, I was able to download the Yugi source code for implementing the AES algorithm.
http://www.pctoledo.com.br/forum/viewtopic.php?f=4&t=17146Unfortunately, due to the use of Portuguese for both function and variable names, I had difficulty reading it and, with the help of AI, I decided to translate it into English.
Once finished, I decided to make some improvements and share the new source code, including the original.
The following changes were made:
- Translated all function and variable names from Portuguese to English. Where appropriate, some names were replaced with more descriptive ones.
- Unified all functions performing the same task with different keys (e.g., the functions cifraArquivo(), cifraArquivo192(), and cifraArquivo256() were replaced by the function EncryptFile()).
- Added functions to encrypt/decrypt text in CBC and CTR modes. ECB mode was retained only for educational purposes, but its use is strongly discouraged unless the data is non-sensitive.
- Added CTR mode and an HMAC authentication tag to file encryption/decryption.
- Declared all internal functions (not callable from outside this source) as STATIC.
- Removed all validation checks except those for externally accessible functions. Since internal functions receive parameters only from other functions within this source, it is assumed they are valid, making further validation unnecessary.
For the same reason, functions performing such checks were removed.
- Removed the initialization function iniciarAES(), which had to be called before other functions.
Accordingly, tables were declared using #DEFINE, and all PUBLIC variables were replaced by LOCAL variables declared exclusively within the functions where needed.
- Padding code present in various functions was replaced with calls to dedicated padding functions.
- Replaced calls to the HB_RANDOM() function (not cryptographically secure) for random IV generation with a function that invokes the Windows API, which is cryptographically secure. In case of API failure, a fallback code written in C is used. Although not as secure as the API, it is still more robust than HB_RANDOM().
- Main functions no longer require fixed-length hexadecimal keys for encryption; now, strings of any length (passwords) are accepted.
The code will convert the input into a valid key for encryption using the DeriveAESKey() function.
It is unreasonable to expect users to input fixed-length hexadecimal keys.
To compile use: hbmk2 -GTWVT Xhb.hbc test aes -lhbnf -lhbmisc -lhbct -static -w3 -RUN
Regards
Translated with DeepL.com (free version)