use MIME::Base64 qw( encode_base64 decode_base64 ); open( INBIN, '<', 'file.png' ) or die "$!"; open( OUTB64, '>', 'file.b64' ) or die "$!"; binmode( INBIN ); while ( read( INBIN, $buf, 60 * 57 ) ) { print OUTB64 encode_base64( $buf ); } close ( INBIN ); close ( OUTB64 ); open( OUTBIN, '>', 'out.png' ) or die "$!"; open( INB64, '<', 'file.b64' ) or die "$!"; binmode( OUTBIN ); while ( ) { print OUTBIN decode_base64( $_ ); } close ( OUTBIN ); close ( INB64 );