This repository showcases a concise implementation of the AES-256 ECB (Electronic Codebook) encryption algorithm in C, renowned for its heightened security.
- AES-256 ECB: Implements secure AES-256 ECB encryption mode.
- Fixed Block Size: Enforces a 16-byte fixed block size without padding.
- Compatibility: Encrypted data is interoperable with any AES-256 ECB decryption.
Include "AES_256_ECB.h" before using AES-256 ECB functions.
#include "AES_256_ECB.h"
AES_CTX ctx;
Initialize the AES context for encryption.
void AES_EncryptInit(AES_CTX *ctx, const unsigned char *key);
ctx
: AES context structure.key
: Encryption key (32 bytes for AES-256).
Encrypt a single 16-byte block of data.
void AES_Encrypt(AES_CTX *ctx, const unsigned char *plaintext, unsigned char *ciphertext);
ctx
: AES context structure.plaintext
: Input plaintext block (16 bytes).ciphertext
: Output encrypted block (16 bytes).
For multiple blocks, use a loop:
for (unsigned int offset = 0; offset < multiple_block_size; offset += AES_BLOCK_SIZE) {
AES_Encrypt(&ctx, plaintext + offset, ciphertext + offset);
}
Initialize the AES context for decryption.
void AES_DecryptInit(AES_CTX *ctx, const unsigned char *key);
ctx
: AES context structure.key
: Decryption key (32 bytes for AES-256).
Decrypt a single 16-byte block of data.
void AES_Decrypt(AES_CTX *ctx, const unsigned char *ciphertext, unsigned char *plaintext);
ctx
: AES context structure.ciphertext
: Input encrypted block (16 bytes).plaintext
: Output decrypted block (16 bytes).
For multiple blocks, use a loop:
for (unsigned int offset = 0; offset < multiple_block_size; offset += AES_BLOCK_SIZE) {
AES_Decrypt(&ctx, ciphertext + offset, plaintext + offset);
}
Free resources associated with the AES context.
void AES_CTX_Free(AES_CTX *ctx);
ctx
: AES context structure.
#include <stdio.h>
#include "AES_256_ECB.h"
void output(const char *title, const unsigned char *data, unsigned int size) {
printf("%s", title);
for (unsigned int index = 0; index < size; index++) {
printf("%02X", data[index]);
}
printf("\n");
}
int main() {
AES_CTX ctx;
unsigned char key[AES_KEY_SIZE];
unsigned char data[AES_BLOCK_SIZE];
// Initialization and usage example
memset(key, 0x79, AES_KEY_SIZE);
memset(data, 0x79, AES_BLOCK_SIZE);
output("Original: 0x", data, 16);
AES_EncryptInit(&ctx, key);
AES_Encrypt(&ctx, data, data);
output("\nEncrypted: 0x", data, 16);
AES_DecryptInit(&ctx, key);
AES_Decrypt(&ctx, data, data);
output("\nDecrypted: 0x", data, 16);
AES_CTX_Free(&ctx);
return 0;
}
Contributions and feedback are welcome! Open issues or submit pull requests.
This AES-256 ECB implementation is provided under the MIT License.