First I want to encrypt a message with a key using AES cipher. Then hide each byte of the encrypted message inside random pixel and that too by replacing one random component of the pixel completely (either red or green or blue).
I use secureRandom to generate same pixels of image and IV for AES/CBC/PKCS5Padding by giving a seed. Let us say no pixel is generated twice to store the byte.
Now I want to extract it. While extracting, I will generate the same pixels using the secureRandom and with same seed.
What should be the termination condition so that I stop at the last hidden byte's pixel? In the embedding part, the length of the encrypted message is the termination. How can we apply a termination condition for the extraction if possible?
Or else is there any way to embed in such a way that we can extract later from those random pixels?
Just evaluate the length of the ciphertext and store it in the target structure too, but on a fixed location. On decryption phase, read this size information as counter for the number of bytes to retrieve. As an alternative solution you can rely on the fact that a ciphertext is written in multiple blocks of fixed length (which depends on used algorithm and keysize). You can with trail and error try to read and decipher block by block until you got a valid deciphered text or you're out of data.
EDIT:
It's a simple calculation:
Let's say your ciphertext is 1024 bytes long and you use an int for the length information than you need at least 1024 + 4 bytes to store the information. And let's say you hide your ciphertext in 12345 pixel and you store in the first 4 pixel the length info followed by 1024 pixels with ciphertext. If you want to retriev ciphertext you first read in the four bytes to get the length and next length bytes to get the ciphertext. In the given example you read 1028 bytes/pixel, the remaining 11317 pixel / bytes are not of interest.
It's realy that simple, isn't it?