Python高效的字符串混淆

Tim*_*Tim 9 python string unicode

我需要混淆Unicode文本行来减慢那些可能想要提取它们的人的速度.理想情况下,这将使用内置的Python模块或一个小的附加库来完成; 字符串长度将与原始字符串相同或更小; 并且"不混淆"尽可能快.

我尝试了各种字符交换和XOR例程,但它们很慢.Base64和十六进制编码大大增加了大小.到目前为止,我发现最有效的方法是在最低设置下使用zlib进行压缩(1).有没有更好的办法?

Tom*_*ych 10

这对bytes对象使用简单,快速的加密方案.

# For Python 3 - strings are Unicode, print is a function

def obfuscate(byt):
    # Use same function in both directions.  Input and output are bytes
    # objects.
    mask = b'keyword'
    lmask = len(mask)
    return bytes(c ^ mask[i % lmask] for i, c in enumerate(byt))

def test(s):
    data = obfuscate(s.encode())
    print(len(s), len(data), data)
    newdata = obfuscate(data).decode()
    print(newdata == s)


simple_string = 'Just plain ASCII'
unicode_string = ('sensei = \N{HIRAGANA LETTER SE}\N{HIRAGANA LETTER N}'
                  '\N{HIRAGANA LETTER SE}\N{HIRAGANA LETTER I}')

test(simple_string)
test(unicode_string)
Run Code Online (Sandbox Code Playgroud)

Python 2版本:

# For Python 2

mask = 'keyword'
nmask = [ord(c) for c in mask]
lmask = len(mask)

def obfuscate(s):
    # Use same function in both directions.  Input and output are
    # Python 2 strings, ASCII only.
    return ''.join([chr(ord(c) ^ nmask[i % lmask])
                    for i, c in enumerate(s)])

def test(s):
    data = obfuscate(s.encode('utf-8'))
    print len(s), len(data), repr(data)
    newdata = obfuscate(data).decode('utf-8')
    print newdata == s


simple_string = u'Just plain ASCII'
unicode_string = (u'sensei = \N{HIRAGANA LETTER SE}\N{HIRAGANA LETTER N}'
                  '\N{HIRAGANA LETTER SE}\N{HIRAGANA LETTER I}')

test(simple_string)
test(unicode_string)
Run Code Online (Sandbox Code Playgroud)


jte*_*ace 8

旧的ROT13技巧怎么样?

>>> x = 'some string'
>>> y = x.encode('rot13')
>>> y
'fbzr fgevat'
>>> y.decode('rot13')
u'some string'
Run Code Online (Sandbox Code Playgroud)

对于unicode字符串:

>>> x = u'??'
>>> print x
??
>>> y = x.encode('unicode-escape').encode('rot13')
>>> print y
\h570o\h78op
>>> print y.decode('rot13').decode('unicode-escape')
??
Run Code Online (Sandbox Code Playgroud)