Advertisement
DOGGYWOOF

DOGGY OS BETA PASSWORD MANAGER

Jul 12th, 2024 (edited)
15
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.32 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. -- Function to read passwords from a file and decrypt them
  61. function readPasswords(filename, key)
  62. local file = fs.open(filename, "r")
  63. if not file then
  64. return {}
  65. end
  66.  
  67. local encryptedData = file.readAll()
  68. file.close()
  69. local decryptedData = decrypt(encryptedData, key)
  70.  
  71. local passwords = {}
  72. for line in decryptedData:gmatch("[^\r\n]+") do
  73. table.insert(passwords, line)
  74. end
  75.  
  76. return passwords
  77. end
  78.  
  79. -- Function to write passwords to a file after encrypting them
  80. function writePasswords(filename, passwords, key)
  81. local data = table.concat(passwords, "\n")
  82. local encryptedData = encrypt(data, key)
  83.  
  84. local file = fs.open(filename, "w")
  85. file.write(encryptedData)
  86. file.close()
  87. end
  88.  
  89. -- Main Program
  90. local tArgs = {...}
  91. if #tArgs < 1 then
  92. print("Usage: jambler <add|show>")
  93. return
  94. end
  95.  
  96. local mode = tArgs[1]
  97. local filename = "passwords.txt"
  98. local key = generateKey() -- In a real scenario, use a securely stored key
  99. local passwords = readPasswords(filename, key)
  100.  
  101. if mode == "add" then
  102. if #tArgs < 2 then
  103. print("Usage: jambler add <password>")
  104. return
  105. end
  106. local password = table.concat(tArgs, " ", 2)
  107. table.insert(passwords, password)
  108. writePasswords(filename, passwords, key)
  109. print("Password added.")
  110. elseif mode == "show" then
  111. print("Enter the decrypted password to check:")
  112. local inputPassword = read()
  113.  
  114. local inputEncrypted = encrypt(inputPassword, key)
  115.  
  116. local found = false
  117. for _, password in ipairs(passwords) do
  118. if encrypt(password, key) == inputEncrypted then
  119. print("logon success")
  120. found = true
  121. break
  122. end
  123. end
  124.  
  125. if not found then
  126. print("failure")
  127. end
  128. else
  129. print("Invalid mode. Use 'add' or 'show'.")
  130. end
  131.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement