Lucun_Ji

encode.py

Jul 3rd, 2022 (edited)
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.81 KB | None | 0 0
  1. import base64
  2. import json
  3. from typing import Tuple
  4.  
  5. from Cryptodome.Cipher import AES
  6. from Cryptodome.Random.random import sample
  7.  
  8.  
  9. def encrypt(text: str, encoding='utf-8') -> Tuple[str, str]:
  10.     """
  11.    Credit to https://www.jianshu.com/p/87309e36e027
  12.    and https://segmentfault.com/a/1190000012818254?utm_source=tuicool&utm_medium=referral
  13.    """
  14.     random_string = ''.join(sample([c for c in 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'], 16))
  15.  
  16.     pubkey = '010001'
  17.     modulus = '00e0b509f6259df8642dbc35662901477df22677ec152b5ff68ace615bb7b725152b3ab17a876aea8a5aa76d2e417629ec4ee3' \
  18.               '41f56135fccf695280104e0312ecbda92557c93870114af6c9d05c4f7f0c3685b7a46bee255932575cce10b424d813cfe4875d' \
  19.               '3e82047b97ddef52741d546b8e289dc6935b3ece0462db0a22b8e7'
  20.     rs = int(random_string[::-1].encode(encoding).hex(), 16) ** int(pubkey, 16) % int(modulus, 16)
  21.     seckey = format(rs, 'x').zfill(256)
  22.  
  23.     for random_string in [
  24.         '0CoJUm6Qyw8W8jud',
  25.         random_string
  26.     ]:
  27.         encryptor = AES.new(random_string.encode(encoding), AES.MODE_CBC, iv='0102030405060708'.encode(encoding))
  28.         pad = 16 - len(text) % 16
  29.         text = (text + pad * chr(pad)).encode(encoding)
  30.         text = base64.b64encode(encryptor.encrypt(text)).decode(encoding)
  31.     return text, seckey
  32.  
  33.  
  34. if __name__ == '__main__':
  35.     # out = encrypt(json.dumps({
  36.     # '/api/nuser/account/get': {},
  37.     # '/api/music-vip-membership/front/vip/info': {},
  38.     # '/api/purchased/redvip/vipstatus': {},
  39.     # }))
  40.     out = encrypt(json.dumps({
  41.         's': 'One',
  42.         'type': 1,
  43.         'limit': 30,
  44.         'offset': 0
  45.     }))
  46.     body = 'params=' + out[0].replace('/', '%2F').replace('+', '%2B').replace('=', '%3D') + \
  47.            '&encSecKey=' + out[1]
  48.     print(body)
  49.  
  50.  
Add Comment
Please, Sign In to add comment