Advertisement
HPWebcamAble

Password Encrypter (WIP)

Jun 28th, 2015
380
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.34 KB | None | 0 0
  1. --=== Variables ===--
  2. local filePath = "save.pass"
  3. local settings = {
  4.   back = colors.black,
  5.   text = colors.white
  6. }
  7. local w,h = term.getSize()
  8. os.loadAPI("aes")
  9.  
  10. --=== Functions ===--
  11. local function clear()
  12.   term.clear()
  13.   term.setCursorPos(1,1)
  14. end
  15.  
  16. local function scrollRead(x,y,nLength,insertText) --This is a simple scrolling-read function I made
  17.   if insertText then
  18.     insertText = tostring(insertText)
  19.     cPos = #insertText+1
  20.     cInput = insertText
  21.   else
  22.     cPos = 1
  23.     cInput = ""
  24.   end
  25.   term.setCursorBlink(true)
  26.   while true do
  27.     term.setCursorPos(x,y)
  28.     term.write(string.rep(" ",nLength))
  29.     term.setCursorPos(x,y)
  30.     if string.len(cInput) > nLength-1 then
  31.       term.write(string.sub(cInput,(nLength-1)*-1))
  32.     else
  33.       term.write(cInput)
  34.     end
  35.     if cPos > nLength-1 then
  36.       term.setCursorPos(x+nLength-1,y)
  37.     else  
  38.      term.setCursorPos(x+cPos-1,y)
  39.     end
  40.     local event,p1 = os.pullEvent()
  41.     if event == "char" then
  42.       cInput = string.sub(cInput,1,cPos)..p1..string.sub(cInput,cPos+1)
  43.       cPos = cPos + 1                  
  44.     elseif event == "key" then
  45.       if p1 == keys.enter then
  46.         break
  47.       elseif p1 == keys.backspace then
  48.         if cPos > 1 then
  49.           cInput = string.sub(cInput,1,cPos-2)..string.sub(cInput,cPos)
  50.           cPos = cPos - 1
  51.         end
  52.       elseif p1 == keys["end"] then
  53.         cPos = string.len(cInput)+1
  54.       end    
  55.     end
  56.   end
  57.   term.setCursorBlink(false)
  58.   return cInput
  59. end
  60.  
  61. function printC(text,y,screen)--Prints text centered at y on screen (term or monitor)
  62.   if not text then error("expected string,number",2) end
  63.   y = tonumber(y)
  64.   if not y then error("expected string,number",2) end
  65.   screen = screen or term
  66.   local tLenght = #tostring(text)
  67.   local sStart = math.ceil(w/2-tLenght/2)
  68.   local sEnd = sStart + tLenght
  69.   screen.setCursorPos(sStart,y)
  70.   screen.write(text)
  71.   return sStart,sEnd
  72. end
  73.  
  74.  
  75. --=== Program ===--
  76. term.setBackgroundColor(settings.back)
  77. clear()
  78. term.setTextColor(settings.text)
  79. printC("Welcome to Password Encrypter",2)
  80.  
  81. --local hasBeenOpened = false
  82. --if fs.exists(filePath) then -- Was likely used before, get password
  83. --  printC("Enter master password",5)
  84. --  local pass = scrollRead(14,6,10)
  85.  
  86. --end
  87. local continue = true
  88. while continue do
  89.   printC("Enter a key:",5)
  90.   printC("Enter a secret:",8)
  91.   local key = scrollRead(17,6,17)
  92.   local input = scrollRead(17,9,17)
  93.  
  94.   printC("Preforming Calculations...",h)
  95.  
  96.   local enc = aes.encrypt(key,input)
  97.   local f = fs.open(filePath,"wb")
  98.   for i = 1, #enc do
  99.     local cur = string.sub(enc,i,i)
  100.     if cur == "\n" then error("newline Detected!") end
  101.     f.write(string.byte(cur))
  102.   end
  103.   f.close()
  104.   local f2 = fs.open(filePath,"rb")
  105.   local dec = ""
  106.   for byte in f2.read do
  107.     dec = dec..string.char(byte)
  108.   end
  109.   f2.close()
  110.   dec = aes.decrypt(key,dec)
  111.  
  112.   clear()
  113.   printC("Key:",2)
  114.   printC(key,3)
  115.  
  116.   printC("Secret",5)
  117.   printC(input,6)
  118.  
  119.   printC("Encrypted:",8)
  120.   term.setTextColor(colors.black)
  121.   term.setBackgroundColor(colors.white)
  122.   printC(enc,9)
  123.   term.setTextColor(colors.white)
  124.   term.setBackgroundColor(colors.black)
  125.  
  126.   printC("Decrypted:",11)
  127.   printC(dec,12)
  128.  
  129.   printC("Press 'Enter' to continue",h)
  130.   repeat
  131.     local event,tKey = os.pullEvent("key")
  132.   until tKey == keys.enter
  133.  
  134.   clear()
  135. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement