当前位置:   article > 正文

python中base64库用法详解_python base64

python base64

1、什么是Base64

        Base64是一种用64个字符来表示任意二进制数据的方法。

        Base64是一种基于64个可打印字符来表示二进制数据的表示方法。由于2^6=64,所以每6个比特为一个单元,对应某个可打印字符。

        3个字节有24个比特,对应于4个Base64单元,即3个字节可由4个可打印字符来表示。在Base64中的可打印字符包括字母A-Z、a-z、数字0-9,这样共有62个字符,此外两个可打印符号在不同的系统中而不同。

2、python中的base64模块

Base64模块真正用得上的方法只有8个,分别是:

encode, decode为一组, 专门用来编码和解码文件的, 也可以对StringIO里的数据做编解码;

encodestring, decodestring为一组,专门用来编码和解码字符串

b64encode, b64decode为一组, 用来编码和解码字符串,并且有一个替换符号字符的功能

因为Base64编码后的字符除了英文字母和数字外还有三个字符’ + / =‘,其中’=‘只是为了补全编码后的字符数为4的整数,而’+‘和’/‘在一些情况下需要被替换的,b64encode和b64decode正是提供了这样的功能。至于什么情况下’+‘和’/'需要被替换,最常见的就是对url进行Base64编码的时候。

urlsafe_b64decode, urlsafe_b64encode为一组,这个就是用来专门对url进行Base64编解码的,实际上也是调用的前一组函数。

base64.b64encode()将bytes类型数据进行base64编码,返回编码后的bytes类型

base64.b64deocde()将base64编码的bytes类型进行解码,返回解码后的bytes类型

decode的作用是将其他编码的字符串转换成unicode编码

encode的作用是将unicode编码转换成其他编码的字符串

3、Base64使用场景

        Base64是一种任意二进制到文本字符串的编码方法,常用于在URL、Cookie、网页中传输少量二进制数据,包括MIME的电子邮件及XML的一些复杂数据。

在邮件中的用途:

        在MIME格式的电子邮件中,base64可以用来将binary的字节序列数据编码成ASCII字符序列构成的文本。使用时,在传输编码方式中指定base64。使用的字符包括大小写字母各26个,加上10个数字,和加号“+”,斜杠“/”,一共64个字符,等号“=”用来作为后缀用途。

在URL中的用途:

        标准的Base64并不适合直接放在URL里传输,因为URL编码器会把标准Base64中的“/”和“+”字符变为形如“%XX”的形式,而这些“%”号在存入数据库时还需要再进行转换,因为ANSI SQL中已将“%”号用作通配符。

        为解决此问题,可采用一种用于URL的改进Base64编码,它不在末尾填充'='号,并将标准Base64中的“+”和“/”分别改成了“*”和“-”,这样就免去了在URL编解码和数据库存储时所要作的转换,避免了编码信息长度在此过程中的增加,并统一了数据库、表单等处对象标识符的格式。

        另有一种用于正则表达式的改进Base64变种,它将“+”和“/”改成了“!”和“-”,因为“+”,“*”在正则表达式中都可能具有特殊含义。

4、base64转换过程

5、代码实现

5.1 简单使用

 示例代码1:

  1. import base64
  2. s = '人生苦短,我学python!'
  3. # 加密
  4. b = base64.b64encode(s.encode('utf-8'))
  5. print(b)
  6. # 解密
  7. c = base64.b64decode(b).decode('utf-8')
  8. print(c)

运行结果:

 示例代码2:

  1. from base64 import b64encode, b64decode
  2. s = 'I love python!'
  3. print(s)
  4. s_encode = s.encode(encoding='utf-8')
  5. print(s_encode)
  6. s_encode_base64 = b64encode(s_encode)
  7. print(s_encode_base64)
  8. s_decode_base64 = b64decode(s_encode_base64)
  9. print(s_decode_base64)
  10. s_decode = s_decode_base64.decode(encoding='utf-8')
  11. print(s_decode)

运行结果:

5.2 url使用base64

示例代码:

  1. import base64
  2. url = 'http://www.baidu.com/info'
  3. # 加密
  4. b = base64.b64encode(url.encode('utf-8'))
  5. print(b)
  6. # 解密
  7. c = base64.b64decode(b).decode('utf-8')
  8. print(c)

运行结果:

 5.3 读写文件

示例代码1:

  1. from base64 import b64encode, b64decode
  2. with open(r'./text.txt', 'rb') as f: # 此处不能使用encoding='utf-8', 否则报错
  3. base64_data = b64encode(f.read()) # b64encode是编码
  4. print(base64_data) # 输出生成的base64码,Bytes类型, b'xxxxx'类型的字符串
  5. new_data = b64decode(base64_data)
  6. print(new_data)

运行结果:

5.4 base64对图片数据进行加密

示例代码:

  1. import base64
  2. # 读取一张图片,将图片数据转换为base64格式
  3. read_gif = open('./run.gif', 'rb')
  4. read_data = read_gif.read()
  5. print(read_data)
  6. read_gif.close()
  7. base_gif = base64.b64encode(read_data)
  8. print(base_gif)
  9. # data = base_gif.decode('ascii')
  10. data = base_gif.decode()
  11. print(data)
  12. new_data = base64.b64decode(base_gif)
  13. print(new_data)
  14. # 将读取的数据重新写入文件,保存为图片格式
  15. new_run = open('new_run.gif', 'wb')
  16. new_run.write(new_data)
  17. new_run.close()

运行结果:

5.5 base64嵌套加解密

 示例代码:

  1. import base64
  2. import re
  3. html = """
  4. <html>
  5. <head>
  6. <title>My Page Title({0})</title>
  7. </head>
  8. <body>
  9. <h1>This is a Heading({1})</h1>
  10. <p>This is a paragraph({2}).</p>
  11. </body>
  12. </html>
  13. """
  14. html_list = []
  15. # 对字符串嵌套加密
  16. print('开始加密:')
  17. for i in range(2):
  18. new_html = html.format(i, i, i)
  19. title = re.findall(r'<title>(.*?)</title>', new_html)[0]
  20. h1 = re.findall(r'<h1>(.*?)</h1>', new_html)[0]
  21. p = re.findall(r'<p>(.*?)</p>', new_html)[0]
  22. new_html = new_html.replace(title, '{0}').replace(h1, '{1}').replace(p, '{2}')
  23. new_html = new_html.format(base64.b64encode(title.encode('utf-8')).decode('utf-8'),
  24. base64.b64encode(h1.encode('utf-8')).decode('utf-8'),
  25. base64.b64encode(p.encode('utf-8')).decode('utf-8'))
  26. html_list.append(base64.b64encode(new_html.encode('utf-8')).decode('utf-8'))
  27. print(new_html)
  28. print('开始解密:')
  29. # 对字符串嵌套解密
  30. for data in html_list:
  31. html_data = base64.b64decode(data).decode('utf-8')
  32. title = re.findall(r'<title>(.*?)</title>', html_data)[0]
  33. h1 = re.findall(r'<h1>(.*?)</h1>', html_data)[0]
  34. p = re.findall(r'<p>(.*?)</p>', html_data)[0]
  35. html_data = html_data.replace(title, '{0}').replace(h1, '{1}').replace(p, '{2}')
  36. html_data = html_data.format(base64.b64decode(title).decode('utf-8'),
  37. base64.b64decode(h1).decode('utf-8'),
  38. base64.b64decode(p).decode('utf-8'))
  39. print(html_data)

运行结果;

  1. 开始加密:
  2. <html>
  3. <head>
  4. <title>TXkgUGFnZSBUaXRsZSgwKQ==</title>
  5. </head>
  6. <body>
  7. <h1>VGhpcyBpcyBhIEhlYWRpbmcoMCk=</h1>
  8. <p>VGhpcyBpcyBhIHBhcmFncmFwaCgwKS4=</p>
  9. </body>
  10. </html>
  11. <html>
  12. <head>
  13. <title>TXkgUGFnZSBUaXRsZSgxKQ==</title>
  14. </head>
  15. <body>
  16. <h1>VGhpcyBpcyBhIEhlYWRpbmcoMSk=</h1>
  17. <p>VGhpcyBpcyBhIHBhcmFncmFwaCgxKS4=</p>
  18. </body>
  19. </html>
  20. 开始解密:
  21. <html>
  22. <head>
  23. <title>My Page Title(0)</title>
  24. </head>
  25. <body>
  26. <h1>This is a Heading(0)</h1>
  27. <p>This is a paragraph(0).</p>
  28. </body>
  29. </html>
  30. <html>
  31. <head>
  32. <title>My Page Title(1)</title>
  33. </head>
  34. <body>
  35. <h1>This is a Heading(1)</h1>
  36. <p>This is a paragraph(1).</p>
  37. </body>
  38. </html>
  39. Process finished with exit code 0

暂未想到更好的base64嵌套解决方案,读者若有更好的解决方法欢迎留言评论!

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号