使用py2exe/pygame2exe在exe中隐藏高分文本文件 [英] hiding highscore text file in exe using py2exe/pygame2exe

查看:100
本文介绍了使用py2exe/pygame2exe在exe中隐藏高分文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在游戏中有一个用Pygame制作的高分系统,它将名称和分数保存在文本文件name.txtscore.txt中,但是问题是任何人都可以编辑该文本文件,所以我可以隐藏吗它们放在exe文件中.
python版本2.7
设置脚本: pygame2exe

I have a highscore system in my game that I made in Pygame, it saves name and score in text files name.txt and score.txt but the problem is that anyone can edit that text files.So could i hide them in exe file.
python version 2.7
setup script: pygame2exe

如果可能的话,我可以在没有额外模块的情况下进行操作. 更新:我显然需要以某种方式对文本进行加密.您能建议一些简单加密算法吗? 请注意,我使用数字和换行符.

And if it's possible could I do it without extra modules. UPDATE:I'll obviously need to encrypt text somehow.Can you suggest some simple encryption alghoritham? Note that I use numbers and newlines.

我仍然想将文件放入exe.

推荐答案

对该算法使用强加密没有多大意义,因为您的程序需要访问解密密钥,因此,如果确定的用户查看了您程序的代码,他们便可以得出结论.程序在哪里存储密钥以及它的用途.没错,阅读与学习了解.pyc中的代码比读取.py困难一些,但比破坏良好的加密要容易得多.

There's not much point using strong encryption for this algorithm because your program needs to access the decryption key, so if a determined user looks at your program's code they can figure out where your program stores the key and what it does with it. True, reading & understanding the code in a .pyc is somewhat more difficult than reading a .py, but it's still a lot easier than breaking good encryption.

无论如何,这是一些对任意字符串进行简单加密/解密的代码.如果字符串不是非常随机,则效果最佳;长字符串比短字符串的效果更好.

Anyway, here's some code that does simple encryption / decryption of an arbitrary string. It works best if the string isn't very random, and it works better on long strings than short ones.

要进行加密,首先使用gzip压缩数据,这不仅可以使数据变小,而且可以减少数据中的模式数量,从而提高了加密质量.然后,它会将压缩后的数据中每个字节的整数值与一系列在(256)范围内的随机整数进行异或,以生成编码版本.

To encrypt, it first compresses the data using gzip, which not only can make the data smaller, it reduces the amount of pattern in the data, which improves the quality of the encryption. It then XORs the integer value of each byte (in the zipped data) with a series of random integers in the range(256) to produce the encoded version.

从这个意义上讲,异或运算是对称的:如果a = b ^ c,则b = a ^ c(也c = a ^ b).因此,要撤消XORing,我们只需再次应用即可.

The XOR operation is symmetrical in this sense: if a = b ^ c, then b = a ^ c (and also c = a ^ b). So to undo XORing, we just have to apply it again.

因此,要解密,我们只需反转加密过程即可.首先,我们将编码版本与用于编码的相同系列随机整数异或,然后解压缩.

So to decrypt, we simply reverse the encryption process. First we XOR the encoded version with the same series of random integers we used to encode it and then we unzip it.

如果要隐藏的数据量很小或高度随机,则可以跳过压缩/解压缩步骤,因为对来自良好伪随机数生成器(例如Python的Mersenne Twister)的一系列随机字节进行异或运算假设攻击者既不知道生成您的随机数的算法也不知道随机序列的种子,那么它实际上是一种很好的加密形式. (请注意,Python的random.seed()接受任何可散列的对象作为种子:例如,int,字符串甚至是元组).

If the amount of data you want to hide is fairly small or highly random you can skip the zipping / unzipping steps, since XORing with a series of random bytes from a good pseudo-random number generator (like the Mersenne Twister that Python uses by default) is actually quite a good form of encryption, assuming that the attacker doesn't know both the algorithm that generates your random numbers and the seed for the random sequence. (Note that Python's random.seed() accepts any hashable object as the seed: eg an int, a string, or even a tuple).

下面的代码使用年份日历作为测试数据,以使流程易于运行.

The code below uses a calendar of the year as test data to make it easy to see that the process works.

gzipcrypt.py

#! /usr/bin/env python

''' Simple encryption
    Encode by gziping then XORing bytes with a pseudorandom stream
    Decode by XORing and then unziping with the same pseudorandom stream

    Written by PM 2Ring 2014.09.28
'''

import random

import sys
from calendar import calendar  


def randoms(seed):
    random.seed(seed)
    while True:
        yield random.randint(0, 255)


def xorcrypt(data, key):
    return str(bytearray(d ^ k for d, k in 
        zip(bytearray(data), randoms(key))))


def zipcrypt(data, key):
    return xorcrypt(data.encode('zlib_codec'), key)


def decryptzip(data, key):
    return xorcrypt(data, key).decode('zlib_codec')


def main():
    #Test encryption & decryption
    key = sys.argv[1] if len(sys.argv) > 1 else 42
    data = calendar(2014)

    print data
    print 'Length:', len(data), '\n'

    #code = xorcrypt(data, key)
    code = zipcrypt(data, key)
    print `code` 
    print 'Length:', len(code), '\n'

    #newd = xorcrypt(code, key)
    newd = decryptzip(code, key)
    print newd
    print newd == data


if __name__ == '__main__':
    main()


修改

这是xorcrypt()的简单版本,它不使用生成器函数bytearray()或列出希望更容易理解的列表.

Here's a simpler version of xorcrypt() that doesn't use a generator function, bytearray(), or list comprehensions that's hopefully more comprehensible.

def xorcrypt1(data, key):
    random.seed(key)
    code = []
    for ch in data:
        n = ord(ch)
        n = n ^ random.randint(0, 255)
        code.append(chr(n))
    return ''.join(code)

这篇关于使用py2exe/pygame2exe在exe中隐藏高分文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆