Skip to content

Instantly share code, notes, and snippets.

@Stehlampe2020
Last active January 8, 2024 21:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Stehlampe2020/1c2cb631e2671200b7b620ac6093ed9e to your computer and use it in GitHub Desktop.
Save Stehlampe2020/1c2cb631e2671200b7b620ac6093ed9e to your computer and use it in GitHub Desktop.
n~m_b encoding implemented in Python3, see https://www.youtube.com/watch?v=KCBz_qNLBZA&lc=Ugydlu_IOPb_8ZcYK5l4AaABAg.9zK6QW-fOIN9zKANTHEGj_ for FlyTech's video
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
{N/D}code n~m_b encoded text • Script written by Lampe2020 <kontakt@lampe2020.de> (GitHub:Stehlampe2020)
Encoding definition: n~m_b → n=n bits; m=m'th to last bit flipped; b=binary code that can be decoded to ASCII text if transformations applied
"""
from sys import stderr as STDERR # Make it easy to give output as error
def ncode_nmb(n:int=7, m:int|bool=2, b:str='I am the binary master.') -> str:
"""
Ncode ASCII-encoded text in n~m_b encoding.
Set m to 0 or False to not flip any bit.
"""
n:int = abs(int(n))
m:int = abs(int(m)) if m else False
if n<7:
print(f'ncode_nmb: Warning: {n=} (<7) → information will get lost during encoding and decoding may return garbage!', file=STDERR)
o:list[str] = [
bin(c)[2:] # Convert to string of 1's and 0s, cut off '0b'
.rjust(n, '0') # Pad to required length if shorter
[-n:] # Cut to required length if longer
for c in b.encode('ASCII') # Encode string to ASCII to work with it
]
if m: # If a bit should be flipped
for i in range(len(o)): # Loop over each encoded character
o[i] = ''.join(
o[i][j] if j!=len(o[i])-m else ( # Decide if we need to flip the bit or no
str(abs(int(o[i][j])-1)) # Flip the bit with the power of maths
)
for j in range(len(o[i])) # Loop over every bit in the encoded character
)
return f'{n}~{m}_{"".join(o)}' # Assemble the encoded string
else:
return f'{n}_{"".join(o)}' # Assemble the encoded string
def dcode_nmb(s:str='7~2_10010110100010110001111011110100010111011011010101100111010001011000001101011110110011000111110000111101101000101101111110001111100011110110110011111100000100011') -> str:
"""
Dcode text from n~m_b encoding to normal ASCII.
"""
n:int
m:int|bool
b:str
try:
n_m:str
n_m, b = s.split('_', 1) # Separate metadata from body
try:
n, m = (int(x) for x in n_m.split('~', 1)) # Separate n and m if possible
except ValueError:
n, m = int(n_m), 0 # No m found, setting it to 0.
except ValueError:
n, m, b = 8, False, s
if n<7:
print(f'dcode_nmb: Warning: {n=} (<7) → information has been lost during encoding and decoding may return garbage!', file=STDERR)
o:list[str] = [b[i:i+n] for i in range(0, len(b), n)] # Split data every n'th character
if m:
for i in range(len(o)): # Loop over each encoded character
o[i] = ''.join(
o[i][j] if j!=len(o[i])-m else ( # Decide if we need to flip the bit or not
str(abs(int(o[i][j])-1)) # Flip the bit with the power of maths
)
for j in range(len(o[i])) # Loop over every bit in the encoded character
)
return bytes(int(c, 2) for c in o).decode('ASCII') # Assemble decoded string
if __name__=='__main__':
n_1, m_1, b_1 = 7, 2, 'I am the binary master.'
s_n_1:str = ncode_nmb(n_1, m_1, b_1)
s_d_1:str = dcode_nmb(s_n_1)
n_2, m_2, b_2 = 9, -1, "I'm not a script-kiddie!"
s_n_2:str = ncode_nmb(n_2, m_2, b_2)
s_d_2:str = dcode_nmb(s_n_2)
print(f'''n~m_b encoding test:
ncode_nmb(n={n_1}, m={m_1}, b={repr(b_1)})
→ {repr(s_n_1)}
dcode_nmb(s={repr(s_n_1)})
→ {repr(s_d_1)}
ncode_nmb(n={n_2}, m={m_2}, b={repr(b_2)})
→ {repr(s_n_2)}
dcode_nmb(s={repr(s_n_2)})
→ {repr(s_d_2)}
''')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment