1

目前正在开发 RPG,我在问如何保护保存的数据,以便玩家/用户无法轻松读取或修改它。我的意思是,有计算机和编程经验的人可以修改它,但我不希望 lambda 用户能够修改它,就像修改纯文本 xml 文件一样容易。

有没有办法用python做到这一点?

4

4 回答 4

2

Justpicklecpickle压缩设置为最大的配置对象是一个快速简便的选择。

于 2013-08-09T07:47:47.743 回答
1

您可以使用 zlib 来压缩数据。

savedata="level=3"

import zlib

#when saving
compressedString = zlib.compress(savedata)

#when loading
loaddata = zlib.decompress(compressedString)
于 2013-08-09T07:53:14.880 回答
1

听起来您需要一个密码库。这将帮助您使用密钥加密或解密文件。好在已经有一个叫做 PyCrypto。你可以在这里下载。

要使用它,一旦你下载了它记录在这里

import string
from Crypto.Cipher import AES
from Crypto import Random
from Crypto.Random import random

def gen_cipher():
    # generates 32 letter long key
    key = ''.join(random.sample(string.ascii_letters, 32))
    iv = Random.new().read(AES.block_size)
    cipher = AES.new(key, AES.MODE_CFB, iv)
    return cipher, iv

def write_data(data, rfile, cipher, iv):
    with open(rfile, 'w') as f:
        msg = iv + cipher.encrypt(b'Users cant edit this')
        f.write(msg)

def read_data(rfile, cipher):
    with open(rfile, 'r') as f:
        data = f.read()
        # first 16 bytes are IV
        return cipher.decrypt(data)[16:]

def encrypt_existing_file(infile, outfile, cipher, iv):
    with open(infile, 'r') as if:
        data = if.read()
        write_data(data, outfile, cipher, iv)

def decrypt_existing_file(infile, outfile, cipher, iv):
    with open(outfile, 'r') as of:
        data = read_data(infile)
        of.write(data)

if __name__ == '__main__':
    cipher, iv = gen_cipher()
    write_data(b"You didn't see anything...", 'file.txt', cipher, iv)
    # ...
    # outputs: You didn't see anything...
    print (read_data('file.txt', cipher))

它通过使用 AES 作为对称密钥密码来工作。首先,我从 32 个随机选择的 ascii 字母中生成一个随机密钥。然后我创建一个初始化向量 (iv)。这在加密文件的开头是必要的,以便正确初始化。然后是密码本身,它需要一个密钥、一个操作模式和一个初始化向量。CFB 模式(密码反馈模式)只是意味着 AES 将以下一个块在某种程度上依赖于前一个块的方式使用。Udacity 有几个关于对称密码AESCBC的精彩视频。

于 2013-08-09T08:04:28.077 回答
0

泡菜可能太大了。在某些情况下工作很奇怪。ZIP 允许从存档的不同部分读写数据。

尝试使用密码压缩或更改文件的第一个字节以防止正常解包

PS。制作一些不同的变体并检查尺寸/速度

于 2013-08-09T08:02:45.437 回答