E

encryption_script

鉴权与保护

0. 运行环境

  • python3

  • pip install -r requirements.txt

1. 鉴权(服务端): verify.py

  • 生成公私钥对

    python RSA/create_keys.py
    
    
  • 根据过期日期(必须)和MAC地址(可选, en0),通过私钥加密,生成认证文件

    • help text

      ❯ python RSA/encrypt.py -h
        usage: encrypt.py [-h] -d DATE [-m MAC]
      
        encrypt expire date & mac address with private key
      
        optional arguments:
          -h, --help            show this help message and exit
          -d DATE, --date DATE  expire date. eg: 2020-06-06
          -m MAC, --mac MAC     mac address
      
    • example

      python RSA/encrypt.py -d 2021-01-12 -m 38:f9:d3:2e:c0:f3
      
  • 项目使用

    • 拷贝以下文件至项目
      • 公钥:RSA/public.pem
      • 认证文件:RSA/certification.cert
      • 验证脚本:verify.py
    • 使用验证脚本鉴权(pip install pycryptdome getmac)

      from verify import verify
      is_valid = verify(path_to_public_key, path_to_cert)
      if is_valid:
          print('verify success')
      else:
          print('verify failed')
      
      

2. 保护源码:py2so.py

  • description: compile the .py to .so(Linux/Mac) or .pdy(Win)
  • help text

    ❯ python py2so.py -h
    usage: py2so.py [-h] -i INPUT [-o OUTPUT] [-e EXCLUDE]
    
    compile the .py to .so(Linux/Mac) or .pdy(Win)
    
    optional arguments:
      -h, --help            show this help message and exit
      -i INPUT, --input INPUT
                            the directory(file) path of your project
      -o OUTPUT, --output OUTPUT
                            the directory path of compiled file
      -e EXCLUDE, --exclude EXCLUDE
                            exclude file. eg: ignore/ignore.py,main.py,ignore
    
  • example

    python py2so.py -i test -e test,main.py
    

3. nuitka打包

  • test_project可以通过以下方式,打包成可执行文件或文件夹

    nuitka3 --standalone --onefile --output-dir=out --include-package=websockets main.py
    
  • 另外一种不打包python环境的方式

    nuitka --standalone --nofollow-imports --include-package=websockets --output-dir=out main.py