Skip to content

Instantly share code, notes, and snippets.

@InJeCTrL
Created January 28, 2021 07:08
Show Gist options
  • Select an option

  • Save InJeCTrL/41114a7cb39b41bbe89706cf12d23ae0 to your computer and use it in GitHub Desktop.

Select an option

Save InJeCTrL/41114a7cb39b41bbe89706cf12d23ae0 to your computer and use it in GitHub Desktop.
替换01加密
class FunEnc:
def __init__(self):
# self.__zero = 'O'
# self.__one = '0'
# self.__sep = '1'
self.__one = '1'
self.__zero = '0'
self.__sep = ' '
def encrypt(self, s):
result = []
for ch in s:
word = ""
# n = ord(ch)
n = int.from_bytes(ch.encode("utf-8"), 'big')
while n > 0:
word += self.__zero if (n & 1) == 0 else self.__one
n >>= 1
result.append(word[::-1])
return self.__sep.join(result)
def decrypt(self, s):
parts = s.split(self.__sep)
result = []
for part in parts:
n = 0
for ch in part:
n <<= 1
if ch == self.__one:
n |= 1
# result.append(chr(n))
result.append(n.to_bytes(3, byteorder="big").decode("utf-8"))
return "".join(result)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment