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!

相关文章:

  • 2021-06-30
  • 2021-12-03
  • 2021-06-12
  • 2021-05-29
  • 2021-08-05
  • 2022-12-23
  • 2021-10-29
  • 2021-12-23
猜你喜欢
  • 2021-05-16
  • 2021-06-24
  • 2022-02-15
  • 2021-06-14
  • 2022-01-01
  • 2022-12-23
  • 2021-12-20
相关资源
相似解决方案