Skip to content

Instantly share code, notes, and snippets.

@eruffaldi
Created May 30, 2017 12:52
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 eruffaldi/c0a883f4374ede7677d04dc4e2204af9 to your computer and use it in GitHub Desktop.
Save eruffaldi/c0a883f4374ede7677d04dc4e2204af9 to your computer and use it in GitHub Desktop.
Minimal Example of LFSR
#https://en.wikipedia.org/wiki/Linear-feedback_shift_register
from bitstring import BitArray
def pad(x,K):
N = len(x)
PAD = N % K
if PAD > 0:
PAD = K-PAD
# tell me better
x = x + BitArray(bin='0'*PAD)
return x
def asbits(x):
if isinstance(x,BitArray):
return x
else:
return BitArray(bytes=x)
def lfsr(t,k,wn=0):
t = asbits(t)
k = asbits(k)
q = BitArray()
K = len(k)
N = len(t)
t = pad(t,K)
print "processing: ",N,"with key",K,"extended to",len(t)
for i in range(0,len(t),K):
q += t[i:i+K] ^ k
k.rol(1)
if wn != 0:
q = q[0:wn]
return q,N
def main():
key = "ciao"
input = "ciaomondo"
w,wn = repeated(input,key)
print "encrypted as",len(w),"original len",wn
print repeated(w,key,wn)[0].bytes
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment