Answer the question
In order to leave comments, you need to log in
TypeError: 'int' object has no attribute '__getitem__'?
Hello!
In general, I need to store the record for the game in a separate text file. The record is encrypted using PyCrypto.
Wrote this script:
# -*- 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()
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question