opc0de
opc0de

Reputation: 11767

Decrypt AES string

I am using the openssl library in order to decrypt some raw strings that come from a device.

The encryption that the device is using is AES - 128 bit.

Here is my code :

unsigned char *aes_decrypt(EVP_CIPHER_CTX *e, unsigned char *ciphertext, int *len)
{
  int p_len = *len, f_len = 0;
  unsigned char *plaintext = new unsigned char [p_len + 128];
  memset(plaintext,0,p_len + 128);
  syslog(LOG_NOTICE,"P_LEN BEFORE: %d",p_len);
  EVP_DecryptInit_ex(e, NULL, NULL, NULL, NULL);
  EVP_DecryptUpdate(e, plaintext, &p_len, ciphertext, *len);
  EVP_DecryptFinal_ex(e, plaintext+p_len, &f_len);

  syslog(LOG_NOTICE,"P_LEN : %d",p_len);
  syslog(LOG_NOTICE,"F_LEN : %d",f_len);
  *len = p_len + f_len;

  syslog(LOG_NOTICE,"MARIMEA ESTE %d",*len);
  return plaintext;
}

My questions are :

  1. Is the encrypted string length equal to the decrypted string length? (in AES 128 bit)

  2. If f_len represents the decrypted amount of bytes (correct me if I am wrong) then why is it smaller than the actual data decrypted?

Thanks

Upvotes: 0

Views: 547

Answers (1)

SKi
SKi

Reputation: 8466

AES-128 is a block cipher. Block size is 128 bits (16 bytes). So length of ciphertext is always a multiple of 16 bytes. So the ciphertext can be bigger than plaintext.

EDIT:

Answers:

  1. No, encrypted data can be bigger or equal length.
  2. Wrong, f_len does represents the decrypted amount of bytes. (p_len + f_len) does it.

Upvotes: 4

Related Questions