Advertisement
DOGGYWOOF

Doggy OS Bitlocker

Sep 3rd, 2024
4
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.12 KB | None | 0 0
  1. -- Encryption and Decryption program for ComputerCraft
  2. -- Encrypts and decrypts every file in the /disk/ directory using a Caesar cipher
  3.  
  4. local shift = 3 -- Number of positions to shift each character in the Caesar cipher
  5.  
  6. -- Function to encrypt a string using Caesar cipher
  7. local function encrypt(text, shift)
  8. local encryptedText = ""
  9. for i = 1, #text do
  10. local char = text:sub(i, i)
  11. local byte = string.byte(char)
  12. -- Encrypt only printable ASCII characters
  13. if byte >= 32 and byte <= 126 then
  14. local newByte = ((byte - 32 + shift) % 95) + 32
  15. encryptedText = encryptedText .. string.char(newByte)
  16. else
  17. encryptedText = encryptedText .. char
  18. end
  19. end
  20. return encryptedText
  21. end
  22.  
  23. -- Function to decrypt a string using Caesar cipher
  24. local function decrypt(text, shift)
  25. local decryptedText = ""
  26. for i = 1, #text do
  27. local char = text:sub(i, i)
  28. local byte = string.byte(char)
  29. -- Decrypt only printable ASCII characters
  30. if byte >= 32 and byte <= 126 then
  31. local newByte = ((byte - 32 - shift) % 95) + 32
  32. decryptedText = decryptedText .. string.char(newByte)
  33. else
  34. decryptedText = decryptedText .. char
  35. end
  36. end
  37. return decryptedText
  38. end
  39.  
  40. -- Function to process a file (encrypt or decrypt based on the action)
  41. local function processFile(filePath, action)
  42. local file = fs.open(filePath, "r")
  43. if not file then
  44. print("Failed to open file: " .. filePath)
  45. return
  46. end
  47.  
  48. local content = file.readAll()
  49. file.close()
  50.  
  51. local processedContent
  52. if action == "encrypt" then
  53. processedContent = encrypt(content, shift)
  54. elseif action == "decrypt" then
  55. processedContent = decrypt(content, shift)
  56. else
  57. print("Invalid action: " .. action)
  58. return
  59. end
  60.  
  61. -- Write processed content back to the file
  62. local processedFile = fs.open(filePath, "w")
  63. if not processedFile then
  64. print("Failed to write to file: " .. filePath)
  65. return
  66. end
  67.  
  68. processedFile.write(processedContent)
  69. processedFile.close()
  70.  
  71. print(action:capitalize() .. ": " .. filePath)
  72. end
  73.  
  74. -- Function to encrypt or decrypt all files in /disk/
  75. local function processAllFilesInDisk(action)
  76. local path = "/disk/"
  77.  
  78. if not fs.exists(path) then
  79. print("Path does not exist: " .. path)
  80. return
  81. end
  82.  
  83. local files = fs.list(path)
  84.  
  85. for _, file in ipairs(files) do
  86. local filePath = fs.combine(path, file)
  87. if not fs.isDir(filePath) then
  88. processFile(filePath, action)
  89. else
  90. print("Skipping directory: " .. filePath)
  91. end
  92. end
  93.  
  94. print("All files " .. action .. "ed.")
  95. end
  96.  
  97. -- Helper function to capitalize the first letter of a string
  98. function string:capitalize()
  99. return (self:gsub("^%l", string.upper))
  100. end
  101.  
  102. -- Start the process
  103. local action = "encrypt" -- Change this to "decrypt" to decrypt files
  104. processAllFilesInDisk(action)
  105.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement