Well, it wasn't obvious from your post that you used binmode().
I am not that familiar with Base64 encoding algorithm and I don't know if the code that you posted actually works; What I do know is that it's slow - heavy use of regexes and even one s///e where simple substr() would probably suffice.
Do you have shell access? If so, download and install
MIME::Base64. You do not need any special privileges if you install it into your home directory.
--perlplexer