DOGGYWOOF

Simple Doggy OS Encryption algrothim

Sep 3rd, 2024
4
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.09 KB | None | 0 0
  1. -- Text Encryption Jambler in ComputerCraft
  2. -- This script will encrypt and decrypt text using a simple substitution cipher
  3.  
  4. -- Function to generate a key for encryption and decryption
  5. function generateKey()
  6. local chars = {}
  7. for i = 32, 126 do -- ASCII range for printable characters
  8. table.insert(chars, string.char(i))
  9. end
  10.  
  11. local key = {}
  12. while #chars > 0 do
  13. local index = math.random(#chars)
  14. table.insert(key, table.remove(chars, index))
  15. end
  16.  
  17. return key
  18. end
  19.  
  20. -- Function to create a map from a key
  21. function createMap(key)
  22. local map = {}
  23. for i = 32, 126 do
  24. map[string.char(i)] = key[i - 31]
  25. end
  26. return map
  27. end
  28.  
  29. -- Function to invert a map
  30. function invertMap(map)
  31. local invMap = {}
  32. for k, v in pairs(map) do
  33. invMap[v] = k
  34. end
  35. return invMap
  36. end
  37.  
  38. -- Function to encrypt text using a key
  39. function encrypt(text, key)
  40. local map = createMap(key)
  41. local encrypted = {}
  42. for i = 1, #text do
  43. local char = text:sub(i, i)
  44. table.insert(encrypted, map[char] or char)
  45. end
  46. return table.concat(encrypted)
  47. end
  48.  
  49. -- Function to decrypt text using a key
  50. function decrypt(text, key)
  51. local map = invertMap(createMap(key))
  52. local decrypted = {}
  53. for i = 1, #text do
  54. local char = text:sub(i, i)
  55. table.insert(decrypted, map[char] or char)
  56. end
  57. return table.concat(decrypted)
  58. end
  59.  
  60. -- Main Program
  61. local tArgs = {...}
  62. if #tArgs < 1 then
  63. print("Usage: jambler <encrypt|decrypt> <text>")
  64. return
  65. end
  66.  
  67. local mode = tArgs[1]
  68. local text = table.concat(tArgs, " ", 2)
  69.  
  70. -- Set a fixed key for simplicity. In a real scenario, this should be securely shared or dynamically generated.
  71. local key = generateKey()
  72.  
  73. if mode == "encrypt" then
  74. local encryptedText = encrypt(text, key)
  75. print("Encrypted Text: " .. encryptedText)
  76. elseif mode == "decrypt" then
  77. local decryptedText = decrypt(text, key)
  78. print("Decrypted Text: " .. decryptedText)
  79. else
  80. print("Invalid mode. Use 'encrypt' or 'decrypt'.")
  81. end
  82.  
Add Comment
Please, Sign In to add comment