Python 常用库(随时补充)

 

1. Python-RSA使用手册

英文文档见Python-RSA使用手册,主要介绍了Python-RSA的消息的加密解密、文件的加密解密以及签名的方法。

Installation

使用pip install rsa安装Python-RSA模块

Generating keys

>>> import rsa
>>> (pubkey, privkey) = rsa.newkeys(512)

Encryption and decryption

To encrypt or decrypt a message, use rsa.encrypt() resp. rsa.decrypt(). Let’s say that Alice wants to send a message that only Bob can read.

  1. Bob generates a keypair, and gives the public key to Alice. This is done such that Alice knows for sure that the key is really Bob’s (for example by handing over a USB stick that contains the key).

    >>> import rsa
    >>> (bob_pub, bob_priv) = rsa.newkeys(512)
  2. Alice writes a message, and encodes it in UTF-8. The RSA module only operates on bytes, and not on strings, so this step is necessary.

    >>> message = 'hello Bob!'.encode('utf8')
  3. Alice encrypts the message using Bob’s public key, and sends the encrypted message.

    >>> import rsa
    >>> crypto = rsa.encrypt(message, bob_pub)
  4. Bob receives the message, and decrypts it with his private key.

    >>> message = rsa.decrypt(crypto, bob_priv)
    >>> print(message.decode('utf8'))
    hello Bob!
原文地址:https://www.cnblogs.com/tangkaixin/p/6756791.html