Advertisement
tom2018

file-transfer with encryption test

May 11th, 2013
274
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- By tom2018 5/10/2013 last updated 5/11/2013
  2. -- feel free to modify any part of this program
  3. -- just please send me a PM on the forums!
  4.  
  5. --encryption found when searching around, author unknown msg me on forums if you own this code and would
  6. --like me to remove it or give you credits
  7.  
  8. function enc(str, key) -- Function
  9. local tProp = {} -- Make table
  10. for i = 1, str:len() do -- loop for str length
  11.   local val = tonumber((str:byte(i) * tonumber(key) + (12.6 * key)) / 2.8)--Byte str times keyadd 12.6 times key and divide 2.8
  12.   table.insert(tProp, val) -- rest is to return the data
  13. end
  14. return tostring(table.concat(tProp,"|"))
  15. end
  16.  
  17. function dec(str, key)
  18. local data = {}
  19. for word in string.gmatch(str, "[^|]+") do
  20.   local rdata = ((tonumber(word)*2.8)-(12.6*key))/key
  21.   table.insert(data, math.floor(rdata+0.5))
  22. end
  23. return string.char(unpack(data))
  24. end
  25.  
  26.  
  27. --config
  28.  
  29. side = "top" --modem
  30. rand = false --more secure not yet implemented
  31. btlp = false --not yet implement
  32. stpmsg = "end" --msg that indicates end of file
  33. shtdown = false -- if true will shutdown at end of file transfer
  34. gfb = true -- return current file size and other usefull data
  35. pmsg = "ping" --default ping msg
  36. dc = false --basic encryption method (eta will be wrong)
  37. vsn = 1.4 -- version
  38. cp = true -- keep pinging if failed connection
  39.  
  40. --code
  41. rednet.open(side)
  42. term.clear()
  43. term.setCursorPos(1,1)
  44. print("tomsfiletranfer "..vsn.."  |")
  45. print("by tom2018           |")
  46. print("---------------------#")
  47. print('Type help for command list')
  48.  
  49. while true do
  50. ct = false
  51. write("action> ")
  52. p = read()
  53.  
  54. if p == "help" then
  55. print[[help -cmd list
  56. exit -leave program
  57. id -computer id
  58. files -send file
  59. filer -receive file]]
  60. ct = true
  61. end
  62.  
  63. if p == "id" then
  64. print(os.getComputerID())
  65. ct = true
  66. end
  67.  
  68. if p == "filer" then
  69. write("ID> ")
  70. cid = read()
  71. stop = false
  72. cid = cid + 0
  73. write("save as: ")
  74. nami = read()
  75. h = fs.open(nami,"w")
  76. write("ping msg> ")
  77. rednet.send(cid,read())
  78.  
  79. while stop == false do
  80. icid, pack = rednet.receive(2)
  81. if cp == false then
  82. if icid == nil then
  83. print("no connection established!")
  84. sleep(1.5)
  85. os.reboot()
  86. end
  87. else
  88. if icid == nil then
  89. print("no connection established.")
  90. sleep(1.5)
  91. end
  92. end
  93. if icid == cid then
  94. print("Received "..pack.." Saving to file")
  95.  
  96. if pack ~= stpmsg then
  97. h.write(pack)
  98. if gfb == true then
  99. print(fs.getSize(nami))
  100. end
  101. else
  102. print("file complete!")
  103. if dc == true then
  104. write("key> ")
  105. keys = read()
  106. h.close()
  107. h = fs.open(nami,'r')
  108. nonen = h.readAll()
  109. h.close()
  110. h = fs.open(nami,'w')
  111. h.write(dec(nonen,keys))
  112. end
  113. if dc == false then
  114. h.close()
  115. end
  116. stop = true
  117. if shtdown == true then
  118. os.shutdown()
  119. else
  120. os.reboot()
  121. end
  122. end
  123.  
  124. elseif icid ~= nil then
  125. print("Blocked Conection from ID: "..icid)
  126. end
  127. sleep(0)
  128. end
  129.  
  130. ct = true
  131. end
  132.  
  133. if p == "files" then
  134. write("file> ")
  135. nami = read()
  136. if dc == true then
  137. write("key> ")
  138. keys = read()
  139. end
  140. h = fs.open(nami,"r")
  141. if dc == false then
  142. wpack = h.readAll()
  143. else
  144. wpack = enc(h.readAll(), keys)
  145. end
  146. wpackt = {}
  147.  
  148. print("Dividing into Packets may take a while!")
  149. for char in string.gmatch(wpack, ".") do
  150. table.insert(wpackt, char)
  151. sleep(0)
  152. end
  153.  
  154. if gfb == true then
  155. print("hosting on "..os.getComputerID())
  156. eta = #wpackt * .01
  157. print("Estimated transfer time in seconds")
  158. print(eta)
  159. end
  160.  
  161. while true do
  162.  
  163. cid, auth = rednet.receive()
  164. stop = false
  165. c = 1
  166. wpn = #wpackt + 1
  167. if auth == "sup" then
  168. print("Seeding ID:"..cid)
  169. while c < wpn do
  170. rednet.send(cid, wpackt[c])
  171. c = c + 1
  172. sleep(0.01)
  173. end
  174. rednet.send(cid, stpmsg)
  175. print("Seed Complete!")
  176.  
  177. elseif auth == pmsg then
  178. rednet.send(cid,nami)
  179. end
  180. sleep(0)
  181. end
  182.  
  183. ct = true
  184. end
  185.  
  186. if p == "exit" then
  187. print("Thankyou for using Tomsfiletransfer.")
  188. sleep(1.5)
  189. os.reboot()
  190. ct = true
  191. end
  192.  
  193. if ct == false then
  194. print("Cmd not recongnized.")
  195. end
  196. sleep(0)
  197. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement