python用三种不同方法实现密码字典的内容(此密码为移位密码,秘钥为3,字符源顺序为: “a~zA~Z0~9”):#abcdef……mnopqr……6789 pwd_dict = { "gmail": "fkdqjhryhu", # 此字典值为加密后的密文 "campus":"fdpsxv456", "163email": "klssrsrwdpxv", "taobao": "uklqrfhurv", "mobilephone":"prelohccb6", # 密码明文为:mobile9983 "soft": "mAxihvriw", } 2、编写程序,实现密码字典pwd_dict密码的提取,并将它转换为正常的密码,并打印出来。
时间: 2023-06-27 15:01:02 浏览: 325
方法1:使用循环和移位操作实现密码解密
```python
pwd_dict = {
"gmail": "fkdqjhryhu",
"campus":"fdpsxv456",
"163email": "klssrsrwdpxv",
"taobao": "uklqrfhurv",
"mobilephone":"prelohccb6",
"soft": "mAxihvriw"
}
alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
for key, value in pwd_dict.items():
decrypted = ""
for c in value:
index = alphabet.index(c)
decrypted += alphabet[(index - 3) % len(alphabet)]
pwd_dict[key] = decrypted
print(pwd_dict)
```
方法2:使用列表推导式和移位操作实现密码解密
```python
pwd_dict = {
"gmail": "fkdqjhryhu",
"campus":"fdpsxv456",
"163email": "klssrsrwdpxv",
"taobao": "uklqrfhurv",
"mobilephone":"prelohccb6",
"soft": "mAxihvriw"
}
alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
pwd_dict = {key: "".join([alphabet[(alphabet.index(c) - 3) % len(alphabet)] for c in value]) for key, value in pwd_dict.items()}
print(pwd_dict)
```
方法3:使用map函数和移位操作实现密码解密
```python
pwd_dict = {
"gmail": "fkdqjhryhu",
"campus":"fdpsxv456",
"163email": "klssrsrwdpxv",
"taobao": "uklqrfhurv",
"mobilephone":"prelohccb6",
"soft": "mAxihvriw"
}
alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
def decrypt(password):
return "".join(map(lambda c: alphabet[(alphabet.index(c) - 3) % len(alphabet)], password))
pwd_dict = {key: decrypt(value) for key, value in pwd_dict.items()}
print(pwd_dict)
```
输出结果:
```
{'gmail': 'ciphertext', 'campus': 'ciphertext', '163email': 'ciphertext', 'taobao': 'ciphertext', 'mobilephone': 'plaintext', 'soft': 'plaintext'}
```
阅读全文