Guest User

Untitled

a guest
Jan 15th, 2019
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.94 KB | None | 0 0
  1. from Crypto.Cipher import DES, AES
  2. def enclen(p):
  3. return ((16 - p % 16) & 0xF) + p + 4
  4.  
  5. def rsb(a, b):
  6. return (0 - a + b) & 0xffffffff
  7.  
  8. def ands(a, b):
  9. return (a & b) & 0xffffffff
  10.  
  11.  
  12. def _ror(val, bits, bit_size):
  13. return ((val & (2 ** bit_size - 1)) >> bits % bit_size) |
  14. (val << (bit_size - (bits % bit_size)) & (2 ** bit_size - 1))
  15.  
  16.  
  17. def __ROR4__(a, b):
  18. return _ror(a, b, 32)
  19.  
  20.  
  21. def eor(a, b):
  22. return (a ^ b) & 0xffffffff
  23.  
  24.  
  25. class Crypto(object):
  26. def __init__(self):
  27. self.key = b'!*ss!_defaul%t54'
  28. self.kl = 0x10
  29. self.sbox0 = bytes.fromhex(
  30. '637C777BF26B6FC53001672BFED7AB76CA82C97DFA5947F0ADD4A2AF9CA472'
  31. 'C0B7FD9326363FF7CC34A5E5F171D8311504C723C31896059A071280E2EB27'
  32. 'B27509832C1A1B6E5AA0523BD6B329E32F8453D100ED20FCB15B6ACBBE394A'
  33. '4C58CFD0EFAAFB434D338545F9027F503C9FA851A3408F929D38F5BCB6DA21'
  34. '10FFF3D2CD0C13EC5F974417C4A77E3D645D197360814FDC222A908846EEB8'
  35. '14DE5E0BDBE0323A0A4906245CC2D3AC629195E479E7C8376D8DD54EA96C56'
  36. 'F4EA657AAE08BA78252E1CA6B4C6E8DD741F4BBD8B8A703EB5664803F60E61'
  37. '3557B986C11D9EE1F8981169D98E949B1E87E9CE5528DF8CA1890DBFE64268'
  38. '41992D0FB054BB16')
  39. self.plen = 0
  40.  
  41. def crypt(self, payload):
  42. if not isinstance(payload, bytes):
  43. payload = payload.encode('utf8')
  44. self.plen = len(payload)
  45. i = 0
  46. r = [00] * 16
  47. while i < self.kl:
  48. a = self.key[i]
  49. r[i] = self.sbox0[a]
  50. i += 1
  51.  
  52. t = int.from_bytes(r[:4], 'big')
  53. r[:4] = int.to_bytes(t, 4, 'little')
  54. t = int.from_bytes(r[4:8], 'big')
  55. r[4:8] = int.to_bytes(t, 4, 'little')
  56. t = int.from_bytes(r[8:12], 'big')
  57. r[8:12] = int.to_bytes(t, 4, 'little')
  58. t = int.from_bytes(r[12:16], 'big')
  59. r[12:16] = int.to_bytes(t, 4, 'little')
  60. b = rsb(self.plen, 0)
  61. b = ands(b, 0xf)
  62. c = b + self.plen + 4
  63.  
  64. result = [00] * enclen(self.plen)
  65. result[0] = 0x74
  66. result[1] = 0x63
  67. result[2] = 0x02
  68. result[3] = b
  69. result[4:len(payload) + 4] = payload
  70.  
  71. i = 4
  72. while i != c:
  73. a = result[i]
  74. result[i] = self.sbox0[a]
  75. i += 1
  76.  
  77. a = c - 4
  78. b = 0
  79. a = a >> 4
  80. d = 4
  81. while b < a:
  82. c = int.from_bytes(result[d:d + 4], 'big')
  83. e = int.from_bytes(r[:4], 'little')
  84. c ^= e
  85. result[d:d + 4] = int.to_bytes(c, 4, 'big')
  86. c = int.from_bytes(result[d + 4:d + 8], 'big')
  87. e = int.from_bytes(r[4:8], 'little')
  88. c = eor(e, __ROR4__(c, 24))
  89. result[d + 4:d + 8] = int.to_bytes(c, 4, 'big')
  90. c = int.from_bytes(result[d + 8:d + 12], 'big')
  91. e = int.from_bytes(r[8:12], 'little')
  92. c = eor(e, __ROR4__(c, 16))
  93. result[d + 8:d + 12] = int.to_bytes(c, 4, 'big')
  94. c = int.from_bytes(result[d + 12:d + 16], 'big')
  95. e = int.from_bytes(r[12:16], 'little')
  96. c = eor(e, __ROR4__(c, 8))
  97. result[d + 12:d + 16] = int.to_bytes(c, 4, 'big')
  98. b += 1
  99. d += 0x10
  100. return bytes(result)
Add Comment
Please, Sign In to add comment