在做一些网络传输的时候会对数据进行加密,这边例举一些常用的加密算法
1.md5
1.字符串md5
import hashlib
if __name__ == '__main__':
username = "test"
username_md5 = hashlib.md5(username.encode(encoding='UTF-8')).hexdigest()
print(username_md5)
hashlib里面还有sha的各种加密方式,调用跟md5一样,只要把md5换成sha1就可以,可以自己尝试。
2.文件md5
import hashlib
if __name__ == '__main__':
username = "./test.txt"
m = hashlib.md5()
n = 1024 * 4
inp = open(username, 'rb')
while True:
buf = inp.read(n)
if buf:
m.update(buf)
else:
break
print(m.hexdigest())
2.base64
base64的加密方式
import base64
if __name__ == '__main__':
username = "test"
username_encode = base64.b64encode(username.encode(encoding='UTF-8'))
print(username_encode)
username_decode = base64.b64decode(username_encode)
print(username_decode)
3.AES
- 秘钥:加密的时候用秘钥,解密的时候需要同样的秘钥才能解出来
- 明文:需要加密的参数
- 模式:aes 加密常用的有 ECB 和 CBC 模式(我只用了这两个模式,还有其他模式)
- iv 偏移量:这个参数在 ECB 模式下不需要,在 AES 模式下需要
- AES CBC 加密:
from Crypto.Cipher import AES
from binascii import b2a_hex, a2b_hex
# 如果text不足16位的倍数就用空格补足为16位
def add_to_16(text):
if len(text.encode('utf-8')) % 16:
add = 16 - (len(text.encode('utf-8')) % 16)
else:
add = 0
text = text + ('\0' * add)
return text.encode('utf-8')
# 加密函数
def encrypt(text):
key = '9999999999999999'.encode('utf-8')
mode = AES.MODE_CBC
iv = b'qqqqqqqqqqqqqqqq'
text = add_to_16(text)
cryptos = AES.new(key, mode, iv)
cipher_text = cryptos.encrypt(text)
# 因为AES加密后的字符串不一定是ascii字符集的,输出保存可能存在问题,所以这里转为16进制字符串
return b2a_hex(cipher_text)
# 解密后,去掉补足的空格用strip() 去掉
def decrypt(text):
key = '9999999999999999'.encode('utf-8')
iv = b'qqqqqqqqqqqqqqqq'
mode = AES.MODE_CBC
cryptos = AES.new(key, mode, iv)
plain_text = cryptos.decrypt(a2b_hex(text))
return bytes.decode(plain_text).rstrip('\0')
if __name__ == '__main__':
e = encrypt("hello world") # 加密
d = decrypt(e) # 解密
print("加密:", e)
print("解密:", d)
2.AES ECB加密
from Crypto.Cipher import AES
from binascii import b2a_hex, a2b_hex
def add_to_16(text):
if len(text.encode('utf-8')) % 16:
add = 16 - (len(text.encode('utf-8')) % 16)
else:
add = 0
text = text + ('\0' * add)
return text.encode('utf-8')
# 加密函数
def encrypt(text):
key = '9999999999999999'.encode('utf-8')
mode = AES.MODE_ECB
text = add_to_16(text)
cryptos = AES.new(key, mode)
cipher_text = cryptos.encrypt(text)
return b2a_hex(cipher_text)
# 解密后,去掉补足的空格用strip() 去掉
def decrypt(text):
key = '9999999999999999'.encode('utf-8')
mode = AES.MODE_ECB
cryptor = AES.new(key, mode)
plain_text = cryptor.decrypt(a2b_hex(text))
return bytes.decode(plain_text).rstrip('\0')
if __name__ == '__main__':
e = encrypt("hello world") # 加密
d = decrypt(e) # 解密
print("加密:", e)
print("解密:", d)
4.RSA
rsa的加解密需要公钥私钥,一般客户端使用公钥进行加密,服务器使用私钥进行解密,验证合法性。
在ubuntu上面执行上面两个命令,即可产生private.key和public.key两个文件
openssl genrsa -out private.key 2048
openssl rsa -in private.key -pubout -out public.key
使用上面两个文件进行加解密测试,如下:
import rsa
if __name__ == '__main__':
username = "test"
with open("./public.key", mode="rb") as f:
public_key = f.read()
pubkey = rsa.PublicKey.load_pkcs1_openssl_pem(public_key)
username_rsa = rsa.encrypt(username.encode(), pubkey)
print(username_rsa.hex())
print(len(username_rsa))
with open("./private.key", mode="rb") as f:
private_key = f.read()
prikey = rsa.PrivateKey.load_pkcs1(private_key)
username = rsa.decrypt(username_rsa, prikey)
print(username)