Advertisement
Guest User

Untitled

a guest
Oct 6th, 2015
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.95 KB | None | 0 0
  1.  
  2. def encode value, offset = 13
  3. cypher = ""
  4. value.each_char do |c|
  5. if c.ord <= 90 && c.ord >= 65
  6. cypher << ((((c.ord - 65)+ offset) % 26) + 65)
  7. elsif c.ord <= 122 && c.ord >= 91
  8. cypher << ((((c.ord - 97)+ offset) % 26) + 97)
  9. else
  10. cypher << c
  11. end
  12. end
  13. cypher
  14. end
  15.  
  16. def decode value, offset = 13
  17. decypher = ""
  18. value.each_char do | c |
  19. if c.ord <= 90 && c.ord >= 65
  20. decypher << ((((c.ord - 65)-offset) % 26) + 65)
  21. elsif c.ord <= 122 && c.ord >= 91
  22. decypher << ((((c.ord - 97)-offset) % 26) + 97)
  23. else
  24. decypher << c
  25. end
  26. end
  27. decypher
  28. end
  29.  
  30. def code (file, offset = 13)
  31. input = File.open('assignment.txt', "r")
  32. output = File.open('assignment.txt', "a")
  33. input.each_line do |line|
  34. if line[0] == "e"
  35. line = line[2..-1]
  36. output.write(encode(line, offset))
  37. elsif line[0] == "d"
  38. line = line[2..-1]
  39. output.write(decode(line, offset))
  40. else
  41. puts "Not a thing"
  42. end
  43. end
  44. input.close
  45. output.close
  46. end
  47.  
  48. code("decypher",13)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement