Guest User

Untitled

a guest
Apr 22nd, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.77 KB | None | 0 0
  1. import sys
  2. import codecs
  3. import textwrap
  4. from math import sin
  5. import numpy as np
  6.  
  7. def notBit (decNum):
  8. binNum = bin(decNum)[2:]
  9. newBinNum = ''
  10. for c in binNum:
  11. if (c == '0'):
  12. newBinNum += '1'
  13. else:
  14. newBinNum += '0'
  15. return int(newBinNum, 2)
  16.  
  17. def convertToBin (hexNum):
  18. binNum = ''
  19. for i in hexNum:
  20. binNum += format(i, '08b')
  21. return(binNum)
  22.  
  23. def convertToDec (hexNum):
  24. binNum = ''
  25. for i in hexNum:
  26. binNum += format(i, '08b')
  27. binNum = int(binNum, 2)
  28. return(binNum)
  29.  
  30. def shift (decNum, n):
  31. binNum = bin(decNum)[2:]
  32. binNum = binNum[n:]+binNum[:n]
  33. return int(binNum, 2)
  34.  
  35. def F(b,c,d):
  36. return (b & c)|(notBit(b) & d)
  37.  
  38. def G(b,c,d):
  39. return (b & d)|(notBit(d) & c)
  40.  
  41. def H(b,c,d):
  42. return b ^ c ^ d
  43.  
  44. def I(b,c,d):
  45. return c ^ (notBit(d) | b)
  46.  
  47. def K(i,r):
  48. return int(2**32 * abs(sin(i + 16 * (r - 1))))
  49.  
  50. def getTable(i,j):
  51. table = np.array([[(1,7),(2,12),(3,17),(4,22),(5,7),(6,12),(7,17),(8,22),(9,7),(10,12),(11,17),(12,22),(13,7),(14,12),(15,17),(16,22)],
  52. [(2,5),(7,9),(12,14),(1,20),(6,5),(11,9),(15,14),(5,20),(10,5),(15,9),(4,14),(9,20),(14,5),(3,9),(8,14),(13,20)],
  53. [(6,4),(9,11),(12,16),(15,23),(2,4),(5,11),(8,16),(11,23),(14,4),(1,11),(4,16),(7,23),(10,4),(13,11),(16,16),(3,23)],
  54. [(1,6),(8,10),(15,15),(6,21),(13,6),(4,10),(11,15),(2,21),(9,6),(16,10),(7,15),(14,21),(5,6),(12,10),(3,15),(10,21)]])
  55. return table[i,j]
  56.  
  57. fun = [F, G, H, I]
  58. name ='Шад'.encode('cp1251')
  59. name2 = convertToDec(codecs.encode('Шад'))
  60. hexName = codecs.encode(name, 'hex')
  61. binName = bin(int.from_bytes(name, byteorder=sys.byteorder))[2:]
  62. binName += '1'
  63. for i in range(0,448 - len(binName)):
  64. binName += '0'
  65. length = bin(len(binName))[2:]
  66. for i in range(0,64 - len(length)):
  67. binName += '0'
  68. binName += length
  69. print(hex(int(binName,2))[2:])
  70. print()
  71. binName = textwrap.wrap(binName, 32)
  72. decName = list(map(lambda x: int(x,2), binName))
  73. a = b'\x67\x45\x23\x01'
  74. b = b'\xef\xcd\xab\x89'
  75. c = b'\x98\xba\xdc\xfe'
  76. d = b'\x10\x32\x54\x76'
  77.  
  78. a = startA = convertToDec(a)
  79. b = startB = convertToDec(b)
  80. c = startC = convertToDec(c)
  81. d = startD = convertToDec(d)
  82.  
  83. for i in range(0,4):
  84. for j in range(0,16):
  85. print(hex(a)[2:])
  86. print(hex(b)[2:])
  87. print(hex(c)[2:])
  88. print(hex(d)[2:])
  89. print()
  90. a, b, c, d = d, (shift(fun[i](b, c, d) ^ a ^ decName[getTable(i,j)[0]-1] ^ K(j+1, i+1), getTable(i,j)[1]) ^ b), b, c
  91.  
  92. print(hex(a)[2:])
  93. print(hex(b)[2:])
  94. print(hex(c)[2:])
  95. print(hex(d)[2:])
  96. print()
  97.  
  98. a = hex(a ^ startA)[2:]
  99. b = hex(b ^ startB)[2:]
  100. c = hex(c ^ startC)[2:]
  101. d = hex(d ^ startD)[2:]
  102.  
  103. print(a)
  104. print(b)
  105. print(c)
  106. print(d)
  107. print()
  108.  
  109. print(d,end = '')
  110. print(c,end = '')
  111. print(b,end = '')
  112. print(a,end = '')
Add Comment
Please, Sign In to add comment