Many ancient peoples, such as the Greeks and the Chinese, practiced steganography, which means hiding the existence of a message. Methods used ranged from writing the message on the wooden backing of a wax tablet and then covering it with wax; writing the message on fine silk, then scrunching it into a ball covered with wax, and swallowing the ball; and the use of invisible ink, which has been around since the 1st century AD.
But these methods were imperfect, so ancient people also practiced cryptography, which is putting a message into some sort of code so that even if the enemy finds the message, they can’t read it.
Examples from the ancient world:
The Greeks used a device called a scytale as early as the 5th century BC. It’s a wooden staff around which the message sender wraps a leather strip. Unwrapped, the leather strip has a series of letters on it that appear to be gibberish, and must be decoded by someone with another scytale of the correct diameter and construction.
The Kama-Sutra (dating from the 4th century AD) advised women to learn the art of “secret writing,” which was a simple form of cryptography involving letter substitution.
Julius Caesar used cryptography to send military messages. He used a simple substitution cipher where each letter is replaced by the letter three places down from it. So A is D, B is E, etc. This kind of cipher is now called a Caesar shift cipher.
Nowadays, many people can easily break substitution ciphers, and even do it for fun. I like to solve the ciphers in Games Magazine and have been doing it since childhood (although puzzle ciphers are generally easier, because they preserve spaces and punctuation, while real ciphers generally run all the letters together to make them harder to break). But in the ancient world, there was no cryptanalysis, and these simple ciphers were probably effective.
Further reading: The Code Book, by Simon Singh