Advertisement
Alex1979

LCR

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