OpenSSL block ciphers return length of the text as output of the encryption (envelope_seal()), and If I have to send the length over network, I append the length with the ciphertext as I can transmit only one field and envelope_open() needs the length of the ciphertext. Is there any other way that I can calculate the length of the ciphertext, which contains null terminated characters, on the receiving side?


1 Answer 1


Most binary network protocols are already bounded. So if you just send the ciphertext (or the IV and the ciphertext) then the length will be known by the transport mechanism. Otherwise the method of communicating the length is entirely up to your imagination as long as you can map it to a presentation that is acceptable to your transport protocol.

Null termination characters within ciphertext or tag are not an issue for binary connections; null termination characters are only an issue for C-style null terminated strings. You don't want to send binary data directly over a text-based connection; you need a form of encoding such as base 64 encoding if you want to do that. The output size of base 64 encoding is determined during decoding, although the base 64 input must of course be bounded as well.

Note that you need some form of integrity protection of the IV and ciphertext if you want to securely send/receive ciphertext. So you need an authenticated mode of encryption or an authentication tag created by a MAC.

  • $\begingroup$ But how to calculate length of null terminated string? I am using Base64 while transporting. I need to calculate the length. My decryption fails randomly if i use strlen on unsigned char as it is null terminated c-style string. $\endgroup$
    – Kahn
    Feb 8, 2015 at 2:17
  • $\begingroup$ It's not a (textual) string, so don't treat it like one. You need either to have the length in advance (e.g. given to you during base 64 decode) or you need to encode the length yourself. As the octet string (also known as byte array) can have any value anywhere, you cannot determine the length from the content. $\endgroup$
    – Maarten Bodewes
    Feb 8, 2015 at 3:18
  • $\begingroup$ I have length while encoding, can I get this length back while decoding? If yes, please let me know how? $\endgroup$
    – Kahn
    Feb 8, 2015 at 10:12
  • $\begingroup$ I don't know what kind of function you are currently using to decode. What about the len in len = BIO_read(bio, *buffer, strlen(b64message)); ? $\endgroup$
    – Maarten Bodewes
    Feb 8, 2015 at 10:57
  • $\begingroup$ wiki.openssl.org/index.php/… See this link for symmetric Enc $\endgroup$
    – Kahn
    Feb 8, 2015 at 11:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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