MD5是一个非常常见的摘要(hash)逻辑. 其特点就是小巧. 速度快. 极难被破解
远生态
from hashlib import md5
obj = md5()
obj.update("admin".encode("utf-8"))
res = obj.hexdigest()
print(res)
有时候改一下,比如加个盐
因为原生的md5可以通过撞库实现,这里用django开发举例
import hashlib
SECRET_KEY = 'django-insecure-hkrj5qe6)4-oe)g&+s-_)90r8$$fk_*a1w33=2wikt4!^4_h6c'
def md5_string(data_string):
obj = hashlib.md5(SECRET_KEY.encode('utf-8'))
obj.update(data_string.encode('utf-8'))
return obj.hexdigest()
print(md5_string('admin'))
sha1, sha256的例子
from hashlib import sha256, sha1
def encrypt(data_string):
obj = sha256(b'admin')
obj.update(data_string.encode('utf-8'))
return obj.hexdigest()
print(encrypt('qwe123'))
sha1, sha256, md5都属于摘要算法. 都是在计算hash值. 只是散列的程度不同
base64编码:
构成:26个大写字母A~Z,26个小写字母a~z,10个数字0~9,符号“+”与符号“/”。Base64编码的基本思路是将原始数据的三个字节拆分转化为四个字节,然后根据Base64的对应表,得到对应的编码数据。
当原始数据凑不够三个字节时,编码结果中会使用额外的符号“=”来表示这种情况
import base64
bs = "admin".encode('utf-8')
print(base64.b64encode(bs).decode())
#YWRtaW4=
解密:
有些密文的b64长度不是4的倍数. 会报错
import base64
# data_string = 'YWRtaW4'
# print(base64.b64decode(data_string))
data_string = 'YWRtaW4='
print(base64.b64decode(data_string))
进行填充:
import base64
data_string = 'YWRtaW4'
data_string += ('=' * (4 - len(data_string) % 4))
print("填充以后:", data_string)
res = base64.b64decode(data_string)
print("结果:", res)
#填充以后: YWRtaW4=
#结果: b'admin'
# 进程已结束,退出代码0
AES加密:
pip install pycryptodome
AES 加密最常用的模式就是 ECB模式 和 CBC 模式,区别就是 ECB 不需要 iv偏移量,而CBC需要
ecb
加密:
from Crypto.Cipher import AES
import base64
# 秘钥,16位或24位或32位
key = '1234567890123456'.encode('utf-8')
#需要为16倍数
# text = 'how did i fall in love with you'
text = 'how did i fall in love with you?'
text = text.encode()
aes = AES.new(key, AES.MODE_ECB)
encrypt_text = aes.encrypt(text)
print("加密后的data:", encrypt_text)
base64_encrypt_text = base64.b64encode(encrypt_text).decode()
print("base64之后的数据:", base64_encrypt_text)
#加密后的data: b"\x0f\x9c\xb5\x1a\xbc\xc2\xbe\xc01\x8b'd\x94X\xf4b\xfb\x0c5\xd0BVU\x1e\xa5}L\xc1\xd2\xd2J\x8f"
#base64之后的数据: D5y1GrzCvsAxiydklFj0YvsMNdBCVlUepX1MwdLSSo8=
解密:
from Crypto.Cipher import AES
import base64
key = '1234567890123456'.encode()
aes = AES.new(key, AES.MODE_ECB)
text = 'D5y1GrzCvsAxiydklFj0YvsMNdBCVlUepX1MwdLSSo8='.encode()
encrypt_base64 = base64.b64decode(text)
decrypt_text = aes.decrypt(encrypt_base64).decode()
print(decrypt_text)
#how did i fall in love with you?
cbc
加密:
from Crypto.Cipher import AES
import base64
# 秘钥,16位或24位或32位
key = '1234567890123456'.encode('utf-8')
#需要为16倍数
# text = 'how did i fall in love with you'
text = 'how did i fall in love with you?'
text = text.encode()
#偏移量必须为16字节
iv = b'abcdabcdabcdabcd'
aes = AES.new(key, AES.MODE_CBC, iv)
encrypt_text = aes.encrypt(text)
print("加密后的data:", encrypt_text)
base64_encrypt_text = base64.b64encode(encrypt_text).decode()
print("base64之后的数据:", base64_encrypt_text)
#加密后的data: b'\x07\x11l\xe9\xaej!\xf9\xd2\xeb?\x8f\x85\xb2\xd5\x8c\x97\xd3iA\x86}\x00\x87\x8e\xe7\xd0m\xbc\x9f\xec\x02'
#base64之后的数据: BxFs6a5qIfnS6z+PhbLVjJfTaUGGfQCHjufQbbyf7AI=
解密·
from Crypto.Cipher import AES
import base64
# 秘钥,16位或24位或32位
key = '1234567890123456'.encode('utf-8')
#需要为16倍数
# text = 'how did i fall in love with you'
text = 'how did i fall in love with you?'
text = text.encode()
#偏移量必须为16字节
iv = b'abcdabcdabcdabcd'
aes = AES.new(key, AES.MODE_CBC, iv)
encrypt_text = 'BxFs6a5qIfnS6z+PhbLVjJfTaUGGfQCHjufQbbyf7AI='.encode()
encrypt_base64 = base64.b64decode(encrypt_text)
decrypt_text = aes.decrypt(encrypt_base64).decode()
print("解密以后", decrypt_text)
#解密以后 how did i fall in love with you?
非对称加密(RSA)
非对称加密. 加密和解密的秘钥不是同一个秘钥.
创建公私钥
from Crypto.PublicKey import RSA
rsa_key = RSA.generate(1024)
#生成秘钥
with open("rsa.public.pem", mode="wb") as fp:
fp.write(rsa_key.public_key().exportKey())
with open("rsa.private.pem", mode='wb') as fp:
fp.write(rsa_key.exportKey())
形式,
rsa.public.pem
-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDPH4nexCyCeKD0sEqiLarrsO8I
Guw82Szv56y2JAw8Xbg/uFubhjV8k9a2isyJyFTBJ3tpnYNBTRzPjEeBJSczxLgs
nJOag0suvpprfL/Gxhsn5RSi5MBoxKQb3fSOQUHw4eSqxG/RyWWPmSyLSN9jHmnS
AGEsjVQd2sGga78D4wIDAQAB
-----END PUBLIC KEY-----
rsa.private.pem
-----BEGIN RSA PRIVATE KEY-----
MIICXAIBAAKBgQDPH4nexCyCeKD0sEqiLarrsO8IGuw82Szv56y2JAw8Xbg/uFub
hjV8k9a2isyJyFTBJ3tpnYNBTRzPjEeBJSczxLgsnJOag0suvpprfL/Gxhsn5RSi
5MBoxKQb3fSOQUHw4eSqxG/RyWWPmSyLSN9jHmnSAGEsjVQd2sGga78D4wIDAQAB
AoGAAV11D82UeKkXS56w9zthGu4hGJk404GNfQ1Iw1UexhiBdW8xsILJJL5nQ6aU
9mBdROvEetBcBCsXAk5QKXk7/2WDVqN0mBRGLmGwR0IGzYi7C+edVUrSQ9d/e9aA
v8HCZkDZADXLgSTk6aip/x2KSy/KYX4ezoayQ7K4L2hYjIUCQQDR0tq+Z6NUfxLb
s9Gh4g1Y6KZULJTwL6uAOpFJna26obWNO2Pb1x8H1qHkq2SSGXdOnmV6JiddVvTi
Lu35St7/AkEA/LSLcE0tk6eGGPPXWzt14d4Cgf2S1o7oKoWfPzHDu6nrQXW4knyf
uRmWMgU9gsFBvoY1Jj+3oLlm39V4AZ0/HQJAPMrGrdwBKn4WzKjw1+JS06u0ZTb1
gBGPsvtFFPsmVqh7nYvThuJxDyhUh5hQ7zOJqbGi3R0o0pZbJR1Lvaq/qQJBAKUa
SGfs5LQtpX00AyYGKTSS2kyXoZIw+BqgBrVBDELV93BjO13psmk7AFhZ+CAvp4aV
JLCCvLRzrXTcgXV8iTkCQBGUEa03ZTqpg5QsXur6nInrMw63QOo8SyVvPBzWhy7d
vmNFOtd3rb4+JM7s/fLO4HZxwmxwSHrGBKzOUCrPDi8=
-----END RSA PRIVATE KEY-----
加密:
from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_v1_5
import base64
data_string = "加油吧"
with open("rsa.public.pem", mode="r") as fp:
pk = fp.read()
rsa_pk = RSA.importKey(pk)
rsa = PKCS1_v1_5.new(rsa_pk)
result = rsa.encrypt(data_string.encode('utf-8'))
base64_res = base64.b64encode(result).decode('utf-8')
print(base64_res)
#doYGAcuEGtXk7EgIyoInLJBtWFYOaYCxWOER6Euc2QpkNyhB0jUFhyg76ejDsETrfnez5JQUcGKGysvG5jswQUArzgdcAmpFUv0qWltmTLl0rW+UcAkd7LcWAyv2Ld2XQFArTOBX4TKhxS/DIaQQTH8fRVqK7UwpKP6fXK9gvpI=
解密
from Crypto.Cipher import PKCS1_v1_5
from Crypto.PublicKey import RSA
import base64
data_string = "doYGAcuEGtXk7EgIyoInLJBtWFYOaYCxWOER6Euc2QpkNyhB0jUFhyg76ejDsETrfnez5JQUcGKGysvG5jswQUArzgdcAmpFUv0qWltmTLl0rW+UcAkd7LcWAyv2Ld2XQFArTOBX4TKhxS/DIaQQTH8fRVqK7UwpKP6fXK9gvpI="
with open("rsa.private.pem", mode="r") as fp:
prikey = fp.read()
rsa_pk = RSA.importKey(prikey)
rsa = PKCS1_v1_5.new(rsa_pk)
res = rsa.decrypt(base64.b64decode(data_string), None)
final_res = res.decode('utf-8')
print("解密结果:", final_res)
#解密结果: 加油吧