1 | using System; |
---|

2 | using System.Collections.Generic; |
---|

3 | using System.Linq; |
---|

4 | using System.Text; |
---|

5 | using System.Collections; |
---|

6 | namespace Cryptool.SystemOfEquations |
---|

7 | { |
---|

8 | class LFSR |
---|

9 | { |
---|

10 | public int length; |
---|

11 | string feedbackpolynom; |
---|

12 | public bool[][] internalstate; |
---|

13 | public char[] outputcells; |
---|

14 | public LFSR(string charpolynom) |
---|

15 | { |
---|

16 | feedbackpolynom = charpolynom; |
---|

17 | length = charpolynom.Length; |
---|

18 | internalstate = new bool[length][]; |
---|

19 | for (int i = 0; i < internalstate.Length; i++) |
---|

20 | { |
---|

21 | internalstate[i] = new bool[length]; |
---|

22 | for (int j = 0; j < internalstate.Length; j++) |
---|

23 | { |
---|

24 | if (i == j) internalstate[i][j] = true; |
---|

25 | else internalstate[i][j] = false; |
---|

26 | } |
---|

27 | } |
---|

28 | } |
---|

29 | public bool[] XorCells(bool[] cell1, bool[] cell2) |
---|

30 | { |
---|

31 | bool[] res = new bool[cell2.Length]; |
---|

32 | for (int i = 0; i < cell2.Length; i++) |
---|

33 | { |
---|

34 | res[i] = cell1[i] ^ cell2[i]; |
---|

35 | } |
---|

36 | return res; |
---|

37 | } |
---|

38 | public void OneClock() |
---|

39 | { |
---|

40 | char[] c = feedbackpolynom.ToCharArray(); |
---|

41 | bool[] res = new bool[length]; |
---|

42 | res = internalstate[0]; |
---|

43 | for (int k = 1; k < length; k++) |
---|

44 | { |
---|

45 | if (c[k] == '1') res = XorCells(res, internalstate[k]); |
---|

46 | } |
---|

47 | for (int j = 0; j < internalstate.Length - 1; j++) |
---|

48 | { |
---|

49 | internalstate[j] = internalstate[j + 1]; |
---|

50 | } |
---|

51 | internalstate[length - 1] = res; |
---|

52 | } |
---|

53 | public void Clock(int time) |
---|

54 | { |
---|

55 | for (int i = 0; i < time; i++) |
---|

56 | { |
---|

57 | OneClock(); |
---|

58 | } |
---|

59 | } |
---|

60 | public ArrayList Output() |
---|

61 | { |
---|

62 | ArrayList lfsroutput = new ArrayList(); |
---|

63 | for (int i = 0; i < outputcells.Length; i++) |
---|

64 | { |
---|

65 | if (outputcells[i] == '1') |
---|

66 | lfsroutput.Add(internalstate[i]); |
---|

67 | } |
---|

68 | return lfsroutput; |
---|

69 | } |
---|

70 | } |
---|

71 | } |
---|