chacha20 python
时间: 2023-10-17 12:34:04 浏览: 143
ChaCha20 is a symmetric encryption algorithm that provides both confidentiality and integrity of data. In Python, you can use the `cryptography` library to implement ChaCha20 encryption. Here's an example of how you can use it:
```python
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms
from cryptography.hazmat.primitives.ciphers.aead import ChaCha20Poly1305
# Generate a random key and nonce
key = os.urandom(32)
nonce = os.urandom(12)
# Create a ChaCha20 cipher object
cipher = Cipher(algorithms.ChaCha20(key, nonce), mode=None)
# Create an encryptor object
encryptor = cipher.encryptor()
# Encrypt the plaintext
ciphertext = encryptor.update(b"Hello, World!") + encryptor.finalize()
# Create a decryptor object
decryptor = cipher.decryptor()
# Decrypt the ciphertext
decrypted_plaintext = decryptor.update(ciphertext) + decryptor.finalize()
print(decrypted_plaintext.decode())
```
Make sure you have the `cryptography` library installed before running this code. You can install it using `pip install cryptography`.
阅读全文