Advertisement
HeroBrine1st

Crypt.lua

Apr 20th, 2018
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.80 KB | None | 0 0
  1. --[[ Алгоритм шифрования-дешифрования на основе сети Фейстеля
  2. Created by Dimus
  3. No rights reserved ]]
  4.  
  5. --[[ Нахождение хеш-функции по алгоритму md5
  6. *Apokalypsys, Dimus
  7. *http://minecrafting.ru/forum/viewtopic.php?f=32&t=5331
  8. ]]
  9.  
  10. local bit = require("bit32")
  11. local hex_char = "0123456789abcdef"
  12. local crypt = {}
  13. local function getHex(seed)
  14. local str = ""
  15. for i = 0, 3 do
  16. local ind1, ind2 = bit.band(bit.rshift(seed, i * 8 + 4), 15) + 1, bit.band(bit.rshift(seed, i * 8), 15) + 1
  17. str = str..
  18. hex_char:sub(ind1, ind1)..
  19. hex_char:sub(ind2, ind2)
  20. end
  21. return str
  22. end
  23.  
  24. local function string_to_blks(str)
  25. local nblk = bit.rshift((str:len() + 8), 6) + 1
  26. local blks = {}
  27. local len = str:len()
  28. for i = 0, nblk * 16 - 1 do
  29. blks[i] = 0
  30. end
  31. for i = 0, str:len() - 1 do
  32. blks[bit.rshift(i, 2)] =
  33. bit.bor(
  34. blks[bit.rshift(i, 2)],
  35. bit.lshift(
  36. str:byte(i+1),
  37. (((i) % 4) * 8)
  38. )
  39. )
  40. end
  41. blks[bit.rshift(len, 2)] =
  42. bit.bor(
  43. blks[bit.rshift(len, 2)],
  44. bit.lshift(
  45. 128,
  46. (((len) % 4) * 8)
  47. )
  48. )
  49. blks[nblk * 16 - 2] = len * 8
  50. return blks
  51. end
  52.  
  53. local function add(x, y)
  54. return x + y > 4294967296 and x + y or x + y - 4294967296
  55. end
  56.  
  57. local function rol(number, count)
  58. return bit.bor(bit.lshift(number, count), bit.rshift(number, (32 - count)))
  59. end
  60.  
  61. local function X(a, b, c, x, s, t)
  62. return add(rol(add(add(b, a), add(x, t)), s), c)
  63. end
  64.  
  65. local function F(a, b, c, d, x, s, t)
  66. return X(bit.bor(bit.band(b, c), bit.band(bit.bnot(b), d)), a, b, x, s, t)
  67. end
  68.  
  69. local function G(a, b, c, d, x, s, t)
  70. return X(bit.bor(bit.band(b, d), bit.band(c, bit.bnot(d))), a, b, x, s, t)
  71. end
  72.  
  73. local function H(a, b, c, d, x, s, t)
  74. return X(bit.bxor(bit.bxor(b, c), d), a, b, x, s, t)
  75. end
  76.  
  77. local function I(a, b, c, d, x, s, t)
  78. return X(bit.bxor(c, bit.bor(b, bit.bnot(d))), a, b, x, s, t)
  79. end
  80.  
  81. function crypt.md5(encoding_string)
  82. local blks = string_to_blks(encoding_string)
  83.  
  84. local a = 1732584193
  85. local b = -271733879
  86. local c = -1732584194
  87. local d = 271733878
  88.  
  89. for i = 0, #blks-1, 16 do
  90. local olda, oldb, oldc, oldd = a, b, c, d
  91.  
  92. a = F(a, b, c, d, blks[i+ 0], 7, -680876936)
  93. d = F(d, a, b, c, blks[i+ 1], 12, -389564586)
  94. c = F(c, d, a, b, blks[i+ 2], 17, 606105819)
  95. b = F(b, c, d, a, blks[i+ 3], 22, -1044525330)
  96. a = F(a, b, c, d, blks[i+ 4], 7, -176418897)
  97. d = F(d, a, b, c, blks[i+ 5], 12, 1200080426)
  98. c = F(c, d, a, b, blks[i+ 6], 17, -1473231341)
  99. b = F(b, c, d, a, blks[i+ 7], 22, -45705983)
  100. a = F(a, b, c, d, blks[i+ 8], 7, 1770035416)
  101. d = F(d, a, b, c, blks[i+ 9], 12, -1958414417)
  102. c = F(c, d, a, b, blks[i+10], 17, -42063)
  103. b = F(b, c, d, a, blks[i+11], 22, -1990404162)
  104. a = F(a, b, c, d, blks[i+12], 7, 1804603682)
  105. d = F(d, a, b, c, blks[i+13], 12, -40341101)
  106. c = F(c, d, a, b, blks[i+14], 17, -1502002290)
  107. b = F(b, c, d, a, blks[i+15], 22, 1236535329)
  108.  
  109. a = G(a, b, c, d, blks[i+ 1], 5, -165796510)
  110. d = G(d, a, b, c, blks[i+ 6], 9, -1069501632)
  111. c = G(c, d, a, b, blks[i+11], 14, 643717713)
  112. b = G(b, c, d, a, blks[i+ 0], 20, -373897302)
  113. a = G(a, b, c, d, blks[i+ 5], 5, -701558691)
  114. d = G(d, a, b, c, blks[i+10], 9, 38016083)
  115. c = G(c, d, a, b, blks[i+15], 14, -660478335)
  116. b = G(b, c, d, a, blks[i+ 4], 20, -405537848)
  117. a = G(a, b, c, d, blks[i+ 9], 5, 568446438)
  118. d = G(d, a, b, c, blks[i+14], 9, -1019803690)
  119. c = G(c, d, a, b, blks[i+ 3], 14, -187363961)
  120. b = G(b, c, d, a, blks[i+ 8], 20, 1163531501)
  121. a = G(a, b, c, d, blks[i+13], 5, -1444681467)
  122. d = G(d, a, b, c, blks[i+ 2], 9, -51403784)
  123. c = G(c, d, a, b, blks[i+ 7], 14, 1735328473)
  124. b = G(b, c, d, a, blks[i+12], 20, -1926607734)
  125.  
  126. a = H(a, b, c, d, blks[i+ 5], 4, -378558)
  127. d = H(d, a, b, c, blks[i+ 8], 11, -2022574463)
  128. c = H(c, d, a, b, blks[i+11], 16, 1839030562)
  129. b = H(b, c, d, a, blks[i+14], 23, -35309556)
  130. a = H(a, b, c, d, blks[i+ 1], 4, -1530992060)
  131. d = H(d, a, b, c, blks[i+ 4], 11, 1272893353)
  132. c = H(c, d, a, b, blks[i+ 7], 16, -155497632)
  133. b = H(b, c, d, a, blks[i+10], 23, -1094730640)
  134. a = H(a, b, c, d, blks[i+13], 4, 681279174)
  135. d = H(d, a, b, c, blks[i+ 0], 11, -358537222)
  136. c = H(c, d, a, b, blks[i+ 3], 16, -722521979)
  137. b = H(b, c, d, a, blks[i+ 6], 23, 76029189)
  138. a = H(a, b, c, d, blks[i+ 9], 4, -640364487)
  139. d = H(d, a, b, c, blks[i+12], 11, -421815835)
  140. c = H(c, d, a, b, blks[i+15], 16, 530742520)
  141. b = H(b, c, d, a, blks[i+ 2], 23, -995338651)
  142.  
  143. a = I(a, b, c, d, blks[i+ 0], 6, -198630844)
  144. d = I(d, a, b, c, blks[i+ 7], 10, 1126891415)
  145. c = I(c, d, a, b, blks[i+14], 15, -1416354905)
  146. b = I(b, c, d, a, blks[i+ 5], 21, -57434055)
  147. a = I(a, b, c, d, blks[i+12], 6, 1700485571)
  148. d = I(d, a, b, c, blks[i+ 3], 10, -1894986606)
  149. c = I(c, d, a, b, blks[i+10], 15, -1051523)
  150. b = I(b, c, d, a, blks[i+ 1], 21, -2054922799)
  151. a = I(a, b, c, d, blks[i+ 8], 6, 1873313359)
  152. d = I(d, a, b, c, blks[i+15], 10, -30611744)
  153. c = I(c, d, a, b, blks[i+ 6], 15, -1560198380)
  154. b = I(b, c, d, a, blks[i+13], 21, 1309151649)
  155. a = I(a, b, c, d, blks[i+ 4], 6, -145523070)
  156. d = I(d, a, b, c, blks[i+11], 10, -1120210379)
  157. c = I(c, d, a, b, blks[i+ 2], 15, 718787259)
  158. b = I(b, c, d, a, blks[i+ 9], 21, -343485551)
  159.  
  160. a = add(a, olda)
  161. b = add(b, oldb)
  162. c = add(c, oldc)
  163. d = add(d, oldd)
  164. end
  165. return getHex(a)..getHex(b)..getHex(c)..getHex(d), a,b,c,d
  166. end
  167. ----------------- End md5 ------------------
  168.  
  169. local Base=2^32
  170. --[[ функции преобразования подблока по ключу
  171. subblock - преобразуемый подблок
  172. key - ключ
  173. возвращаяемое значение - преобразованный блок]]
  174. local function f(subblock, key)
  175. local _,res=crypt.md5(subblock..key)
  176. return res
  177. -- return math.fmod(subblock + key, Base)
  178. end
  179.  
  180. --[[Шифрование открытого текста
  181. left - левый входной подблок
  182. right - правый входной подблок
  183. key - массив ключей]]
  184. local function C(left, right, key)
  185. for i = 1,#key do
  186. left,right = bit.bxor(right, f(left, key[i])), left
  187. end
  188. return left,right
  189. end
  190. --[[Расшифрование текста
  191. left - левый зашифрованный подблок
  192. right - правый зашифрованный подблок]]
  193. local function D(left, right, key)
  194. for i = #key,1,-1 do
  195. left,right = right, bit.bxor(left, f(right, key[i]))
  196. end
  197. return left,right
  198. end
  199. --Функция формирования массива ключей
  200. function crypt.getkey(pwd)
  201. local key={}
  202. local hesh=pwd
  203. for i=0,3 do
  204. hesh,key[i*4+1],key[i*4+2],key[i*4+3],key[i*4+4]=crypt.md5(hesh)
  205. end
  206. return key
  207. end
  208.  
  209. local function StrToInt(str)
  210. local int = 0
  211. local byte
  212. for i = 0, 3 do
  213. byte=str:sub(1,1)
  214. str=str:sub(2)
  215. int=bit.lshift(int,8)+(string.byte(byte) or 0)
  216. end
  217. return int, str
  218. end
  219.  
  220. local function IntToHex(int)
  221. local str = ""
  222. local char
  223. for i = 0, 7 do
  224. char=bit.band(bit.rshift(int, 28), 15)
  225. int=bit.lshift(int,4)
  226. str=str..string.format('%x',char)
  227. end
  228. return str
  229. end
  230.  
  231. local function HexToInt(str)
  232. local int = 0
  233. local byte
  234. for i = 0, 3 do
  235. byte=tonumber(str:sub(1,2),16) or 0
  236. str=str:sub(3)
  237. int=bit.lshift(int,8)+byte
  238. end
  239. return int, str
  240. end
  241.  
  242. local function IntToStr(int)
  243. local str = ""
  244. local char
  245. for i = 0, 3 do
  246. char=bit.band(bit.rshift(int, 24), 255)
  247. int=bit.lshift(int,8)
  248. str=str..string.char(char)
  249. end
  250. return str
  251. end
  252.  
  253. --[[Шифрование открытого текста
  254. str - входной текст
  255. key - массив ключей]]
  256. function crypt.crypt(str,key)
  257. local str1=""
  258. local left,right
  259. while #str>0 do
  260. left,str=StrToInt(str)
  261. right,str=StrToInt(str)
  262. left,right=C(left, right, key)
  263. str1=str1..IntToHex(left)
  264. str1=str1..IntToHex(right)
  265. end
  266. return str1
  267. end
  268.  
  269. --[[Расшифрование текста
  270. str - зашифрованный текст
  271. key - массив ключей]]
  272. function crypt.decrypt(str,key)
  273. local str1=""
  274. local left,right
  275. while #str>0 do
  276. left,str=HexToInt(str)
  277. right,str=HexToInt(str)
  278. left,right=D(left, right, key)
  279. str1=str1..IntToStr(left)
  280. str1=str1..IntToStr(right)
  281. end
  282. return str1
  283. end
  284.  
  285. return crypt
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement