常见的加密方式
1. Base64
1.1 介绍
Base64是一种用64个字符来表示任意二进制数据的方法。base64是一种编码方式而不是加密算法。只是看上去像是加密
Base64使用A-Z
,a-z
,0-9
,+
,/
64个字符实现对数据进行加密
1.2 Python使用
import base64
base64.b64encode(b'sxt')
b'YmluYXJ5AHN0cmluZw=='
base64.b64decode(b'YmluYXJ5AHN0cmluZw==')
2. MD5加密
2.1 介绍
MD5信息摘要算法(英语:MD5 Message-Digest Algorithm),一种被广泛使用的密码散列函数,可以产生出一个128位(16字节)的散列值(hash value),用于确保信息传输完整一致
当我们需要保存某些密码信息以用于身份确认时,如果直接将密码信息以明码方式保存在数据库中,不使用任何保密措施,系统管理员就很容易能得到原来的密码信息,这些信息一旦泄露, 密码也很容易被破译。为了增加安全性,有必要对数据库中需要保密的信息进行加密,这样,即使有人得到了整个数据库,如果没有解密算法,也不能得到原来的密码信息
2.2 Python介绍
import hashlib
def test_md51():
m = hashlib.md5()
m.update(b'sxt')
pwd = m.hexdigest()
print(pwd)
def test_md52():
pwd = hashlib.new('md5', b'sxt').hexdigest()
print(pwd)
def test_md53():
data ='你好'
pwd = hashlib.md5(data.encode(encoding='UTF-8')).hexdigest()
print(pwd)
3. AES/DES
https://pycryptodome.readthedocs.io/en/latest/src/introduction.html
3.1 介绍
DES全称为Data Encryption Standard,即数据加密标准,是一种使用密钥加密的算法。该加密算法是一种对称加密方式,其加密运算、解密运算需要使用的是同样的密钥(一组字符串)即可
注意:
现在用AES这个标准来替代原先先的DES
AES与DES的区别:
- 加密后密文长度不同:
- DES加密后密文长度是8的整数倍
- AES加密后密文长度是16的整数倍
- 应用场景的不同:
- 企业级开发使用DES足够安全
- 如果要求使用AES
使用DES/AES进行数据交互时要求双方都拥有相同的私钥
3.2 安装
pip3 install pycryptodome
3.3 Python使用
DES
import base64
from Crypto.Cipher import DES
class EncryptDate:
def __init__(self, key):
self.key = key.encode("utf-8") # 初始化**
self.length = DES.block_size # 初始化数据块大小
self.aes = DES.new(self.key, DES.MODE_ECB) # 初始化AES,ECB模式的实例
def pad(self, text):
"""
#填充函数,使被加密数据的字节码长度是block_size的整数倍
"""
# count = len(text.encode('utf-8'))
# add = self.length - (count % self.length)
# entext = text + (chr(add) * add)
# return entext
args = text.encode('utf-8')
while len(args) % DES.block_size != 0:
args += b'\x00'
return args.decode()
def encrypt(self, encrData): # 加密函数
res = self.aes.encrypt(self.pad(encrData).encode("utf8"))
msg = str(base64.b64encode(res), encoding="utf8")
return msg
def decrypt(self, decrData): # 解密函数
res = base64.decodebytes(decrData.encode("utf8"))
msg = self.aes.decrypt(res).decode("utf8")
return msg
eg = EncryptDate("12345678") # 这里**的长度必须是16的倍数
data = {"name": "sxt123", "realname": "尚学堂", "sex": "1"}
res = eg.encrypt(str(data))
print(res)
print(eg.decrypt(res))
AES
import base64
from Crypto.Cipher import AES
class EncryptDate:
def __init__(self, key):
self.key = key.encode("utf-8") # 初始化**
self.length = AES.block_size # 初始化数据块大小
self.aes = AES.new(self.key, AES.MODE_ECB) # 初始化AES,ECB模式的实例
def pad(self, text):
"""
#填充函数,使被加密数据的字节码长度是block_size的整数倍
"""
# count = len(text.encode('utf-8'))
# add = self.length - (count % self.length)
# entext = text + (chr(add) * add)
# return entext
args = text.encode('utf-8')
while len(args) % AES.block_size != 0:
args += b'\x00'
return args.decode()
def encrypt(self, encrData): # 加密函数
res = self.aes.encrypt(self.pad(encrData).encode("utf8"))
msg = str(base64.b64encode(res), encoding="utf8")
return msg
def decrypt(self, decrData): # 解密函数
res = base64.decodebytes(decrData.encode("utf8"))
msg = self.aes.decrypt(res).decode("utf8")
return msg
eg = EncryptDate("1234567890123456") # 这里**的长度必须是16的倍数
data = {"name": "sxt123", "realname": "尚学堂", "sex": "1"}
res = eg.encrypt(str(data))
print(res)
print(eg.decrypt(res))
4. RSA
4.1 介绍
RSA加密算法是一种非对称加密算法。在公开密钥加密和电子商业中RSA被广泛使用
RSA算法需要两个密钥:
- 公开密钥(publickey)
- 私有密钥(privatekey)
- 公钥与私钥是一对,如果用公钥对数据进行加密,只有用对应的私钥才能解密。因为加密和解密使用的是两个不同的密钥,所以这种算法叫作非对称加密算法
使用时都是使用公钥加密,使用私钥解密。公钥可以公开,私钥自己保留。
算法强度复杂、安全性依赖算法与密钥但是由于其算法复杂,而使得加密解密速度没有对称加密速度快。
4.2 Python使用
class EncryptDate():
def encrypt_data(self,msg,pb):
key = RSA.importKey(pb)
cipher = PKCS1_cipher.new(key)
encrypt_text = base64.b64encode(cipher.encrypt(bytes(msg.encode("utf-8"))))
return encrypt_text.decode('utf-8')
def decrypt_data(self,msg,pv):
key = RSA.importKey(pv)
cipher = PKCS1_cipher.new(key)
back_text = cipher.decrypt(base64.b64decode(msg), 0)
return back_text.decode('utf-8')
if __name__ == '__main__':
enc = EncryptDate()
tmp = enc.encrypt_data('123',public_key)
print(enc.decrypt_data(tmp,private_key))