由来
做爬虫逆向的时候, 经常要用到加密解密
目录
pkcs7填充: pad和unpad
crypto: 一个共同方法 + 一个例子
pkcs7填充: pad和unpad
1. 为什么要PKCS7填充?
因为PKCS7是当下各大加密算法都遵循的数据填充算法
2. 基础
1个字节有8位, 所以16个字节是128位, 比如: 一个字母A就是一个字节
16进制: 0-9, a,b,c,d,e,f, 其中a代表10,后面依次加1, 满16进一,所以 15对应\x0f,16对应\x10
转换:
int -> unicode: chr(1) 结果是 \x01
unicode -> int: ord('\x11')值为17
3. pkcs7补位规则
补位的个数: 不足16位字节的倍数,补足变成16位的倍数,如果刚好是16的倍数, 补16个字节
补位的值: 等于补位个数的unicode码, 比如:content有7位,那么补充9个字节,每个字节的值都是\x09
4. 如何获取真实字符串?
直接获取字符串的最后一个字符,转换为int即为补位的数, 然后,真实 = 原始字符串长度 - 补位的数
def pad(content: str, block_size: int = 16) -> str:
p_len = block_size - len(content) % block_size
p = p_len * chr(p_len)
return content + p
def unpad(content: str) -> str:
last_char = ord(content[-1])
# 获取真实字符串,去掉最后补位的数据
return content[:-last_char]
def test_pad():
# 计算字符数
assert 3 == len('11中'), 'pad china error'
assert 4 == len('abcd'), 'pad china error'
p1 = pad('1')
assert '1\x0f\x0f\x0f\x0f\x0f\x0f\x0f\x0f\x0f\x0f\x0f\x0f\x0f\x0f\x0f' == p1, 'pad 1 error'
b = r'1\x0f\x0f\x0f\x0f\x0f\x0f\x0f\x0f\x0f\x0f\x0f\x0f\x0f\x0f\x0f'
assert f"'{b}'" == repr(p1), 'pad 1 1 error'
p2 = pad('11')
assert '11\x0e\x0e\x0e\x0e\x0e\x0e\x0e\x0e\x0e\x0e\x0e\x0e\x0e\x0e' == p2, 'pad 2 error'
p15 = pad('111111111122222')
assert '111111111122222\x01' == p15, 'pad 15 error'
p16 = pad('1111111111222222')
assert '1111111111222222\x10\x10\x10\x10\x10\x10\x10\x10\x10\x10\x10\x10\x10\x10\x10\x10' == p16, 'pad 16 error'
def test_unpad():
p1 = '1\x0f\x0f\x0f\x0f\x0f\x0f\x0f\x0f\x0f\x0f\x0f\x0f\x0f\x0f\x0f'
assert '1' == unpad(p1)
p2 = '11\x0e\x0e\x0e\x0e\x0e\x0e\x0e\x0e\x0e\x0e\x0e\x0e\x0e\x0e'
assert '11' == unpad(p2)
p15 = '111111111122222\x01'
assert '111111111122222' == unpad(p15)
p16 = '1111111111222222\x10\x10\x10\x10\x10\x10\x10\x10\x10\x10\x10\x10\x10\x10\x10\x10'
assert '1111111111222222' == unpad(p16)
def pad(content: str, block_size: int = 16) -> str: p_len = block_size - len(content) % block_size p = p_len * chr(p_len) return content + p def unpad(content: str) -> str: last_char = ord(content[-1]) # 获取真实字符串,去掉最后补位的数据 return content[:-last_char] def test_pad(): # 计算字符数 assert 3 == len('11中'), 'pad china error' assert 4 == len('abcd'), 'pad china error' p1 = pad('1') assert '1\x0f\x0f\x0f\x0f\x0f\x0f\x0f\x0f\x0f\x0f\x0f\x0f\x0f\x0f\x0f' == p1, 'pad 1 error' b = r'1\x0f\x0f\x0f\x0f\x0f\x0f\x0f\x0f\x0f\x0f\x0f\x0f\x0f\x0f\x0f' assert f"'{b}'" == repr(p1), 'pad 1 1 error' p2 = pad('11') assert '11\x0e\x0e\x0e\x0e\x0e\x0e\x0e\x0e\x0e\x0e\x0e\x0e\x0e\x0e' == p2, 'pad 2 error' p15 = pad('111111111122222') assert '111111111122222\x01' == p15, 'pad 15 error' p16 = pad('1111111111222222') assert '1111111111222222\x10\x10\x10\x10\x10\x10\x10\x10\x10\x10\x10\x10\x10\x10\x10\x10' == p16, 'pad 16 error' def test_unpad(): p1 = '1\x0f\x0f\x0f\x0f\x0f\x0f\x0f\x0f\x0f\x0f\x0f\x0f\x0f\x0f\x0f' assert '1' == unpad(p1) p2 = '11\x0e\x0e\x0e\x0e\x0e\x0e\x0e\x0e\x0e\x0e\x0e\x0e\x0e\x0e' assert '11' == unpad(p2) p15 = '111111111122222\x01' assert '111111111122222' == unpad(p15) p16 = '1111111111222222\x10\x10\x10\x10\x10\x10\x10\x10\x10\x10\x10\x10\x10\x10\x10\x10' assert '1111111111222222' == unpad(p16)
crypto: 一个共同方法 + 一个例子
代码很简单, 不需要过多的解释
from base64 import b64decode, b64encode
from Crypto.Cipher import AES
from Crypto.Util.Padding import pad, unpad
class CryptoR():
def __init__(self, key: str, iv: str):
self.aes = AES.new(key.encode('utf8'), AES.MODE_CBC, iv.encode('utf8'))
def encrypto(self, text: str) -> str:
p = pad(text.encode('utf8'), AES.block_size)
return b64encode(self.aes.encrypt(p)).decode('utf8')
def decrypto(self, text: str) -> str:
d = self.aes.decrypt(b64decode(text))
return unpad(d, AES.block_size).decode('utf8')
def call_en():
text = 'm3u8.okjx.cc|057f1eed099f2f7e'
key = 'dvyYRQlnPRCMdQSe'
iv = '057f1eed099f2f7e'
aes = CryptoR(key, iv)
e1 = aes.encrypto(text)
assert e1 == 'ncZvyrJ2IPdKSF2EmvfZAGVrtAj9D8eXnGpjPTV1QTM=', 'encrypto error'
def call_de():
text = 'HCizZLcgJfm1LvajcEBMHChheUySaJgXpuPL+L9BDW4jdd9ds5qCycVwN6SpotDujY/tok+sT2M//AeeKnDk6TrZ0gvFa1szUi8j3xg2TTZXZMW/52wuXhr1vnxrbrz4socZ2twSY1T63NiJx3XAcLe6RXfSmBxuPzstMQ0pDSSDNPT4H5hKYy1wo227zzcnxdAuwo1WTvcdaMIuXBS/9IkbqNFklzikvvdGjJjsVxRLwSd5T6v4I47CUAgVtncxpf3cKlM37lmEWpZwskJKG/IGkJRRy9K2a/tdIMWNu0JbsF0jlyMNT/kT2OS08jsRsGJPxOnpEiFIPgE6UPGUgq7+SaBHvFdSfusdqDTwBHuyXJTZBixolYEnggfr3UfWOtP1pvWR4R7bY2N7XJ7gEf6QMfjbu0YB1V4+2WX5ucy43954EniGFbUw2Zxi35y+OGgkpunPTzccVbGw871RjiU7iNVo+IXvQHgG+0FPjaWUezWYm4YAePohtxJYAqg+1XATcgFOKTUn21QQCDKAuQ=='
key = '0EAE7A71512EC09C'
iv = '675480787382e6f4'
aes = CryptoR(key, iv)
d1 = aes.decrypto(text)
vkey=5DD8A1F1C4657D16EE8F61CEA9D4FEDF5195B3F0404A4104E6877F4D61D6DF3F02B8EA021782BFF03319989D91AF586088584D8DB58C6908C6B83C02137CE54CB3F09873AC784EFB2D24A2A4D041757D7E753A5523BB08AC010E33F4DD1F8A16F8266900BA457AF5223DB94A6187A6280CEA7CCA4BF76EDC&QQ=335583&From=www.nxflv.com'
assert d1 == d2, 'decrypto error'
def main():
call_en()
call_de()
if __name__ == '__main__':
main()
审核编辑:刘清
全部0条评论
快来发表一下你的评论吧 !