Advertisement
ZNZNCOOP

Feistel

Jul 16th, 2014
317
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.73 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.   local _,res=md5(subblock..key)
  175.   return res
  176. --  return math.fmod(subblock + key, Base)
  177. end
  178.  
  179. --[[Шифрование открытого текста
  180. left - левый входной подблок
  181. right - правый входной подблок
  182. key - массив ключей]]
  183. local function C(left, right, key)
  184.     for i = 1,#key do
  185.         left,right = bit.bxor(right, f(left, key[i])), left
  186.     end
  187.     return left,right
  188. end
  189. --[[Расшифрование текста
  190. left - левый зашифрованный подблок
  191. right - правый зашифрованный подблок]]
  192. local function D(left, right, key)
  193.     for i = #key,1,-1 do
  194.         left,right = right, bit.bxor(left, f(right, key[i]))
  195.     end
  196.     return left,right
  197. end
  198. --Функция формирования массива ключей
  199. function getkey(pwd)
  200.   local key={}
  201.   local hesh=pwd
  202.   for i=0,3 do
  203.     hesh,key[i*4+1],key[i*4+2],key[i*4+3],key[i*4+4]=md5(hesh)
  204.   end
  205.   return key
  206. end
  207.  
  208. local function StrToInt(str)
  209.    local int = 0
  210.    local byte
  211.    for i = 0, 3 do
  212.      byte=str:sub(1,1)
  213.      str=str:sub(2)
  214.      int=bit.blshift(int,8)+(string.byte(byte) or 0)
  215.    end
  216.    return int, str
  217. end
  218.  
  219. local function IntToHex(int)
  220.    local str = ""
  221.    local char
  222.    for i = 0, 7 do
  223.       char=bit.band(bit.brshift(int, 28), 15)
  224.       int=bit.blshift(int,4)
  225.       str=str..string.format('%x',char)
  226.    end
  227.    return str
  228. end
  229.  
  230.  local function HexToInt(str)
  231.    local int = 0
  232.    local byte
  233.    for i = 0, 3 do
  234.      byte=tonumber(str:sub(1,2),16) or 0
  235.      str=str:sub(3)
  236.      int=bit.blshift(int,8)+byte
  237.    end
  238.    return int, str
  239. end
  240.  
  241.  local function IntToStr(int)
  242.    local str = ""
  243.    local char
  244.    for i = 0, 3 do
  245.       char=bit.band(bit.brshift(int, 24), 255)
  246.       int=bit.blshift(int,8)
  247.       str=str..string.char(char)
  248.    end
  249.    return str
  250. end
  251.  
  252. --[[Шифрование открытого текста
  253. str - входной текст
  254. key - массив ключей]]
  255. function crypt(str,key)
  256.   local str1=""
  257.   local left,right
  258.   while #str>0 do
  259.     left,str=StrToInt(str)
  260.     right,str=StrToInt(str)
  261.     left,right=C(left, right, key)
  262.     str1=str1..IntToHex(left)
  263.     str1=str1..IntToHex(right)
  264.   end
  265.   return str1
  266. end
  267.  
  268. --[[Расшифрование текста
  269. str - зашифрованный текст
  270. key - массив ключей]]
  271. function decrypt(str,key)
  272.   local str1=""
  273.   local left,right
  274.   while #str>0 do
  275.     left,str=HexToInt(str)
  276.     right,str=HexToInt(str)
  277.     left,right=D(left, right, key)
  278.     str1=str1..IntToStr(left)
  279.     str1=str1..IntToStr(right)
  280.   end
  281.   return str1
  282. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement