Skip to content

Instantly share code, notes, and snippets.

@AndyNovo
Created February 26, 2016 13:27
Show Gist options
  • Save AndyNovo/0d364992fe1c17d8526b to your computer and use it in GitHub Desktop.
Save AndyNovo/0d364992fe1c17d8526b to your computer and use it in GitHub Desktop.
def gen_8_bits(state, length=128):
output=0
for i in range(8):
next_out = state%2
next_state_bit = next_out ^ ((state>>1)%2) ^ ((state>>2)%2) ^ ((state>>7)%2)
state = (state >> 1) | (next_state_bit << (length-1))
output = (output << 1) | next_out
return output, state
key = int("".join(['1' for i in range(128)]),2)
#hex digest to hex digest
def hex_crypt(plain, key):
cipher = ''
state = key
for hc in [plain[i:i+2] for i in range(0,len(plain),2)]:
output, state = gen_8_bits(state)
cipher += hex(int(hc,16)^output)[2:-1].zfill(2)
return cipher
import binascii
def encrypt(plain, key):
digest = binascii.hexlify(plain)
return hex_crypt(digest, key)
def decrypt(ct, key):
pt_hex = hex_crypt(ct, key)
return binascii.unhexlify(pt_hex)
ct = encrypt("*** *** *** *** Andy is the best.", key)
print ct
print decrypt(ct, key)
#BTW binascii.unhexlify(ct) == '\xd5\xd5\xd5\xdf\xd5\xd5\xd5\xdf\xd5\xd5\xd5\xdf\xd5\xd5\xd5\xdfAndy is the gre\x1ctest. Just waiK\xdd'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment