用python写一个密码加解密程序
时间: 2023-11-25 16:06:19 浏览: 43
以下是一个简单的密码加解密程序示例:
```python
def encrypt(password):
# 将密码转换为 ASCII 码
ascii_password = [ord(char) for char in password]
# 将 ASCII 码加上 1
encrypted_password = [char + 1 for char in ascii_password]
# 将加密后的 ASCII 码转换为字符串
encrypted_password = ''.join([chr(char) for char in encrypted_password])
return encrypted_password
def decrypt(password):
# 将密码转换为 ASCII 码
ascii_password = [ord(char) for char in password]
# 将 ASCII 码减去 1
decrypted_password = [char - 1 for char in ascii_password]
# 将解密后的 ASCII 码转换为字符串
decrypted_password = ''.join([chr(char) for char in decrypted_password])
return decrypted_password
# 测试
password = 'hello world'
encrypted_password = encrypt(password)
print('加密后的密码为:', encrypted_password)
decrypted_password = decrypt(encrypted_password)
print('解密后的密码为:', decrypted_password)
```
此程序将输入的密码转换为 ASCII 码,并将每个字符的 ASCII 码加上 1,得到加密后的密码。解密时将每个字符的 ASCII 码减去 1,得到原始密码。注意,此程序并不是非常安全的密码加密方案,只是一个简单的示例。