Guest User

Untitled

a guest
Oct 19th, 2018
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 19.82 KB | None | 0 0
  1. --[[
  2.  
  3. the Advanced Roblox CryptoSystem, or ARCS, is being designed to bring better security to roblox games, such as private servers.
  4. While still in its infancy, hopefully one day, ARCS will become a suitable means of data encryption.
  5.  
  6. ARCS currently supplies 2 features.
  7.  
  8. A secure Hash function,
  9. And secure encryption algorithm.
  10.  
  11.  
  12. The secure Hash algorithm (A SHA256 variant), can be called with the Hash() function.
  13. It has 1 input, which is the text to be hashed.
  14.  
  15. print(ARCS.Hash("Some Random Data"))
  16. -->28551017D06B84BD97742D373E60D4CF6D1D863E3817A7CB7058B824204194A6
  17.  
  18.  
  19. ARCS also includes the ARCS Cipher, is a (hopefully) secure encryption algorithm.
  20. The cipher has 3 inputs, the text to be encrypted, the key to use, and the number of rounds (optional)
  21. You can use ARCS.CipherEN to encrypt, and ARCS.CipherDE to decrypt
  22.  
  23. print(ARCS.CipherEN("HOLY SHNAPPLE","WHAT 9000!?!?!?"))
  24. -->36FF255D11705BDF5939208F7C25BE54B20992B45265ACE387551AE4798D5D5F
  25.  
  26. print(ARCS.CipherDE("36FF255D11705BDF5E12208C7C25BE5493BE8F2C200F55FC26CD2C3EE4D49D6E","WHAT 9000!?!?!?"))
  27. -->HOLY SHNAPPLE 08/16/10 13:53:42 0016
  28.  
  29. (The number of rounds is -not- required when decrypting, even if the string has been encrypted with more than 16)
  30.  
  31. the decryption function will return two strings, the decrypted text, and the decrypted header.
  32. The header is a 16 character string of text included at the begining of the cipher text.
  33. The header includes the time and date of when the encryption was started for that string, and
  34. the number of rounds it was encrypted with.
  35.  
  36. Currently, the ARCS cipher is a variable strength cipher, in the sense, that you can use more rounds in the encryption.
  37. The main portion of the encryption consits of a function that systematically encrypts the text.
  38. When you encrypt something, its encrypted in stages, each stage is a round. Each round is just the main portion of the cipher,
  39. but it is repeated over and over again to offer more security. There is a mininum of 16 rounds, and a maximum of 9999.
  40.  
  41. It should also be noted that the header string is always encrypted with 16 rounds, which is strong enough to protect the inocous data
  42. held within it.
  43.  
  44. The ARCS Cipher currently is not exactly how I would like it to be, so there will most likely be several updates in the comming days,
  45. to improve security, and efficiency.
  46.  
  47. Please check back every once in a while to ensure that you are using the most up-to-date version of ARCS.
  48.  
  49. ]]--
  50.  
  51. ARCS = {}
  52.  
  53. function XOR(in1, in2)
  54. final = ""
  55. for i=1,#in1 do
  56. x1 = in1:sub(i,i)
  57. x2 = in2:sub(i,i)
  58. if x1 == x2 then
  59. final = final .. "0"
  60. else
  61. final = final .. "1"
  62. end
  63. end
  64. return final
  65. end
  66.  
  67. function AND(in1, in2)
  68. final = ""
  69. for u=1,#in1/8 do
  70. x1 = in1:sub(u*8-7,u*8)
  71. x2 = in2:sub(u*8-7,u*8)
  72. local out = ""
  73. for i=1,8 do
  74. i1 = x1:sub(i,i)
  75. i2 = x1:sub(i,i)
  76. if i1 == i2 then
  77. out = out .. "1"
  78. elseif i1 ~= i2 then
  79. out = out .. "0"
  80. end
  81. end
  82. final = final .. out
  83. end
  84. return final
  85. end
  86.  
  87. function NOT(in1)
  88. final = ""
  89. for u=1,#in1/8 do
  90. x1 = in1:sub(u*8-7,u*8)
  91. local out = ""
  92. for i=1,8 do
  93. i1 = x1:sub(i,i)
  94. i2 = x1:sub(i,i)
  95. if i1 == "1" then
  96. out = out .. "0"
  97. else
  98. out = out .. "1"
  99. end
  100. end
  101. final = final .. out
  102. end
  103. return final
  104. end
  105.  
  106. function OR(in1,in2)
  107. local out = ""
  108. for i=1,8 do
  109. i1 = in1:sub(i,i)
  110. i2 = in2:sub(i,i)
  111. if i1 == "1" or i2 == "1" then
  112. out = out .. "1"
  113. else
  114. out = out .. "0"
  115. end
  116. end
  117. return out
  118. end
  119.  
  120. function LeftShift(x,n)
  121. y=#x
  122. x = x:sub(n+1,32)
  123. x = x .. string.rep("0",32-#x)
  124. return x
  125. end
  126.  
  127. function RightShift(x,n)
  128. y=#x
  129. x = x:sub(1,n)
  130. x = string.rep("0",32-n) .. x
  131. return x
  132. end
  133.  
  134. function RightRotate(x,n)
  135. return x:sub(n+1,#x) .. x:sub(1,n)
  136. end
  137.  
  138.  
  139. function moduloAdd(txt1,txt2,x)
  140. if not x then x = 32 end
  141. txt1 = bToDec(txt1)
  142. txt2 = bToDec(txt2)
  143. txt = (txt1 + txt2) % 2^x
  144. final = nToBin(txt,x)
  145. return final
  146. end
  147.  
  148. function columTran(txt)
  149. local tmp = {}
  150. for i=1,16 do
  151. tmp[i] = txt:sub(i*8-7,i*8)
  152. end
  153. local final = ""
  154. local x,y = -3,1
  155. repeat
  156. x = (x + 4)
  157. if x > 16 then
  158. y = y + 1
  159. x = y
  160. end
  161. final = final .. tmp[x]
  162. until x == 16
  163. return final
  164. end
  165.  
  166. function shiftRow(txt,way)
  167. if not way then
  168. local tmp = {}
  169. for i=1,4 do
  170. tmp[i] = txt:sub(i*32-31,i*32)
  171. end
  172. tmp[2] = RightRotate(tmp[2],8)
  173. tmp[3] = RightRotate(tmp[3],16)
  174. tmp[4] = RightRotate(tmp[4],24)
  175. return table.concat(tmp)
  176. elseif way then
  177. local tmp = {}
  178. for i=1,4 do
  179. tmp[i] = txt:sub(i*32-31,i*32)
  180. end
  181. tmp[2] = RightRotate(tmp[2],24)
  182. tmp[3] = RightRotate(tmp[3],16)
  183. tmp[4] = RightRotate(tmp[4],8)
  184. return table.concat(tmp)
  185. end
  186. end
  187.  
  188. function cToHex(text)
  189. final = ''
  190. for i=1,#text do
  191. work = string.format("%X",text:sub(i,i):byte())
  192. if #work == 1 then
  193. work = "0"..work
  194. end
  195. final = final .. work
  196. end
  197. return final
  198. end
  199.  
  200. function hToBin(char)
  201. local final = ""
  202. for i=1,#char/2 do
  203. local fix = 0
  204. fix = tonumber(char:sub(i*2-1,i*2),16)
  205. final = final .. nToBin(fix)
  206. end
  207. return final
  208. end
  209.  
  210. function nToBin(Decimal,x)
  211. if x == nil then x = 8 end
  212. local BinaryRep = ""
  213. local Number = Decimal
  214. while Number > 0 do
  215. BinaryRep = BinaryRep .. Number % 2
  216. Number = math.floor(Number / 2)
  217. end
  218. return Decimal == 1 and ("0"):rep(x-1) .. "1" or ("0"):rep(x - BinaryRep:len()) .. BinaryRep:reverse()
  219. end
  220.  
  221. function nToHex(num)
  222. return string.format("%X",num)
  223. end
  224.  
  225. function bToHex(txt)
  226. rfinal = ""
  227. for u=1,#txt/8 do
  228. bin = tostring(txt:sub(u*8-7,u*8))
  229. local final = ""
  230. for i=1,8,4 do
  231. local fix = bin:sub(i,i+3)
  232. local subtotal = 0
  233. local o = 0
  234. for u=4,1,-1 do
  235. local fx = fix:sub(u,u)
  236. if fx == "1" then
  237. subtotal = subtotal + 2^(o)
  238. end
  239. o = o +1
  240. end
  241. if subtotal > 9 then
  242. subtotal = string.char(subtotal+55)
  243. end
  244. final = final .. subtotal
  245. end
  246. rfinal = rfinal .. final
  247. end
  248. return rfinal
  249. end
  250.  
  251. function hToDec(text)
  252. return tonumber(text,16)
  253. end
  254.  
  255. function bToDec(text)
  256. return tonumber(text,2)
  257. end
  258.  
  259. function hToChar(text)
  260. final = ""
  261. for i=1,#text/2 do
  262. work = text:sub(i*2-1,i*2)
  263. work = hToDec(work)
  264. work = string.char(work)
  265. final = final .. work
  266. end
  267. return final
  268. end
  269.  
  270. function bToChar(text)
  271. final = ""
  272. for i=1,#text/8 do
  273. work = text:sub(i*8-7,i*8)
  274. final = final .. string.char(tonumber(work,2))
  275. end
  276. return final
  277. end
  278.  
  279. function cToBin(text)
  280. final = ""
  281. for i=1,#text do
  282. final = final .. nToBin(text:sub(i,i):byte())
  283. end
  284. return final
  285. end
  286.  
  287. function sMod(num,base)
  288. if num == base then
  289. return 1
  290. else
  291. return num % base
  292. end
  293. end
  294.  
  295.  
  296. function XinY(num,base)
  297. local x = 0
  298. while num >= base do
  299. num = num - base
  300. x=x+1
  301. end
  302. return x
  303. end
  304.  
  305. function nextInt(num,base)
  306. y = XinY(num,base) + 1
  307. return base*y - num
  308. end
  309.  
  310.  
  311. Blocks={}
  312. HVal = {}
  313. Blocks[0] = {}
  314. Constants = {}
  315. Constants[0] = "01000010100010100010111110011000"
  316. Constants[1] = "01110001001101110100010010010001"
  317. Constants[2] = "10110101110000001111101111001111"
  318. Constants[3] = "11101001101101011101101110100101"
  319. Constants[4] = "00111001010101101100001001011011"
  320. Constants[5] = "01011001111100010001000111110001"
  321. Constants[6] = "10010010001111111000001010100100"
  322. Constants[7] = "10101011000111000101111011010101"
  323. Constants[8] = "11011000000001111010101010011000"
  324. Constants[9] = "00010010100000110101101100000001"
  325. Constants[10] = "00100100001100011000010110111110"
  326. Constants[11] = "01010101000011000111110111000011"
  327. Constants[12] = "01110010101111100101110101110100"
  328. Constants[13] = "10000000110111101011000111111110"
  329. Constants[14] = "10011011110111000000011010100111"
  330. Constants[15] = "11000001100110111111000101110100"
  331. Constants[16] = "11100100100110110110100111000001"
  332. Constants[17] = "11101111101111100100011110000110"
  333. Constants[18] = "00001111110000011001110111000110"
  334. Constants[19] = "00100100000011001010000111001100"
  335. Constants[20] = "00101101111010010010110001101111"
  336. Constants[21] = "01001010011101001000010010101010"
  337. Constants[22] = "01011100101100001010100111011100"
  338. Constants[23] = "01110110111110011000100011011010"
  339. Constants[24] = "10011000001111100101000101010010"
  340. Constants[25] = "10101000001100011100011001101101"
  341. Constants[26] = "10110000000000110010011111001000"
  342. Constants[27] = "10111111010110010111111111000111"
  343. Constants[28] = "11000110111000000000101111110011"
  344. Constants[29] = "11010101101001111001000101000111"
  345. Constants[30] = "00000110110010100110001101010001"
  346. Constants[31] = "00010100001010010010100101100111"
  347. Constants[32] = "00100111101101110000101010000101"
  348. Constants[33] = "00101110000110110010000100111000"
  349. Constants[34] = "01001101001011000110110111111100"
  350. Constants[35] = "01010011001110000000110100010011"
  351. Constants[36] = "01100101000010100111001101010100"
  352. Constants[37] = "01110110011010100000101010111011"
  353. Constants[38] = "10000001110000101100100100101110"
  354. Constants[39] = "10010010011100100010110010000101"
  355. Constants[40] = "10100010101111111110100010100001"
  356. Constants[41] = "10101000000110100110011001001011"
  357. Constants[42] = "11000010010010111000101101110000"
  358. Constants[43] = "11000111011011000101000110100011"
  359. Constants[44] = "11010001100100101110100000011001"
  360. Constants[45] = "11010110100110010000011000100100"
  361. Constants[46] = "11110100000011100011010110000101"
  362. Constants[47] = "00010000011010101010000001110000"
  363. Constants[48] = "00011001101001001100000100010110"
  364. Constants[49] = "00011110001101110110110000001000"
  365. Constants[50] = "00100111010010000111011101001100"
  366. Constants[51] = "00110100101100001011110010110101"
  367. Constants[52] = "00111001000111000000110010110011"
  368. Constants[53] = "01001110110110001010101001001010"
  369. Constants[54] = "01011011100111001100101001001111"
  370. Constants[55] = "01101000001011100110111111110011"
  371. Constants[56] = "01110100100011111000001011101110"
  372. Constants[57] = "01111000101001010110001101101111"
  373. Constants[58] = "10000100110010000111100000010100"
  374. Constants[59] = "10001100110001110000001000001000"
  375. Constants[60] = "10010000101111101111111111111010"
  376. Constants[61] = "10100100010100000110110011101011"
  377. Constants[62] = "10111110111110011010001111110111"
  378. Constants[63] = "11000110011100010111100011110010"
  379.  
  380. function miniReset()
  381. for i=0,7 do
  382. HVal[i] = Blocks[0][i]
  383. end
  384. end
  385.  
  386. function largeReset()
  387. Blocks[0][0] = "01101010000010011110011001100111"
  388. Blocks[0][1] = "10111011011001111010111010000101"
  389. Blocks[0][2] = "00111100011011101111001101110010"
  390. Blocks[0][3] = "10100101010011111111010100111010"
  391. Blocks[0][4] = "01010001000011100101001001111111"
  392. Blocks[0][5] = "10011011000001010110100010001100"
  393. Blocks[0][6] = "00011111100000111101100110101011"
  394. Blocks[0][7] = "01011011111000001100110100011001"
  395. end
  396.  
  397. largeReset()
  398. for i=0,63 do Constants[i] = hToBin(Constants[i]) end
  399.  
  400. function ARCS.Hash(text)
  401. text = tostring(text)
  402. --String preprocessing
  403. local tmp = cToBin(text)
  404. tml = #tmp
  405. local nextI = nextInt(tml+65,512)
  406. tmp = tmp .. "1" .. string.rep("0",nextI)
  407. local append = nToBin(#text)
  408. append = string.rep("0",64-#append) .. append
  409. text = tmp .. append
  410. local nB = XinY(#text,512)
  411. largeReset()
  412. for i=1,nB do
  413. Blocks[i] = {}
  414. Blocks[i].String=text:sub(i*512-511,i*512)
  415. end
  416. --Block preprocessing
  417. for i=1,nB do
  418. work = Blocks[i].String
  419. for u=0,15 do
  420. Blocks[i][u] = Blocks[i].String:sub((u+1)*32-31,(u+1)*32)
  421. end
  422. for u=16,63 do
  423. s0 = XOR(RightRotate(Blocks[i][u-15],7),RightRotate(Blocks[i][u-15],18))
  424. s0 = XOR(s0,RightRotate(Blocks[i][u-15],3))
  425. s1 = XOR(RightRotate(Blocks[i][u-2],17),RightRotate(Blocks[i][u-2],19))
  426. s1 = XOR(s1,RightRotate(Blocks[i][u-2],10))
  427. step1 = moduloAdd(Blocks[i][u-16],s0)
  428. step1 = moduloAdd(step1,Blocks[i][u-7])
  429. Blocks[i][u] = moduloAdd(step1,s1)
  430. end
  431. end
  432. --Getting to the actual hash now
  433. for i=1,nB do
  434. miniReset()
  435. for o=0,63 do
  436. local s0 = XOR(RightRotate(HVal[0],2),RightRotate(HVal[0],13))
  437. s0 = XOR(s0,RightRotate(HVal[0],22))
  438. local maj = XOR(AND(HVal[0],HVal[1]),AND(HVal[0],HVal[2]))
  439. maj = XOR(maj,AND(HVal[1],HVal[2]))
  440. local t2 = moduloAdd(s0,maj)
  441. local s1 = XOR(RightRotate(HVal[4],6),RightRotate(HVal[4],11))
  442. s1 = XOR(s1,RightRotate(HVal[4],25))
  443. local ch = XOR(AND(HVal[4],HVal[5]),AND(NOT(HVal[4]),HVal[6]))
  444. local t1 = moduloAdd(HVal[7],s1)
  445. t1 = moduloAdd(t1,ch)
  446. t1 = moduloAdd(t1,Constants[o])
  447. t1 = moduloAdd(t1,Blocks[i][o])
  448. HVal[7] = HVal[6]
  449. HVal[6] = HVal[5]
  450. HVal[5] = HVal[4]
  451. HVal[4] = moduloAdd(HVal[3],t1)
  452. HVal[3] = HVal[2]
  453. HVal[2] = HVal[1]
  454. HVal[1] = HVal[0]
  455. HVal[0] = moduloAdd(t1,t2)
  456. end
  457. for o=0,7 do
  458. Blocks[i][o] = moduloAdd(Blocks[i-1][o],HVal[o])
  459. end
  460. end
  461. FINAL = ""
  462. for i=0,7 do
  463. FINAL = FINAL .. bToHex(Blocks[nB][i])
  464. end
  465. return FINAL
  466. end
  467.  
  468. HEX = {"0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F","0000","0001","0010","0011","0100","0101","0110","0111","1000","1001","1010","1011","1100","1101","1110","1111"}
  469. SHIFT = {23, 27, 33, 41, 51, 63, 77, 93, 111, 131, 153, 177, 203, 231, 261, 293, 327, 363, 401, 441, 483, 527, 573, 621, 671, 723, 777, 833, 891, 951, 1013, 1077, 1143, 1211, 1281, 1353, 1427, 1503, 1581, 1661, 1743, 1827, 1913, 2001, 2091, 2183, 2277, 2373, 2471, 2571, 2673, 2777, 2883, 2991, 3101, 3213, 3327, 3443, 3561, 3681, 3803, 3927, 4053, 4181, 4311, 4443, 4577, 4713, 4851, 4991, 5133, 5277, 5423, 5571, 5721, 5873, 6027, 6183, 6341, 6501, 6663, 6827, 6993, 7161, 7331, 7503, 7677, 7853, 8031, 8211, 8393, 8577, 8763, 8951, 9141, 9333, 9527, 9723, 9921, 10121, 10323, 10527, 10733, 10941, 11151, 11363, 11577, 11793, 12011, 12231, 12453, 12677, 12903, 13131, 13361, 13593, 13827, 14063, 14301, 14541, 14783, 15027, 15273, 15521, 15771, 16023, 16277, 16533, 16791, 17051, 17313, 17577, 17843, 18111, 18381, 18653, 18927, 19203, 19481, 19761, 20043, 20327, 20613, 20901, 21191, 21483, 21777, 22073, 22371, 22671, 22973, 23277, 23583, 23891, 24201, 24513, 24827, 25143, 25461, 25781, 26103, 26427, 26753, 27081, 27411, 27743, 28077, 28413, 28751, 29091, 29433, 29777, 30123, 30471, 30821, 31173, 31527, 31883, 32241, 32601, 32963, 33327, 33693, 34061, 34431, 34803, 35177, 35553, 35931, 36311, 36693, 37077, 37463, 37851, 38241, 38633, 39027, 39423, 39821, 40221, 40623, 41027, 41433, 41841, 42251, 42663, 43077, 43493, 43911, 44331, 44753, 45177, 45603, 46031, 46461, 46893, 47327, 47763, 48201, 48641, 49083, 49527, 49973, 50421, 50871, 51323, 51777, 52233, 52691, 53151, 53613, 54077, 54543, 55011, 55481, 55953, 56427, 56903, 57381, 57861, 58343, 58827, 59313, 59801, 60291, 60783, 61277, 61773, 62271, 62771, 63273, 63777, 64283, 64791, 65301, 65813}
  470. function genSBox(seed)
  471. local Key = seed
  472. local SBox = {}
  473. for i=2,16 do
  474. work = seed:sub(i*16-15,i*16)
  475. fin = XOR(work:sub(1,8),work:sub(9,16))
  476. end
  477. local nx,ny = bToDec(fin:sub(5,8)),bToDec(fin:sub(1,4))
  478. of = SHIFT[bToDec(fin)%256+1]
  479. local tmp = {}
  480. local IBox= {}
  481. for y=0,15 do
  482. tmp[y] = {}
  483. SBox[y] = {}
  484. for x=0,15 do
  485. SBox[y][x] = nToBin((y*of+ny)%16,4)..nToBin((x*of+nx)%16,4)
  486. tmp[y][x] = nToBin((y*of+ny)%16,4)..nToBin((x*of+nx)%16,4)
  487. end
  488. end
  489. for i=0,15 do
  490. for x=0,15 do
  491. tmp[x][i] = SBox[(x*(of*2+1)+1+i)%16][i]
  492. end
  493. end
  494. local SBox = {}
  495. local IBox = {}
  496. for i=0,15 do
  497. SBox[HEX[17+i]] = {}
  498. for x=0,15 do
  499. p1,p2,p3 = HEX[17+i],HEX[17+x],tmp[i][(x*of+1+i)%16]
  500. SBox[p1][p2] = p3
  501. if IBox[p3:sub(1,4)] == nil then
  502. IBox[p3:sub(1,4)] = {}
  503. end
  504. IBox[p3:sub(1,4)][p3:sub(5,8)] = p1 .. p2
  505. end
  506. end
  507. tmp = nil
  508. return SBox,IBox
  509. end
  510.  
  511. function SubByte(txt,tab)
  512. final = ""
  513. for i=1,#txt/8 do
  514. p1,p2 = txt:sub(i*8-7,i*8-4),txt:sub(i*8-3,i*8)
  515. final = final .. tab[p1][p2]
  516. end
  517. return final
  518. end
  519.  
  520.  
  521. function ARCS.CipherEN(txt,key,rounds)
  522. Key = hToBin(ARCS.Hash(key)) -- Creates a stronger key
  523. if not rounds or rounds < 16 then --makes sure you use proper round values
  524. rounds = 16
  525. end
  526. if rounds > 9999 then
  527. rounds = rounds % 9999 + 1
  528. end
  529. IV = os.date() -- Sets up the initial CBC (cipher block chaining) vector
  530. IV = string.gsub(string.gsub(IV:sub(1,8) .. IV:sub(10,17),"/",""),":","")
  531. IV = IV .. string.rep("0",16-(#IV+#tostring(rounds))) .. rounds
  532. ttxt = txt .. string.char(3) ..string.rep(string.char(1),nextInt(#txt+1,16))
  533. PTxt = cToBin(IV .. ttxt) -- translates everything into binary for INCREASED speed
  534. PTxt = XOR(PTxt:sub(1,128),Key:sub(1,128)) .. PTxt:sub(129,#PTxt)
  535. nB = XinY(#PTxt,128)
  536. local Blocks = {}
  537. Blocks[1] = PTxt:sub(1,128)
  538. for u=2,nB do -- breaks message into 128 bit (16 character) blocks
  539. Blocks[u] = XOR(PTxt:sub(u*128-127,u*128),Blocks[u-1])
  540. end
  541. TEXT = hToBin(ARCS.Hash(bToChar(Key)))
  542. SBox = genSBox(Key) -- generating the key dependant S-Box
  543. KBox = genSBox(TEXT) -- generating the secondary key dependant S-Box (used for key expansion)
  544. KEYS = {}
  545. KEYS[0] = Key
  546. for i=1,rounds+1 do
  547. tmp = XOR(KEYS[i-1],XOR(SubByte(KEYS[i-1],SBox),SubByte(KEYS[i-1],KBox))) -- Turns 1, 256bit key into the required number of 256 bit keys
  548. KEYS[i] = RightRotate(tmp,(i%64)+1)
  549. end
  550. TMP = Blocks[1]
  551. for u=1,2 do
  552. TMP = XOR(TMP,KEYS[0]:sub(u*128-127,u*128))
  553. end
  554. for u=1,2 do -- encrypts the header with 16 rounds, always, ALWAYS
  555. for i=1,16 do
  556. TMP = SubByte(TMP,SBox)
  557. TMP = shiftRow(TMP)
  558. TMP = XOR(TMP,KEYS[i]:sub(u*128-127,u*128))
  559. end
  560. end
  561. TMP = SubByte(TMP,SBox)
  562. TMP = shiftRow(TMP)
  563. for u=1,2 do
  564. TMP = XOR(TMP,KEYS[17]:sub(u*128-127,u*128))
  565. end
  566. Blocks[1] = TMP
  567. for Z=2,nB do
  568. TMP = Blocks[Z]
  569. for u=1,2 do
  570. TMP = XOR(TMP,KEYS[0]:sub(u*128-127,u*128))
  571. end
  572. for u=1,2 do
  573. for i=1,rounds do
  574. TMP = SubByte(TMP,SBox)
  575. TMP = shiftRow(TMP)
  576. TMP = XOR(TMP,KEYS[i]:sub(u*128-127,u*128))
  577. TMP = RightRotate(TMP,i%64+1)
  578. end
  579. end
  580. TMP = SubByte(TMP,SBox)
  581. TMP = shiftRow(TMP)
  582. for u=1,2 do
  583. TMP = XOR(TMP,KEYS[rounds+1]:sub(u*128-127,u*128))
  584. end
  585. TMP = RightRotate(TMP,(rounds+1)%64+1)
  586. Blocks[Z] = TMP
  587. end
  588. FINAL = ""
  589. for i=1,nB do
  590. FINAL = FINAL .. bToHex(Blocks[i])
  591. end
  592. return FINAL
  593. end
  594.  
  595. function ARCS.CipherDE(txt,key,rounds)
  596. if not rounds or rounds < 16 then
  597. rounds = 16
  598. end
  599. if rounds > 9999 then
  600. rounds = rounds % 9999 + 1
  601. end
  602. Key = hToBin(ARCS.Hash(key))
  603. PTxt = hToBin(txt)
  604. nB = XinY(#PTxt,128)
  605. local Blocks = {}
  606. for u=1,nB do -- breaks message into 128 bit (16 character) blocks
  607. Blocks[u] = PTxt:sub(u*128-127,u*128)
  608. end
  609. TEXT = hToBin(ARCS.Hash(bToChar(Key)))
  610. SBox,IBox = genSBox(Key) -- generating the key dependant S-Box
  611. KBox = genSBox(TEXT) -- generating the secondary key dependant S-Box (used for key expansion)
  612. KEYS = {}
  613. KEYS[0] = Key
  614. for i=1,rounds+1 do
  615. tmp = XOR(KEYS[i-1],XOR(SubByte(KEYS[i-1],SBox),SubByte(KEYS[i-1],KBox))) -- Turns 1, 256bit key into 98 256 bit keys
  616. KEYS[i] = RightRotate(tmp,(i%64)+1)
  617. end
  618. TMP = Blocks[1]
  619. for u=2,1,-1 do
  620. TMP = XOR(TMP,KEYS[17]:sub(u*128-127,u*128))
  621. end
  622. TMP = shiftRow(TMP,true)
  623. TMP = SubByte(TMP,IBox)
  624. for u=2,1,-1 do -- encrypts the header with 16 rounds, always, ALWAYS
  625. for i=16,1,-1 do
  626. TMP = XOR(TMP,KEYS[i]:sub(u*128-127,u*128))
  627. TMP = shiftRow(TMP,true)
  628. TMP = SubByte(TMP,IBox)
  629. end
  630. end
  631. for u=1,2 do
  632. TMP = XOR(TMP,KEYS[0]:sub(u*128-127,u*128))
  633. end
  634. FINAL = {}
  635. for i=1,nB do
  636. FINAL[i] = {}
  637. end
  638. FINAL[1] = TMP
  639. HEADER = bToChar(XOR(TMP,Key:sub(1,128)))
  640. nRnds = tonumber(HEADER:sub(#HEADER-3,#HEADER))
  641. if nRnds then
  642. if nRnds > rounds then
  643. for i=rounds,nRnds+1 do
  644. tmp = XOR(KEYS[i-1],XOR(SubByte(KEYS[i-1],SBox),SubByte(KEYS[i-1],KBox))) -- Turns 1, 256bit key into 98 256 bit keys
  645. KEYS[i] = RightRotate(tmp,(i%64)+1)
  646. end
  647. end
  648. rounds = nRnds
  649. for Z=2,nB do
  650. TMP = Blocks[Z]
  651. TMP = RightRotate(TMP,128-((rounds+1)%64+1))
  652. for u=2,1,-1 do
  653. TMP = XOR(TMP,KEYS[rounds+1]:sub(u*128-127,u*128))
  654. end
  655. TMP = shiftRow(TMP,true)
  656. TMP = SubByte(TMP,IBox)
  657. for u=2,1,-1 do
  658. for i=rounds,1,-1 do
  659. TMP = RightRotate(TMP,128-(i%64+1))
  660. TMP = XOR(TMP,KEYS[i]:sub(u*128-127,u*128))
  661. TMP = shiftRow(TMP,true)
  662. TMP = SubByte(TMP,IBox)
  663. end
  664. end
  665. for u=2,1,-1 do
  666. TMP = XOR(TMP,KEYS[0]:sub(u*128-127,u*128))
  667. end
  668. FINAL[Z] = TMP
  669. end
  670. FINALans = ""
  671. for u=2,nB do -- breaks message into 128 bit (16 character) blocks
  672. work = bToChar(XOR(FINAL[u],FINAL[u-1]))
  673. found = work:find(string.char(3))
  674. if found then
  675. work = work:sub(1,found-1)
  676. end
  677. FINALans = FINALans .. work
  678. end
  679. HEADER = HEADER:sub(1,2) .. "/" .. HEADER:sub(3,4) .. "/" .. HEADER:sub(5,6) .. " ".. HEADER:sub(7,8) .. ":" .. HEADER:sub(9,10) .. ":" .. HEADER:sub(11,12) .. " " .. HEADER:sub(13,16)
  680. return FINALans,HEADER
  681. else
  682. return "Invalid Key or corrupted file"
  683. end
  684. end
Add Comment
Please, Sign In to add comment