#include <stdio.h>
#include <stdint.h>
#include <string.h>
#define AES_BLOCK_SIZE 16
// Example S-box for AES substitution step
static const uint8_t sbox[256] = {
0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5,
0x30, 0x01, 0x67, 0x2b, 0xfe, 0xd7, 0xab, 0x76,
// (Rest omitted for brevity. In a full AES, include all 256 values.)
};
// Perform a single byte substitution using the S-Box
void subBytes(uint8_t *block) {
for (int i = 0; i < AES_BLOCK_SIZE; i++) {
block[i] = sbox[block[i]];
}
}
// Simple AES encryption function (1-round example for simplicity)
void aesEncrypt(uint8_t *plaintext, uint8_t *key, uint8_t *ciphertext) {
for (int i = 0; i < AES_BLOCK_SIZE; i++) {
ciphertext[i] = plaintext[i] ^ key[i]; // XOR plaintext with key
}
subBytes(ciphertext); // Substitute bytes using S-Box
}
// Simple AES decryption function (inverse operations)
void aesDecrypt(uint8_t *ciphertext, uint8_t *key, uint8_t *plaintext) {
for (int i = 0; i < AES_BLOCK_SIZE; i++) {
plaintext[i] = ciphertext[i] ^ key[i]; // XOR ciphertext with key
}
// Substitute bytes using inverse S-Box (omitted for brevity)
}
void printHex(const char *label, uint8_t *data, size_t length) {
for (size_t i = 0; i < length; i++) {
}
}
int main() {
// Example plaintext and key (16 bytes each)
uint8_t plaintext[AES_BLOCK_SIZE] = "This is AES!";
uint8_t key[AES_BLOCK_SIZE] = "simplekey123456"; // 16-byte key
uint8_t ciphertext[AES_BLOCK_SIZE] = {0};
uint8_t decrypted[AES_BLOCK_SIZE] = {0};
// Encrypt the plaintext
aesEncrypt(plaintext, key, ciphertext);
printHex("Encrypted data: ", ciphertext, AES_BLOCK_SIZE);
// Decrypt the ciphertext
aesDecrypt(ciphertext, key, decrypted);
printHex("Decrypted data: ", decrypted, AES_BLOCK_SIZE);
return 0;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdGRpbnQuaD4KI2luY2x1ZGUgPHN0cmluZy5oPgoKI2RlZmluZSB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