python-加解密
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python-加解密相关的知识,希望对你有一定的参考价值。
cryptography库既包括高级规则,也包括普通密码算法(如对称密码、消息摘要和关键派生函数)的低级接口。
$ pip install cryptography
例如,用密码学的高层对称加密方式加密:
from cryptography.fernet import Fernet
key = Fernet.generate_key()
f = Fernet(key)
token = f.encrypt(b"hello,world!")
print token
print key
‘...‘
print f.decrypt(token)
gAAAAABbzaV91tfQiIYclT05Kb30uA6-XDKsp13jHx7lpIvML3NHCtJCxqTIgArIqQgTocnsBanR51vDkeh5NcDkN_B2kL2bDA==
tbpwFLqSZFa6Tufae7JW5gqrxGPQFtRCrfl-gyDIuiw=
hello,world!
AES :
import os
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import padding
padder = padding.PKCS7(128).padder()
backend = default_backend()
key = os.urandom(32)
iv = os.urandom(16)
cipher = Cipher(algorithms.AES(key), modes.CBC(iv), backend=backend)
encryptor = cipher.encryptor()
padded_data = padder.update(b"myhaspl")
padded_data += padder.finalize()
print padded_data
ct = encryptor.update(padded_data) + encryptor.finalize()
decryptor = cipher.decryptor()
decryptorData=decryptor.update(ct) + decryptor.finalize()
unpadder = padding.PKCS7(128).unpadder()
data = unpadder.update(decryptorData)
data + unpadder.finalize()
myhaspl
Out[22]:‘myhaspl‘
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Created on Tue Oct 2 13:23:27 2018
@author: myhaspl
@email:[email protected]
@blog:dmyhaspl.github.io
"""
import os
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import padding
padder = padding.PKCS7(128).padder()
backend = default_backend()
key = os.urandom(32)
iv = os.urandom(16)
cipher = Cipher(algorithms.AES(key), modes.CBC(iv), backend=backend)
encryptor = cipher.encryptor()
messages = raw_input("Please input your message: ")
padded_data = padder.update(messages )
padded_data += padder.finalize()
print padded_data
ct = encryptor.update(padded_data) + encryptor.finalize()
decryptor = cipher.decryptor()
decryptorData=decryptor.update(ct) + decryptor.finalize()
unpadder = padding.PKCS7(128).unpadder()
data = unpadder.update(decryptorData)
data + unpadder.finalize()
Please input your message: hello, world,I am myhaspl.
hello, world,I am myhaspl.
Out[24]:
‘hello, world,I am myhaspl.‘
python3:
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Created on Tue Oct 2 13:23:27 2018
@author: myhaspl
@email:[email protected]
"""
import os
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import padding
padder = padding.PKCS7(128).padder()
backend = default_backend()
key = os.urandom(32)
iv = os.urandom(16)
cipher = Cipher(algorithms.AES(key), modes.CBC(iv), backend=backend)
encryptor = cipher.encryptor()
messages = input("Please input your message: ")
messages=bytes(messages,"utf-8")
padded_data = padder.update(messages )
padded_data += padder.finalize()
print(padded_data)
ct = encryptor.update(padded_data) + encryptor.finalize()
decryptor = cipher.decryptor()
decryptorData=decryptor.update(ct) + decryptor.finalize()
unpadder = padding.PKCS7(128).unpadder()
data = unpadder.update(decryptorData)
print(data + unpadder.finalize())
以上是关于python-加解密的主要内容,如果未能解决你的问题,请参考以下文章