fork download
  1. #include <stdio.h>
  2. #include <stdint.h>
  3. #include <string.h>
  4.  
  5. #define AES_BLOCK_SIZE 16
  6.  
  7. // Example S-box for AES substitution step
  8. static const uint8_t sbox[256] = {
  9. 0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5,
  10. 0x30, 0x01, 0x67, 0x2b, 0xfe, 0xd7, 0xab, 0x76,
  11. // (Rest omitted for brevity. In a full AES, include all 256 values.)
  12. };
  13.  
  14. // Perform a single byte substitution using the S-Box
  15. void subBytes(uint8_t *block) {
  16. for (int i = 0; i < AES_BLOCK_SIZE; i++) {
  17. block[i] = sbox[block[i]];
  18. }
  19. }
  20.  
  21. // Simple AES encryption function (1-round example for simplicity)
  22. void aesEncrypt(uint8_t *plaintext, uint8_t *key, uint8_t *ciphertext) {
  23. for (int i = 0; i < AES_BLOCK_SIZE; i++) {
  24. ciphertext[i] = plaintext[i] ^ key[i]; // XOR plaintext with key
  25. }
  26. subBytes(ciphertext); // Substitute bytes using S-Box
  27. }
  28.  
  29. // Simple AES decryption function (inverse operations)
  30. void aesDecrypt(uint8_t *ciphertext, uint8_t *key, uint8_t *plaintext) {
  31. for (int i = 0; i < AES_BLOCK_SIZE; i++) {
  32. plaintext[i] = ciphertext[i] ^ key[i]; // XOR ciphertext with key
  33. }
  34. // Substitute bytes using inverse S-Box (omitted for brevity)
  35. }
  36.  
  37. void printHex(const char *label, uint8_t *data, size_t length) {
  38. printf("%s", label);
  39. for (size_t i = 0; i < length; i++) {
  40. printf("%02x ", data[i]);
  41. }
  42. printf("\n");
  43. }
  44.  
  45. int main() {
  46. // Example plaintext and key (16 bytes each)
  47. uint8_t plaintext[AES_BLOCK_SIZE] = "This is AES!";
  48. uint8_t key[AES_BLOCK_SIZE] = "simplekey123456"; // 16-byte key
  49. uint8_t ciphertext[AES_BLOCK_SIZE] = {0};
  50. uint8_t decrypted[AES_BLOCK_SIZE] = {0};
  51.  
  52. // Encrypt the plaintext
  53. aesEncrypt(plaintext, key, ciphertext);
  54. printHex("Encrypted data: ", ciphertext, AES_BLOCK_SIZE);
  55.  
  56. // Decrypt the ciphertext
  57. aesDecrypt(ciphertext, key, decrypted);
  58. printHex("Decrypted data: ", decrypted, AES_BLOCK_SIZE);
  59.  
  60. return 0;
  61. }
  62.  
Success #stdin #stdout 0s 5272KB
stdin
Standard input is empty
stdout
Encrypted data: 00 7c f2 7b 00 fe 00 00 00 00 00 00 00 00 00 63 
Decrypted data: 73 15 9f 0b 6c 9b 6b 65 79 31 32 33 34 35 36 63