Obfuscating Strings with ASCII and base 128

走远了吗. 提交于 2019-12-09 13:48:13

问题


Suppose a string is a number system where 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

回答1:


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



回答2:


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

No idea about 'encryption'.




回答3:


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.



来源:https://stackoverflow.com/questions/4588612/obfuscating-strings-with-ascii-and-base-128

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!