开发者

Obfuscating Strings with ASCII and base 128

Suppose a string is a number system 开发者_如何学Cwhere each thing, it can be a char, DEL or any ASCII thing, has a corresponding number according to this ASCII table. How can you convert arbitrary string of the property to number in Python?

An example

#car = 35*128**3+99*128**2+97*128**1+114*128**0=75034866


Try this:

total = 0
for c in "#car":
    total <<= 7
    total += ord(c)
print total

Result:

75034866

To get back the original string:

result = []
while total:
    result.append(chr(total % 128))
    total >>= 7
print ''.join(reversed(result))

Result:

#car


For arbitrarily long numbers, use Decimal in Python 2.x and just int in Python 3.x.

No idea about 'encryption'.


You're looking to find a mapping to obfuscate the data.

Real encryption requires the use of a trap door function - a function that is computationally easy to compute one way, but the inverse of which is difficult to compute without specific information.

Most encryption is based on prime factorization. Alice multiplies two large prime numbers together, and gives the results to Bob. Bob uses this large prime number to encrypt his data using an encryption function. Finding the inverse of Bob's encryption function requires knowing the two original prime numbers (encryption does not). Finding these numbers is a very computationally expensive task, so the encrypted data is 'safe'.

Implementing this correctly is VERY difficult. If you want to keep data safe, find a library that does it for you.

EDIT: I should specify that what I described was public key encryption. Private key encryption works a bit differently. The important thing is that there's a mathematical basis for thinking that encrypted data will be hard to decrypt without a key or some sort.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜