PrinceOfCookies

Untitled

Jul 22nd, 2024 (edited)
18
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.02 KB | None | 0 0
  1. -- Vigenère cipher encryption
  2. function v(k, m)
  3. local e = {}
  4. local l = #k
  5. for i = 1, #m do
  6. local a = k:byte((i - 1) % l + 1)
  7. local b = m:byte(i)
  8. e[i] = string.char(bit32.bxor(b, a))
  9. end
  10. return table.concat(e)
  11. end
  12.  
  13. -- Base64 encoding
  14. local B = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
  15. function b64e(d)
  16. local e = {}
  17. local l = #d
  18. local i = 1
  19. while i <= l do
  20. local a = d:byte(i) or 0
  21. local b = d:byte(i + 1) or 0
  22. local c = d:byte(i + 2) or 0
  23. local n = (a * 65536) + (b * 256) + c
  24. e[#e + 1] = B:sub(math.floor(n / 262144) + 1, math.floor(n / 262144) + 1)
  25. e[#e + 1] = B:sub(math.floor(n / 4096) % 64 + 1, math.floor(n / 4096) % 64 + 1)
  26. e[#e + 1] = B:sub(math.floor(n / 64) % 64 + 1, math.floor(n / 64) % 64 + 1)
  27. e[#e + 1] = B:sub(n % 64 + 1, n % 64 + 1)
  28. i = i + 3
  29. end
  30. local p = l % 3
  31. if p == 1 then
  32. e[#e - 1] = '='
  33. e[#e] = '='
  34. elseif p == 2 then
  35. e[#e] = '='
  36. end
  37. return table.concat(e)
  38. end
  39.  
  40. -- Base64 decoding
  41. function b64d(e)
  42. local d = {}
  43. local i = 1
  44. local l = #e
  45. local function c2v(c)
  46. local idx = B:find(c)
  47. return idx and (idx - 1) or 0
  48. end
  49. while i <= l do
  50. local a = c2v(e:sub(i, i))
  51. local b = c2v(e:sub(i + 1, i + 1))
  52. local c = c2v(e:sub(i + 2, i + 2))
  53. local e = c2v(e:sub(i + 3, i + 3))
  54. local n = (a * 262144) + (b * 4096) + (c * 64) + e
  55. d[#d + 1] = string.char(math.floor(n / 65536) % 256)
  56. if b ~= 64 then d[#d + 1] = string.char(math.floor(n / 256) % 256) end
  57. if c ~= 64 then d[#d + 1] = string.char(n % 256) end
  58. i = i + 4
  59. end
  60. return table.concat(d)
  61. end
  62.  
  63. -- Example usage
  64. local k = "HTIWaoithwaioPTHio"
  65. local m = "Hello World"
  66. local b64 = b64e(m)
  67. print("Base64 Encoded:", b64)
  68. local e = v(k, b64)
  69. print("Vigenère Encrypted:", e)
  70. print("Base64 Decoded:", b64d(v(k, e)))
  71.  
Add Comment
Please, Sign In to add comment