Guest User

Untitled

a guest
May 16th, 2018
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.07 KB | None | 0 0
  1. # direction = 1
  2. # file = "plain.txt"
  3. # output = "output.txt"
  4.  
  5. direction = -1
  6. file = "output.txt"
  7. output = "deciphered.txt"
  8.  
  9. keys = []
  10. File.open("keys.txt","r") do |f|
  11. f.each_line do |l|
  12. keys << l.strip
  13. end
  14. end
  15.  
  16. lines = []
  17.  
  18. File.open(file,"r") do |f|
  19. cnt = 0
  20. f.each_line do |l|
  21. lines << l.strip
  22. end
  23. end
  24.  
  25. sentences = []
  26. len = lines.length/2
  27. len.times do |i|
  28. sentences << {:text => lines[i*2],
  29. :keynum => lines[i*2+1].to_i,
  30. :key => keys[lines[i*2+1].to_i-1]}
  31. end
  32.  
  33.  
  34. def cipher(fwd,text,key)
  35. over = key.length
  36. keycnt = 0
  37. outstr = ""
  38. text.length.times do |i|
  39. char = text[i]+key[keycnt]*fwd
  40. # char = 0 if char < 0
  41. outstr << char.chr
  42. keycnt+=1
  43. keycnt = 0 if keycnt > over - 1
  44. end
  45. return outstr
  46. end
  47.  
  48. sentences.each do |s|
  49. if s[:key]
  50. ciphered = cipher(direction,s[:text],s[:key])
  51. s[:ciphered] = ciphered
  52. else
  53. puts "Keys does not exist for #{s[:text]}, skipping"
  54. end
  55. end
  56.  
  57. File.open(output,"w") do |f|
  58. sentences.each do |s|
  59. if s[:key]
  60. f.puts s[:ciphered]
  61. f.puts s[:keynum]
  62. end
  63. end
  64. end
Add Comment
Please, Sign In to add comment