我在循环文件列表时遇到错误。该错误仅在循环中运行时发生。
我已经确保所有的文件都是有效的,我已经运行了下面的代码,用整数替换for循环中的x,它突然工作得很好。
src = open(SF_list[x]).read()
SE.data = SFC.decrypt(src)
print(f'[{x}] - {SE.getName()}({SF_names[x]})')
也许你们可以帮我,提前谢谢!
错误:
Traceback (most recent call last):
File "c:\Users\USER\Documents\Projects\PROJECT\Interface.py", line 19, in <module>
SE.data = SFC.decrypt(src)
File "c:\Users\USER\Documents\Projects\PROJECT\SFCrypto.py", line 19, in decrypt
return json.loads(save)
File "C:\Python310\lib\json\__init__.py", line 341, in loads
s = s.decode(detect_encoding(s), 'surrogatepass')
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xac in position 2: invalid start byte
Interface.py
import SFCrypto as SFC
SF_folder = os.getenv('LOCALAPPDATA')+'\EXAMPLEPATH'
SF_list = glob.glob(SF_folder + "/*.FILEENDING")
SF_names = [os.path.splitext(os.path.basename(file))[0] for file in SF_list]
vault = ''
if __name__ == '__main__':
if vault == '':
for x in range(SF_list.__len__()):
src = open(SF_list[x]).read()
SE.data = SFC.decrypt(src)
print(f'[{x}] - {SE.getName()}({SF_names[x]})')
SFCrypto:
def decrypt(save: str) -> dict:
save = base64.b64decode(save)
save = CIPHER.decrypt(save)
padding_symbol = save[-1]
if padding_symbol < 16:
save = save[:-padding_symbol]
return json.loads(save)
def encrypt(save: dict) -> str:
save = json.dumps(save, separators=(',', ':'))
save = save.encode()
tail = len(save) % 16
if tail > 0:
padding_symbol = 16 - tail
save += bytes((padding_symbol,)) * padding_symbol
save = CIPHER.encrypt(save)
return base64.b64encode(save).decode()
1条答案
按热度按时间z2acfund1#
发现了这个bug。当使用www.example.com()从同一个“CIPHER”解密多个文件时,它会发生AES.new,我把它放在同一个函数中而不是全局函数中,现在它工作得很好。