繁体   English   中英

在运行eval而不删除空字节的情况下如何解决“源代码字符串不能包含空字节”

[英]How to get around “source code string cannot contain null bytes” when eval is run without removing the null bytes

我有一个用于加密class.__dict__并将其保存到数组中的文件中的程序,因此该文件看起来像这样-

{'some name':[1, 2, 3], 
 'data':'''𚶩9È𚵶𚶛𚵝X§Ë¡¡Ö©𚶕 î𚶣o𚶥[𚵶𚶎 y𚵽𚵮^𚵜𚶩5Ð¥"¢§«!𚵩𚶅𚶚𚵰fa¥𚶯m𚵬c𚶮^𚵵W𚵴𚶭''' #somthing like this but a lot longer
}

然后,我读取它并需要解密数据,但是在此之前,我需要从当前是eval(fileContent)的字符串的数组中获取数据,这给了我错误-

Traceback (most recent call last):
  File "C:/Users/stemb/Documents/programing/python/programs/img editor/__init__.py", line 127, in 
<module>
    main()
  File "C:/Users/stemb/Documents/programing/python/programs/img editor/__init__.py", line 102, in main
    save_code.auto_save_load()
  File "C:\Users\stemb\Documents\programing\python\programs\img editor\save_code.py", line 153, in 
auto_save_load
    data = eval(fileContent)
ValueError: source code string cannot contain null bytes

我的阅读功能是这样
data = open("./save." + GLOBAL.fileType, "r", encoding="utf-8").read()

json.loads提供json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 1 column 2 (char 1)

我的代码是-

    # imports
import json
import random
import time

print("Finished save_progress imports")


def encrypt(obj, log=0): #encrypt
    # set data
    data = str(obj.__dict__)
    key = "codeAsciEightAndMabyA_Seven" + str(time.time())  # crate key
    key = list(key)
    random.shuffle(key)  # randomize key
    cryptionKeys = list()
    encrypted = list()
    iMinus = 0

    for i in range(len(data)):  # create a random + or - for extra security
        cryptionKeys.append(random.choice(("+", "-")))

    # encode part
    for i, c in enumerate(data):
        # set individual data
        charAsci = ord(c)
        cryptionKey = cryptionKeys[i]

        done = 0
        while done == 0:
            try:
                charKey = ord(key[i - iMinus])
                done = 1

            except IndexError:
                iMinus += len(key)

        if cryptionKey == "+":
            encryptedOrd = charAsci + charKey

        else:
            encryptedOrd = charAsci - charKey

            if encryptedOrd < 0:
                encryptedOrd += 110000

                cryptionKeys[i] = "="
                cryptionKey = cryptionKeys[i]

        encryptedChar = chr(encryptedOrd)
        encrypted.append(encryptedChar)

        if log == 1:
            print("charNumb \/")
            print(i)
            print("charAsci \/")
            print(charAsci)
            print("cryptionKey \/")
            print(cryptionKey)
            print("charKey \/")
            print(charKey)
            print("encryptedOrd \/")
            print(encryptedOrd)
            print("encryptedChar \/")
            print(encryptedChar)

            print()

    encrypted2 = encrypted
    encrypted = ""

    for c in encrypted2:
        encrypted += c

    return str(encrypted), str(key), str(cryptionKeys)





def auto_save(GLOBAL): # the save func
    file = open("./save." + GLOBAL.fileType, "w", encoding="utf-8")
    encryptedGLOBAL, key, cryptionKeys = encrypt(GLOBAL)

    out = ("{'key':" + str(key) + ", 'cryptionKeys':" + str(cryptionKeys) + ", 'data':'''" + str(
        encryptedGLOBAL) + "'''}")

    print(out)
    file.write(out)

    file.close()


def auto_save_load(aclass): # the loading dunc
    data = open("./save." + GLOBAL.fileType, "r", encoding="utf-8").read()

    data = eval(data)

    key = data["key"]
    cryptionKeys = data["cryptionKeys"]
    encryptedGLOBAL = data["data"]

    print(key)
    print()
    print(cryptionKeys)
    print()
    print(encryptedGLOBAL)

其他答案说要删除空字节,但是加密方法需要它们。 请帮忙。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM