1
$\begingroup$

I'm trying to figure out how padding works by seeing what happens when I try to encrypt a 16 byte file with AES-128-CBC. My understanding was if I encrypt a file of size, say, 28, then there would automatically be 4 bytes of padding to make it 32. But when I encrypted my 16-byte file, the size ended up being 48 bytes. I did research and learned that a single padding block is automatically added to direct multiples in order to distinguish the padding from the plaintext, but if that's so, then why is it 48 bytes and not 32 as well (16 plaintext + 16 padding)?

$\endgroup$
  • 2
    $\begingroup$ Did you specify an IV? Do the first 16 bytes match that, i.e. your system is including the IV in the cipher text? Can you try ECB instead? $\endgroup$ – Rup Feb 11 at 5:29
  • $\begingroup$ possibe duplicate of What is the difference between PKCS#5 padding and PKCS#7 padding $\endgroup$ – kelalaka Feb 11 at 7:28
  • $\begingroup$ In PKCS#7 padding $$paddingSize = n - (messageLenght \bmod n)$$ $\endgroup$ – kelalaka Feb 11 at 7:30
  • $\begingroup$ If you ask with your code in stackoverflow we can find the reason easily. $\endgroup$ – kelalaka Feb 11 at 11:59
  • $\begingroup$ Or are you using the openssl enc command, or an alias (in this case openssl aes-128-cbc)? That doesn't do plain encryption per the specs of AES and CBC; it does password-pased encryption using salt, which is added to the file and changes its size; see crypto.stackexchange.com/questions/3298/… (caveat: my answer) @kelalaka: and for len multiple of blocksize n that formula gives n (one block) as Q says $\endgroup$ – dave_thompson_085 Feb 12 at 7:39
0
$\begingroup$

Is your "-K" in the code in uppercase or lowercase? If it is in lowercase, the encryption will automatically add a salt value into your cipher text, causing it to have more than 32 bytes. CBC will pad it until it reaches 48 bytes later(the multiplication of 16).

| improve this answer | |
$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.