Python常用加密方式-加盐加密-单向加密-解密
Python 常用后端密码加密方式
1.单向加密-不可解密-除非撞库
实现思路:
在存储用户信息的表中,增加passwod字段,salt字段
passsword:加密后的密文
salt:由加密时,随机生成的盐字段
当用户修改密码时或管理员给重置密码时,此时随机生成盐salt,与新密码进行加密,只需要将盐和密文存储到数据库,这种方法,可以防止反向破解出密码
常用加密hmac代码:
digestmod:此参数是加密方式,还有很多种......
def password_new(salt, password, dig='sha1'):
"""
:param salt: 盐
:param password: 加密的字符
:return:
"""
h1 = hmac.new(key.encode(), password.encode(), digestmod=dig)
return h1.hexdigest()
if __name__ == '__main__':
print(password_new('123456','yangbeita'))
sha1 加密
import hashlib
import random
sha1 = hashlib.sha1()
s = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
salt = ""
for i in range(8):
salt+=random.choice(s)
# data = '2333333{salt}'.format(salt=salt)
data = '123456'
sha1.update(data.encode('utf-8'))
sha1_data = sha1.hexdigest()
print(sha1_data)
md5加密
def md5(s):
s = s.encode("utf8")
m = hashlib.md5()
m.update(s)
return m.hexdigest()
2.加密-解密
base64 加密
import base64
str = 'hello world'.encode(encoding='utf-8')
# 加密
encodestr = base64.b64encode(str)
print(encodestr) # b'aGVsbG8gd29ybGQ='
print(encodestr.decode()) # aGVsbG8gd29ybGQ=
# 解密
decodestr = base64.b64decode(encodestr)
print(decodestr) # b'hello world'
print(decodestr.decode()) # hello world
RSA 加密--此方式还有解密,本文不附带
加密
from Crypto.Cipher import DES3
from Crypto import Random
import binascii
key = 'Sixteen byte key'
msg = 'sona si latine loqueris '
iv = Random.new().read(DES3.block_size)
# print(iv)
def des3_encrypt(key, msg):
cipher = DES3.new(key, DES3.MODE_OFB, iv)
msg = iv + cipher.encrypt(msg)
msg = binascii.b2a_hex(msg)
return msg.decode()
s = des3_encrypt(key.encode(), msg.encode())
print(s)
第三方模块加密
from cryptography.fernet import Fernet
import random
s = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
salt = ""
for i in range(32):
salt += random.choice(s)
print(salt.encode())
# 加密
password = Fernet(b'pXVAHabI4HADuM-fyVogxwV5rHRN1pZe-QQ3yM9ZvPg=').encrypt('yang'.encode()).decode()
print(password)
# 解密
b = Fernet(b'pXVAHabI4HADuM-fyVogxwV5rHRN1pZe-QQ3yM9ZvPg=').decrypt("gAAAAABhYUWM11tfqIyVD1DDIIFG65wdw0hkSXTT84nk1r5fohqL6MUKvQgHpQ2skdo60q9TMzhgM4UDehkla9OKnK0NFa2_hA==".encode()).decode()
print(b)
采用AES 对称加密算法
import base64
from Crypto.Cipher import AES
'''
采用AES对称加密算法
'''
# str不是16的倍数那就补足为16的倍数
def add_to_16(value):
while len(value) % 16 != 0:
value += '\0'
return str.encode(value) # 返回bytes
#加密方法
def encrypt_oracle():
# 秘钥
key = '123456'
# 待加密文本
text = 'yang'
# 初始化加密器
aes = AES.new(add_to_16(key), AES.MODE_ECB)
#先进行aes加密
encrypt_aes = aes.encrypt(add_to_16(text))
#用base64转成字符串形式
encrypted_text = str(base64.encodebytes(encrypt_aes), encoding='utf-8') # 执行加密并转码返回bytes
print(encrypted_text.strip()) # 自己加的的strip, 后面会有一个换行符
print(encrypt_aes)
#解密方法
def decrypt_oralce():
# 秘钥
key = '123456'
# 密文
text = 'XUfQ4HigfgageIzlmuldgw=='
# 初始化加密器
aes = AES.new(add_to_16(key), AES.MODE_ECB)
#优先逆向解密base64成bytes
base64_decrypted = base64.decodebytes(text.encode(encoding='utf-8'))
#
decrypted_text = str(aes.decrypt(base64_decrypted),encoding='utf-8') # 执行解密密并转码返回str
print(decrypted_text)
if __name__ == '__main__':
encrypt_oracle()
decrypt_oralce()