Advertisement
Guest User

Untitled

a guest
Oct 6th, 2015
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.10 KB | None | 0 0
  1. # ROT13
  2. # Joseph Ensminger
  3.  
  4. # encodes string
  5. def encode value, offset
  6. cypher = ""
  7. value.each_char do |c|
  8. if c.ord <= 90 && c.ord >= 65
  9. cypher << ((((c.ord - 65) + offset) % 26) + 65)
  10. elsif c.ord <= 122 && c.ord >= 91
  11. cypher << ((((c.ord - 97) + offset) % 26) + 97)
  12. else
  13. cypher << c
  14. end
  15. end
  16. cypher
  17. end
  18.  
  19. # decodes string
  20. def decode value, offset
  21. decypher = ""
  22. value.each_char do |c|
  23. if c.ord <= 90 && c.ord >= 65
  24. decypher << ((((c.ord - 65) - offset) % 26) + 65)
  25. elsif c.ord <= 122 && c.ord >= 91
  26. decypher << ((((c.ord - 97) - offset) % 26) + 97)
  27. else
  28. decypher << c
  29. end
  30. end
  31. decypher
  32. end
  33.  
  34. # determines which line in file to decode and encode. returns an output file.
  35. def caesarCipher (file, offset = 13)
  36. input = File.open(file, "r")
  37. output = File.open("output","w")
  38. input.each_line do |line|
  39. if line[0] == "e"
  40. line = line[2..-1]
  41. output.write(encode(line, offset))
  42. elsif line[0] == "d"
  43. line = line[2..-1]
  44. output.write(decode(line, offset))
  45. else
  46. puts "No command in file"
  47. end
  48. end
  49. input.close
  50. output.close
  51. end
  52.  
  53. # Testing on file
  54. caesarCipher("cipher",13)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement