Why is the value of the last 16 bytes of the ciphertext encrypted by OpenSSL aes_128_ecb padding witch PKCS7 not in the range of 1- AES_BLOCK_SIZE(16)?
The plainText is “1234567890123456”,16bytes. The key is “aaaaaaaaaaaaaaa”, 16bytes. using aes_128_ecb algorithm and set the padding type to PKCS7. But why is the last block size bytes value not in range 1-16? And some value is greater than AES_BLOCK_SIZE 16? Thanks for your help!
std::string _in = "1234567890123456";
std::string _key = "aaaaaaaaaaaaaaaa";
std::string _out;
int outl = 0;
int out_tl = 0;
_out.resize(_in.size() + AES_BLOCK_SIZE, 0);
EVP_CIPHER_CTX* ctx = EVP_CIPHER_CTX_new();
EVP_EncryptInit_ex(ctx, EVP_aes_128_ecb(), NULL, (unsigned char*)_key.data(), NULL);
EVP_CIPHER_CTX_set_padding(ctx, EVP_PADDING_PKCS7);
EVP_EncryptUpdate(ctx, (unsigned char*)_out.data(), &outl, (unsigned char*)_in.data(), _in.length());
out_tl += outl;
EVP_EncryptFinal_ex(ctx, (unsigned char*)_out.data() + outl, &outl);
out_tl += outl;
_out.resize(out_tl);
printf("total size: %dn", out_tl);
for(int i=_out.size() - AES_BLOCK_SIZE;i<_out.size();i++){
printf("index: %d, %dn", i, *(unsigned char*)(_out.data() + i));
}
EVP_CIPHER_CTX_free(ctx);
And the out result is bellow:
total size: 32
index: 16, 201
index: 17, 5
index: 18, 106
index: 19, 234
index: 20, 164
index: 21, 87
index: 22, 26
index: 23, 91
index: 24, 48
index: 25, 145
index: 26, 142
index: 27, 13
index: 28, 154
index: 29, 25
index: 30, 123
index: 31, 151
Wong Victor is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.