All pastes #783355 Raw Edit

Base64 in Python

public python v1 · immutable
·published 2007-11-19 05:00 UTC
rendered paste body
## b64enc.py - encoder#import systbl = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"print "Type in a line, then press ENTER."l = sys.stdin.readline()l = l[:-1]f = 0p = 0for c in l:	if p == 0:		f |= ord(c)		p = 1	elif p == 1:		f |= ord(c)<<2		p = 2	else:		f |= ord(c)<<4		p = 0	sys.stdout.write(tbl[f & 0x3F])	f >>= 6	if p == 0:		sys.stdout.write(tbl[f & 0x3F])		f >>= 6sys.stdout.write(tbl[f & 0x3F])print ""## b64dec.py - decoder#import sysrtbl = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"print "Type in a line, then press ENTER."l = sys.stdin.readline()l = l[:-1]f = 0p = 0for c in l:	i = 0	while rtbl[i] != c and i < len(rtbl):		i += 1	if p == 0:		f = i		p = 1	elif p == 1:		f |= i<<6		p = 2	elif p == 2:		f |= i<<4		p = 3	else:		f |= i<<2		p = 0	if p != 1:		sys.stdout.write(chr(f & 0xFF))		f >>= 8print ""