Здравствуйте!
В общем, мне необходимо хранить рекорд для игры в отдельном текстовом файле. Рекорд шифруется с помощью PyCrypto.
Написал такой скрипт:
# -*- coding: utf-8 -*-
import sys
import hashlib
from Crypto.Cipher import AES
password = "catchfugitive"
key = hashlib.sha256(password).digest()
iv = 16 * '\x00' # Initialization vector
mode = AES.MODE_CBC
time_score = 45
def decryption(key, mode, iv, record):
"""
Decrypt record from file.
Thanks a lot for PyCrypto.
"""
decryptor = AES.new(key, mode, iv)
record[0] = decryptor.decrypt(str(record[0]))
return record
def encryption(key, mode, iv, record):
"""
Encrypt record to file.
Thanks a lot for PyCrypto.
"""
encryptor = AES.new(key, mode, iv)
record[0] = encryptor.encrypt(record[0])
return record
try:
record_file = open("../statistics/record.txt", "r")
except:
sys.exit()
record = [line.strip() for line in record_file]
print(record[0])
if int(record[0]) < time_score:
record[0] = time_score
record_file.close()
try:
record_file = open("../statistics/record.txt", "w")
except:
sys.exit()
record[0] = encryption(key, mode, iv, record[0])
for index in record:
record_file.write(str(index) + "\n")
record_file.close()
Но при запуске выходит следующая ошибка:
Traceback (most recent call last):
File "script.py", line 47, in
record[0] = encryption(key, mode, iv, record[0])
File "script.py", line 27, in encryption
record[0] = encryptor.encrypt(record[0])
TypeError: 'int' object has no attribute '__getitem__'
Как с этим бороться? Пожалуйста, помогите.