cryptography库介绍
时间: 2023-11-22 18:49:03 浏览: 38
Cryptography 是一个用于加密和解密数据的 Python 库。它提供了各种加密算法,密钥生成和管理工具,以及常见的密码学函数和协议的实现。
该库的主要特点包括:
1. 支持对称加密和非对称加密算法。对称加密使用相同的密钥进行加密和解密,而非对称加密使用一对密钥:公钥和私钥。
2. 提供了常见的加密算法,如AES、Blowfish、DES、TripleDES等。这些算法可以用于保护数据的机密性。
3. 支持数字签名和验证功能,使用非对称加密算法来验证数据的完整性和身份认证。
4. 提供了密码学哈希函数,如MD5、SHA1、SHA256等。这些函数可以用于生成消息摘要和密码散列。
5. 提供了安全随机数生成器,用于生成高质量的随机数。
6. 支持常见的密码学协议,如TLS/SSL、HMAC、RSA等。
通过使用 Cryptography 库,开发人员可以轻松地实现数据的保护和安全传输。它具有广泛的应用领域,包括网络通信、文件加密、数字签名等。
相关问题
如何在python中调用AES库
Python的标准库中是没有 AES 加密库的,不过可以使用第三方库来实现 AES 加密。这里我们介绍两种常用的加密库:`pycryptodome` 和 `cryptography`。
### 使用 pycryptodome 库
首先,需要使用以下命令安装 `pycryptodome` 模块:
```
pip install pycryptodome
```
然后,您可以使用以下代码示例进行 AES 加密和解密:
```python
from Crypto.Cipher import AES
import base64
# 将密钥和待加密的数据补齐为 AES 块大小的倍数
def pad(s):
return s + b"\0" * (AES.block_size - len(s) % AES.block_size)
# 加密函数
def encrypt(message, key):
message = pad(message)
iv = b"1234567812345678" # 初始化向量必须为 16 个字节
cipher = AES.new(key, AES.MODE_CBC, iv)
ciphertext = cipher.encrypt(message)
return base64.b64encode(ciphertext)
# 解密函数
def decrypt(ciphertext, key):
iv = b"1234567812345678" # 初始化向量必须为 16 个字节
cipher = AES.new(key, AES.MODE_CBC, iv)
message = cipher.decrypt(base64.b64decode(ciphertext))
return message.rstrip(b"\0")
# 测试代码
key = b"1234567890123456" # 密钥必须为 16、24 或 32 个字节
message = b"Hello, World!"
encrypted_message = encrypt(message, key)
decrypted_message = decrypt(encrypted_message, key)
print("明文:", message)
print("加密后:", encrypted_message)
print("解密后:", decrypted_message)
```
其中,`encrypt` 函数使用 AES 密码模式(CBC)对数据进行加密,`decrypt` 函数使用同样的方式进行解密。需要注意的是,密钥长度必须为 16、24 或 32 个字节,且初始化向量必须为 16 个字节。本示例中使用的是 CBC 模式,您也可以根据需要使用其他模式(如 ECB、CFB 或 OFB)。
### 使用 cryptography 库
`cryptography` 库是一个功能强大的加密库,可以实现多种加密算法,包括 AES 加密。首先,需要使用以下命令安装 `cryptography` 模块:
```
pip install cryptography
```
然后,您可以使用以下代码示例进行 AES 加密和解密:
```python
from cryptography.fernet import Fernet
# 生成密钥
key = Fernet.generate_key()
# 加密函数
def encrypt(message, key):
f = Fernet(key)
ciphertext = f.encrypt(message)
return ciphertext
# 解密函数
def decrypt(ciphertext, key):
f = Fernet(key)
message = f.decrypt(ciphertext)
return message
# 测试代码
message = b"Hello, World!"
encrypted_message = encrypt(message, key)
decrypted_message = decrypt(encrypted_message, key)
print("明文:", message)
print("加密后:", encrypted_message)
print("解密后:", decrypted_message)
```
其中,`Fernet` 是 `cryptography` 库中 AES 加密的实现类,`generate_key` 方法用于生成随机密钥。需要注意的是,密钥必须是 `bytes` 类型,且密钥长度必须为 32、64 或 128 个字节。本示例中生成的是 32 个字节的随机密钥,您也可以自行指定密钥。
希望这些信息对您有所帮助。
调用Python库,分别实现RSA-3072 和AES128的加密和解密
RSA-3072 加密和解密的实现可以使用 Python 中的 `cryptography` 库,而 AES128 加密和解密则可以使用 Python 中的 `pycryptodome` 库。
下面分别介绍如何使用这两个库实现加密和解密:
1. RSA-3072 加密和解密
```python
from cryptography.hazmat.primitives.asymmetric import rsa, padding
from cryptography.hazmat.primitives import serialization, hashes
# 生成 RSA 密钥对
private_key = rsa.generate_private_key(
public_exponent=65537,
key_size=3072,
)
public_key = private_key.public_key()
# 将密钥对序列化为 PEM 格式
private_key_pem = private_key.private_bytes(
encoding=serialization.Encoding.PEM,
format=serialization.PrivateFormat.PKCS8,
encryption_algorithm=serialization.NoEncryption()
)
public_key_pem = public_key.public_bytes(
encoding=serialization.Encoding.PEM,
format=serialization.PublicFormat.SubjectPublicKeyInfo
)
# 加密明文
message = b"Hello, World!"
ciphertext = public_key.encrypt(
message,
padding.OAEP(
mgf=padding.MGF1(algorithm=hashes.SHA256()),
algorithm=hashes.SHA256(),
label=None
)
)
# 解密密文
plaintext = private_key.decrypt(
ciphertext,
padding.OAEP(
mgf=padding.MGF1(algorithm=hashes.SHA256()),
algorithm=hashes.SHA256(),
label=None
)
)
```
2. AES128 加密和解密
```python
from Crypto.Cipher import AES
from Crypto.Random import get_random_bytes
# 生成随机密钥
key = get_random_bytes(16)
# 加密明文
message = b"Hello, World!"
cipher = AES.new(key, AES.MODE_EAX)
ciphertext, tag = cipher.encrypt_and_digest(message)
# 解密密文
decipher = AES.new(key, AES.MODE_EAX, nonce=cipher.nonce)
plaintext = decipher.decrypt_and_verify(ciphertext, tag)
```
需要注意的是,这两个库的安装方式如下:
```
pip install cryptography
pip install pycryptodome
```