shitchell

InfoSec Assignment 7 - LFSR.py

Oct 31st, 2020 (edited)
1,531
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/env python
  2.  
  3. import sys
  4.  
  5. def main():
  6.     if len(sys.argv) > 1:
  7.         registers = sys.argv[1]
  8.     else:
  9.         registers = '1001'
  10.  
  11.     # Ensure only 1's and 0's are present
  12.     for reg in registers:
  13.         if reg not in '01':
  14.             print('Must only use 1s and 0s', file=sys.stderr)
  15.             sys.exit(1)
  16.  
  17.     # Convert registers to a list of ints
  18.     registers = [int(x) for x in registers]
  19.  
  20.     output = ""
  21.     for i in range(0, 15):
  22.         # Display the registers before any manipulation
  23.         print("[registers]", *registers, end=" ")
  24.         # XOR the last 2 registers
  25.         r1 = registers[-2]
  26.         r2 = registers[-1]
  27.         xor = r1 ^ r2
  28.         # Add the last register to the end of the output
  29.         output += str(registers.pop())
  30.         # Display infoz
  31.         print("[xor]", "{} ^ {} = {}".format(r1, r2, xor), end=" ")
  32.         print("[output]", output)
  33.         # Insert the XOR result to the beginning of the registers
  34.         registers.insert(0, xor)
  35.  
  36. if __name__ == '__main__':
  37.     main()
  38.  
RAW Paste Data