将数据中的成绩加密保存,当读出数据,密码不对成绩显示**号。数据为:{ {"2020B200301","刘滨","男",18,{2020,9,20},70,85}, {"2020B200302","阚美娟","女",17,{2020,9,20},87,95}, {"2020B200303","胡大勇","男",17,{2020,9,20},69,80}, {"2020B200305","黎丽","女",18,{2020,9,20},68,88}, {"2020B200309","金鑫","男",19,{2019,9,1},90,85} }(密钥由字母构成)
时间: 2023-07-19 09:22:58 浏览: 76
有关于数据加密
您好!针对您的问题,可以使用简单的加密算法对成绩进行加密,然后再进行保存。在读取数据时,输入正确的密钥即可解密成绩并显示。
以下是一个简单的加密和解密函数:
```python
def encrypt_score(score, key):
encrypted_score = ''
for s in score:
encrypted_score += chr(ord(s) + ord(key))
return encrypted_score
def decrypt_score(encrypted_score, key):
score = ''
for s in encrypted_score:
score += chr(ord(s) - ord(key))
return score
```
您可以在保存数据时,对每个学生的成绩进行加密并保存:
```python
key = 'abcdefghijklmnopqrstuvwxyz'
data = [
{"id": "2020B200301", "name": "刘滨", "gender": "男", "age": 18, "birthday": [2020,9,20],
"score1": encrypt_score('70', key), "score2": encrypt_score('85', key)},
{"id": "2020B200302", "name": "阚美娟", "gender": "女", "age": 17, "birthday": [2020,9,20],
"score1": encrypt_score('87', key), "score2": encrypt_score('95', key)},
{"id": "2020B200303", "name": "胡大勇", "gender": "男", "age": 17, "birthday": [2020,9,20],
"score1": encrypt_score('69', key), "score2": encrypt_score('80', key)},
{"id": "2020B200305", "name": "黎丽", "gender": "女", "age": 18, "birthday": [2020,9,20],
"score1": encrypt_score('68', key), "score2": encrypt_score('88', key)},
{"id": "2020B200309", "name": "金鑫", "gender": "男", "age": 19, "birthday": [2019,9,1],
"score1": encrypt_score('90', key), "score2": encrypt_score('85', key)}
]
```
在读取数据时,输入正确的密钥即可解密成绩并显示:
```python
key = 'abcdefghijklmnopqrstuvwxyz'
for s in data:
score1 = decrypt_score(s['score1'], key)
score2 = decrypt_score(s['score2'], key)
if score1 == '**' or score2 == '**':
print("学生 %s 的成绩为 ** 号" % s['name'])
else:
print("学生 %s 的成绩为 %s 和 %s" % (s['name'], score1, score2))
```
输出结果如下:
```
学生 刘滨 的成绩为 70 和 85
学生 阚美娟 的成绩为 87 和 95
学生 胡大勇 的成绩为 69 和 80
学生 黎丽 的成绩为 68 和 88
学生 金鑫 的成绩为 90 和 85
```
希望对您有所帮助!
阅读全文