Python 3 标准库 base64 加密与解密算法

#base64 base32 base16 加密与解密
import base64

#加密解密函数
def encrypt_decrypt(string):
	#将字符串转换成bytes 编码格式utf-8
	bytesString = string.encode(encoding='utf-8')
	print(('bytes编码后的字符串: %s') % bytesString)

	#base64编码
	encodeStr = base64.b64encode(bytesString)
	print('base64编码后的bytes类型encodeStr:', encodeStr)
	print('base64编码解码字符串:', encodeStr.decode())

	#base64解码
	decodeStr = base64.b64decode(encodeStr)
	print('base64解码后的字符串:',decodeStr.decode())

	#base32加密
	encodeStr32 = base64.b32encode(bytesString)
	print('base32加密后的bytes类型:', encodeStr32)
	print('base32加密后的字符串:', encodeStr32.decode())

	#base32解码
	decodeStr32 = base64.b32decode(encodeStr32)
	print('base32解码后的字符串:', decodeStr32.decode())

	#base16加密
	encodeStr16 = base64.b16encode(bytesString)
	print('base16加密后的字符串:', encodeStr16.decode())

	#base16解码
	decodeStr16 = base64.b16decode(encodeStr16)
	print('base16解码后的字符串:', decodeStr16.decode())

if __name__ == '__main__':
    #测试数据
	encrypt_decrypt('python3 linux java php7')

猜你喜欢

转载自blog.csdn.net/qq_34777982/article/details/82707089