Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
600 views
in Technique[技术] by (71.8m points)

python - 在运行eval而不删除空字节的情况下如何解决“源代码字符串不能包含空字节”(How to get around “source code string cannot contain null bytes” when eval is run without removing the null bytes)

I have a program where I encrypt class.__dict__ and save it to a file in an array so the file looks something like this -

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

{'some name':[1, 2, 3], 
 'data':'''??9è??????X§??????? ???o??[???? y????^????5D¥"¢§?!????????fa¥??m??c??^??W????''' #somthing like this but a lot longer
}

I then read it and need to decrypt data but before that, I need to get the data from the array that is currently a string which I did with eval(fileContent) and it gives me the error -

(然后,我读取它并需要解密数据,但是在此之前,我需要从数组中获取数据,该数组是我使用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:UsersstembDocumentsprogramingpythonprogramsimg editorsave_code.py", line 153, in 
auto_save_load
    data = eval(fileContent)
ValueError: source code string cannot contain null bytes

My reading function is this

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

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

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

My code is -

(我的代码是-)

    # 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)

Other answers have said to remove the null bytes but the encryption method needs them.

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

Please help.

(请帮忙。)

  ask by Stemboy4002 translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...