Skip to content

Instantly share code, notes, and snippets.

@sundowndev
Created October 12, 2019 15:14
Show Gist options
  • Save sundowndev/367213ef6baeec358c68cf63d410e888 to your computer and use it in GitHub Desktop.
Save sundowndev/367213ef6baeec358c68cf63d410e888 to your computer and use it in GitHub Desktop.
Create a password that can be found/calculated without a computer, using binary to ascii conversion.
#!/usr/bin/env python3
# -*- coding:utf-8 -*-
"""
Create a password that can be found/calculated
without a computer, using binary to ascii conversion.
Example:
Consider the following binary code:
00101011 01011100
01000110 01111000
00110000 01000000
01011010 00101110
Hexa result: 2B 5C 46 78 30 40 5A 2E
Ascii result (wifi password): +\Fx0@Z.
"""
import random
import string
import binascii
import textwrap
def randomword(length):
letters = string.ascii_letters + string.digits + string.punctuation
return ''.join(random.choice(letters) for i in range(length))
def text_to_bits(text, encoding='utf-8', errors='surrogatepass'):
bits = bin(int.from_bytes(text.encode(encoding, errors), 'big'))[2:]
return bits.zfill(8 * ((len(bits) + 7) // 8))
def main(length):
password = randomword(length)
binary = text_to_bits(password)
hexa = textwrap.wrap(hex(int(binary, 2)), 2)
print('Binary tree:')
for i in range(0, length):
text = textwrap.wrap(binary, 8)
print('{}'.format(text[i]))
print('\nPassword: {}'.format(password))
print('Hexa: {}'.format(' '.join(hexa[2:])))
if __name__ == '__main__':
length = 10
main(length)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment