This is half of it, as it only encodes text.
Credit to @FloatingMuffins because I just found this script in one of their projects. I modified it slightly, though.