Skip to content

Instantly share code, notes, and snippets.

@NISH1001
Last active April 4, 2018 09:57
Show Gist options
  • Save NISH1001/9b29f4ffdd054123d173738307b6a55f to your computer and use it in GitHub Desktop.
Save NISH1001/9b29f4ffdd054123d173738307b6a55f to your computer and use it in GitHub Desktop.
embed hidden messages into standard text/string using zero width characters
ZERO_WIDTH_JOINER = '\u200d'
ZERO_WIDTH_SPACE = '\u200b'
ZERO_WIDTH_NON_JOINER = '\u200c'
ZERO_WIDTH_NO_BREAK_SPACE = '\ufeff'
def zeropad(num, padding=8):
return str(num).zfill(padding)
def num_to_binary(num):
return bin(num)[2:]
def text_to_binary(username):
res = []
for c in username:
binary = num_to_binary(ord(c[0]))
res.append(zeropad(binary))
return ' '.join(res)
def binary_to_text(string):
bins = string.split(' ')
res = []
for b in bins:
res.append(chr(int(b, 2)))
return ''.join(res)
def binary_to_zerowidth(binary):
res = []
for b in binary:
if b == ' ':
res.append(ZERO_WIDTH_JOINER)
elif int(b) == 1:
res.append(ZERO_WIDTH_SPACE)
elif int(b) == 0:
res.append(ZERO_WIDTH_NON_JOINER)
return ZERO_WIDTH_NO_BREAK_SPACE.join(res)
def zerowidth_to_binary(string):
chars = string.split(ZERO_WIDTH_NO_BREAK_SPACE)
res = []
for c in chars:
if c == ZERO_WIDTH_SPACE:
res.append('1')
elif c == ZERO_WIDTH_NON_JOINER:
res.append('0')
elif c == ZERO_WIDTH_JOINER:
res.append(' ')
return ''.join(res)
def main():
hidden_string = "fuck you"
string = "i am paradox"
print("Before encoding, Original Message:\n{}".format(string))
string_binary = text_to_binary(hidden_string)
string_zeropad = binary_to_zerowidth(string_binary)
string_enc = string + string_zeropad
print("After encoding with hidden message '{}' : \n{}".format(hidden_string, string_enc))
zb = zerowidth_to_binary(string_zeropad)
hidden = binary_to_text(zb)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment