asymmetric#

2021年还在由活跃的社区维护的rsa扩展有 cryptographyrsa. cryp 集合了非常 多主流的加密算法, 并且都做了非常多的安全和性能优化(C语言实现). 而 rsa 是纯 Python 实现, 安装非常方便, 使用起来也较为简单. 所以本模块最后决定基于 rsa 进行开发.

class windtalker.asymmetric.AsymmetricCipher(my_pubkey, my_privkey, his_pubkey)[source]#

A asymmetric encryption algorithm utility class helps you easily encrypt/decrypt text and files.

Parameters:
  • my_pubkey – your public key

  • my_privkey – your private key

  • his_pubkey – other’s public key you use to encrypt message

中文文档

非对称加密器. 主要用于加密少量信息. 通常用于安全地交换秘钥, 然后用该秘钥作为对称加密 的钥匙对大量数据进行加密.

static new_keys(nbits=1024)[source]#

Create a new pair of public and private key pair to use.

static newkeys(nbits=1024)#

Create a new pair of public and private key pair to use.

encrypt(binary: bytes, use_sign: bool = True, sign_method: str = 'SHA-256', *args, **kwargs) bytes[source]#

Encrypt binary data.

Parameters:

sing_method – one of ‘MD5’, ‘SHA-1’, ‘SHA-224’, SHA-256’, ‘SHA-384’ or ‘SHA-512’

中文文档

  • 发送消息时只需要对方的 public_key

  • 如需使用签名, 则双方都需要持有对方的 public_key

decrypt(token: bytes, signature: Optional[bytes] = None, *args, **kwargs) bytes[source]#

Decrypt binary data.

中文文档

  • 接收消息时只需要自己的 private_key

  • 如需使用签名, 则双方都需要持有对方的 public_key

encrypt_file(**kwargs)[source]#

Encrypt a file. If output_path are not given, then try to use the path with a surfix appended. The default automatical file path handling is defined here windtalker.files.get_encrypted_file_path()

Parameters:
  • path – path of the file you need to encrypt

  • output_path – encrypted file output path

  • overwrite – if True, then silently overwrite output file if exists

  • stream – if it is a very big file, stream mode can avoid using too much memory

  • enable_verbose – trigger on/off the help information

decrypt_file(**kwargs)[source]#

Decrypt a file. If output_path are not given, then try to use the path with a surfix appended. The default automatical file path handling is defined here windtalker.files.recover_path()

Parameters:
  • path – path of the file you need to decrypt

  • output_path – decrypted file output path

  • overwrite – if True, then silently overwrite output file if exists

  • stream – if it is a very big file, stream mode can avoid using too much memory

  • enable_verbose – boolean, trigger on/off the help information

encrypt_dir(**kwargs)[source]#

Encrypt everything in a directory.

Parameters:
  • path – path of the dir you need to encrypt

  • output_path – encrypted dir output path

  • overwrite – if True, then silently overwrite output file if exists

  • stream – if it is a very big file, stream mode can avoid using too much memory

  • enable_verbose – boolean, trigger on/off the help information

decrypt_dir(**kwargs)[source]#

Decrypt everything in a directory.

Parameters:
  • path – path of the dir you need to decrypt

  • output_path – decrypted dir output path

  • overwrite – if True, then silently overwrite output file if exists

  • stream – if it is a very big file, stream mode can avoid using too much memory

  • enable_verbose – boolean, trigger on/off the help information