Python 加密解密
时间: 2024-06-02 15:06:25 浏览: 22
Python提供了许多加密和解密算法的库,例如hashlib、hmac、pycrypto等。其中,hashlib库可以实现MD5、SHA-1、SHA-256等哈希加密算法;hmac库可以实现HMAC加密算法;pycrypto库可以实现AES、DES、RSA等对称和非对称加密算法。
下面以MD5加密算法为例,简单介绍一下Python中的加密解密过程:
1. 加密:
```python
import hashlib
# 明文
str = 'hello world'
# 创建md5对象
hash_obj = hashlib.md5()
# 对字符串进行md5加密
hash_obj.update(str.encode('utf-8'))
# 输出加密后的结果
print(hash_obj.hexdigest())
```
2. 解密:
由于MD5算法是不可逆的,所以不能对其进行解密操作。
以上是Python中的加密解密过程,其他算法的实现方式类似。如果您有具体的加密解密需求,请告诉我,我会尽力帮助您。
相关问题
python 加密解密
以下是Python中常用的加密解密方法:
1. 使用hashlib库进行哈希加密:
```python
import hashlib
# 创建md5对象
md5 = hashlib.md5()
# 更新要加密的数据
md5.update("Hello World".encode('utf-8'))
# 获取加密后的结果
result = md5.hexdigest()
print("MD5加密结果:", result)
```
2. 使用base64库进行Base64编码和解码:
```python
import base64
# 编码
encoded_str = base64.b64encode("Hello World".encode('utf-8'))
print("Base64编码结果:", encoded_str)
# 解码
decoded_str = base64.b64decode(encoded_str).decode('utf-8')
print("Base64解码结果:", decoded_str)
```
3. 使用cryptography库进行对称加密和解密:
```python
from cryptography.fernet import Fernet
# 生成密钥
key = Fernet.generate_key()
# 创建加密对象
cipher_suite = Fernet(key)
# 加密
cipher_text = cipher_suite.encrypt(b"Hello World")
print("对称加密结果:", cipher_text)
# 解密
plain_text = cipher_suite.decrypt(cipher_text)
print("对称解密结果:", plain_text.decode('utf-8'))
```
4. 使用cryptography库进行非对称加密和解密:
```python
from cryptography.hazmat.primitives.asymmetric import rsa, padding
from cryptography.hazmat.primitives import serialization
# 生成密钥对
private_key = rsa.generate_private_key(
public_exponent=65537,
key_size=2048
)
public_key = private_key.public_key()
# 加密
cipher_text = public_key.encrypt(
b"Hello World",
padding.OAEP(
mgf=padding.MGF1(algorithm=hashes.SHA256()),
algorithm=hashes.SHA256(),
label=None
)
)
print("非对称加密结果:", cipher_text)
# 解密
plain_text = private_key.decrypt(
cipher_text,
padding.OAEP(
mgf=padding.MGF1(algorithm=hashes.SHA256()),
algorithm=hashes.SHA256(),
label=None
)
)
print("非对称解密结果:", plain_text.decode('utf-8'))
```
python加密解密
在Python中,可以使用AES算法进行加密和解密操作。引用[1]提供了一个简单的测试脚本,其中使用了encrypt函数和decrypt函数来进行加密和解密操作。在加密时,需要指定加密密钥,并进行填充和base64编码。在解密时,需要对传入的密文进行base64解码,再进行解密和填充去除。引用[2]提供了encrypt函数和decrypt函数的具体实现代码。你可以根据这个代码来实现自己的加密解密程序。
相关推荐
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)