python密码加密与解密
时间: 2023-08-08 07:08:33 浏览: 60
在Python中,常见的加密方式有MD5、SHA1、SHA256等,其中MD5加密是最常用的一种加密方式。下面是一个简单的MD5加密和解密的例子:
```python
import hashlib
# 加密
def encrypt(password):
md5 = hashlib.md5()
md5.update(password.encode('utf-8'))
return md5.hexdigest()
# 解密(MD5加密是不可逆的,因此这里只是一个简单的验证)
def decrypt(password, encrypted_password):
if encrypt(password) == encrypted_password:
return True
else:
return False
# 测试
password = '123456'
encrypted_password = encrypt(password)
print('加密后的密码:', encrypted_password)
print('验证密码是否正确:', decrypt(password, encrypted_password))
```
输出结果如下:
```
加密后的密码: e10adc3949ba59abbe56e057f20f883e
验证密码是否正确: True
```
需要注意的是,MD5加密是不可逆的,所以这里的解密只是一个简单的验证。如果需要更加安全的加密方式,建议使用其他加密算法。
相关问题
python凯撒密码加密解密
以下是Python实现凯撒密码加密和解密的示例代码:
加密:
```python
def caesar_encrypt(plain_text, shift):
"""
凯撒密码加密
:param plain_text: 明文
:param shift: 移位数
:return: 密文
"""
cipher_text = ''
for char in plain_text:
if char.isalpha():
cipher_text += chr((ord(char) - ord('a') + shift) % 26 + ord('a'))
else:
cipher_text += char
return cipher_text
```
解密:
```python
def caesar_decrypt(cipher_text, shift):
"""
凯撒密码解密
:param cipher_text: 密文
:param shift: 移位数
:return: 明文
"""
plain_text = ''
for char in cipher_text:
if char.isalpha():
plain_text += chr((ord(char) - ord('a') - shift + 26) % 26 + ord('a'))
else:
plain_text += char
return plain_text
```
使用示例:
```python
plain_text = 'hello world'
shift = 3
cipher_text = caesar_encrypt(plain_text, shift)
print(cipher_text) # khoor zruog
decrypted_text = caesar_decrypt(cipher_text, shift)
print(decrypted_text) # hello world
```
注:以上代码中,只考虑了小写字母的情况。如果需要加密或解密大写字母或其他字符,请自行修改代码。
凯撒密码加密解密算法Python
凯撒密码是一种简单的替换密码,它是通过将字母表中的每个字母向后(或向前)移动固定数量的位置来进行加密和解密的。这个固定的移动数量被称为偏移量或密钥。
下面是一个用Python实现凯撒密码加密和解密的示例代码:
```python
def caesar_encrypt(text, key):
encrypted_text = ""
for char in text:
if char.isalpha():
if char.isupper():
encrypted_text += chr((ord(char) - 65 + key) % 26 + 65)
else:
encrypted_text += chr((ord(char) - 97 + key) % 26 + 97)
else:
encrypted_text += char
return encrypted_text
def caesar_decrypt(encrypted_text, key):
decrypted_text = ""
for char in encrypted_text:
if char.isalpha():
if char.isupper():
decrypted_text += chr((ord(char) - 65 - key) % 26 + 65)
else:
decrypted_text += chr((ord(char) - 97 - key) % 26 + 97)
else:
decrypted_text += char
return decrypted_text
# 示例用法
text = "Hello, World!"
key = 3
encrypted_text = caesar_encrypt(text, key)
print("加密后的文本:", encrypted_text)
decrypted_text = caesar_decrypt(encrypted_text, key)
print("解密后的文本:", decrypted_text)
```
这段代码中,`caesar_encrypt`函数用于加密文本,`caesar_decrypt`函数用于解密文本。`text`是要加密或解密的文本,`key`是偏移量或密钥,表示字母向后(或向前)移动的位置数。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![py](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)