There is no guaranteed way to say, but in practice, you can do two things:
check for multiple characters other than ascii (if you expect people to send text in English).
check the distribution of values. in plain text, some letters are much more common than others. but in ciphertext all characters are about the same.
an easy way to do the latter is to see if a character occurs more than (N / 256) + 5 * sqrt (N / 256) times (where you have only N characters), in which case it is probably a natural language (unencrypted) .
in python (referring to the logic above to give "true" when encrypted):
def encrypted(text): scores = defaultdict(lambda: 0) for letter in text: scores[letter] += 1 largest = max(scores.values()) average = len(text) / 256.0 return largest < average + 5 * sqrt(average)
math comes from the average, which is a Gaussian distribution over the average, with a variance equal to the average - this is not ideal, but probably pretty close. by default (with a small amount of text when it is unreliable), it will return false (sorry, I used to have the wrong version with "max ()", in which the logic for small numbers was wrong).
source share