Skip to main content

Can you search for base64 with grep? [Resolved]

Is it possible to search for Base64 strings with grep? Say I have a list of text files and I want to find all the Base64 strings contained in them; is there any way to search for them? I mean I know they usually end with = or == and they also have sequences of bytes which relate to what letters they are encoded as within the base64 string. But as for searching for them in general (without knowing anything about what is encoded) I don't really know how to do so.


Question Credit: leeand00
Question Reference
Asked September 19, 2019
Tags: grep, base64
Posted Under: Unix Linux
18 views
1 Answers

You can look for strings which are composed only of valid Base64 characters, with the correct structure and padding if necessary:

grep -E '[A-Za-z0-9+/]{4}*([A-Za-z0-9+/]{4}|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)'

This looks for any number of sequences of four Base64 characters, followed by either another four, or three with a single padding character, or two with two padding characters.


credit: Stephen Kitt
Answered September 19, 2019
Your Answer