The situation with certificates is almost simple - there the border indicates what is expected (certificate or private key).
In OpenPGP armored data, the boundary line also tells you if the key or data is expected.
SSH keys created by several SSH applications have the same boundary lines, but different key formats. Therefore, you need to try to read the data in all expected formats.
RSA public keys usually have the RSA 1.5 format, so you can assume that you have an RSA key.
PKCS # 12 usually does not fit in PEM (I have never seen such files). The same goes for PKCS8.
PKCS # 7 certificate stores are sometimes PEM encoded, and they have something like BEGIN CERTIFICATE STORAGE in their boundary line.
To summarize - to some extent you can rely on the text of the border line, but this does not give you a 100% guarantee.
source share