0
votes

I am using this c library, and have the following:

#include <stdio.h>
#include <string.h>
#include "aes.h"

int main(int argc, char *argv[]) {

  if (argc < 3) return 1;

  uint8_t *key = argv[1];

  uint8_t *content = argv[2];

  uint8_t iv[16] = { 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f,0x0f,0x0f};

  struct AES_ctx ctx;

  AES_init_ctx_iv(&ctx, key, iv);
  AES_CTR_xcrypt_buffer(&ctx, content, strlen(content));

  printf("%s", (char*) content);
  return 0;
}

it gives an output of random characters when used like this:

.\example key msg
prints <random chars>  

The problem is that the chars given are different each run (with same iv), if I try to decrypt the returned value from above, it won't return the original value

.\example key <random chars>  
prints more random chars

but if i use it like this:

#include <stdio.h>
#include <string.h>
#include "aes.h"

int main(int argc, char *argv[]) {

  if (argc < 3) return 1;

  uint8_t *key = argv[1];

  uint8_t *content = argv[2];

  uint8_t iv[16] = { 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f,0x0f,0x0f};

  struct AES_ctx ctx;

  AES_init_ctx_iv(&ctx, key, iv);
  AES_CTR_xcrypt_buffer(&ctx, content, strlen(content));

  printf("enc: %s\n", (char*) content);

  AES_init_ctx_iv(&ctx, key, iv);
  AES_CTR_xcrypt_buffer(&ctx, content, strlen(content));

  printf("dec: %s", (char*) content);
  return 0;
}

this encrypts and decrypts the value. it gives an output like this:

.\example key msg
enc: Vª≈  // encrypted (this changes every run)
dec: msg // decrypted
  1. why does the encrypted value change each time for the same value+key+iv combination
  2. why does the decryption work in the second example, and not when encrypting and decrypting separately
And what is the correct AES key size?Andrew Henle
CTR padding is used, so any key size is accepteduser17953749