python3 常见解密加密算法

来源:互联网 发布:s7200模拟量输出编程 编辑:程序博客网 时间:2024/06/05 04:39

Base64编码,64指A-Z、a-z、0-9、+和/这64个字符,还有“=”号不属于编码字符,而是填充字符。

优点:方法简单

缺点:不保险,别人拿到密文可以自己解密出明文

编码原理:将3个字节转换成4个字节((3 X 8)=24=(4X6)),先读入3个字节,每读一个字节,左移8位,再右移四次,每次6位,这样就有4个字节了。
解码原理:将4个字节转换成3个字节,先读入4个6位(用或运算),每次左移6位,再右移3次,每次8位,这样就还原了。

Python3中base64模块与Python2使用方法有了明显的不一样,接下来简要介绍下base64模块。

Python 3.5.2+ (default, Aug  5 2016, 08:07:14)
[GCC 6.1.1 20160724] on linux
Type "help", "copyright", "credits" or "license" for more information.

首先导入base64模块

>>> import base64
>>> my_str='hello'

然后把字符串转码为UTF-8格式:

>>> utf_str=my_str.encode(encoding="utf-8")
>>> utf_str
b'hello'

我们试着用Base64方式加密:

>>> word=base64.b64encode(utf_str)
>>> word
b'aGVsbG8='

最后用Base64方式解密:

>>> hello=base64.b64decode(word.decode())
>>> hello
b'hello'
>>> hello.decode()
'hello'
可以看到,已经成功解密出来!



0 0
原创粉丝点击