Created
January 28, 2021 07:08
-
-
Save InJeCTrL/41114a7cb39b41bbe89706cf12d23ae0 to your computer and use it in GitHub Desktop.
替换01加密
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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