Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def encode value, offset = 13
- cypher = ""
- value.each_char do |c|
- if c.ord <= 90 && c.ord >= 65
- cypher << ((((c.ord - 65)+ offset) % 26) + 65)
- elsif c.ord <= 122 && c.ord >= 91
- cypher << ((((c.ord - 97)+ offset) % 26) + 97)
- else
- cypher << c
- end
- end
- cypher
- end
- def decode value, offset = 13
- decypher = ""
- value.each_char do | c |
- if c.ord <= 90 && c.ord >= 65
- decypher << ((((c.ord - 65)-offset) % 26) + 65)
- elsif c.ord <= 122 && c.ord >= 91
- decypher << ((((c.ord - 97)-offset) % 26) + 97)
- else
- decypher << c
- end
- end
- decypher
- end
- def code (file, offset = 13)
- input = File.open('assignment.txt', "r")
- output = File.open('assignment.txt', "a")
- input.each_line do |line|
- if line[0] == "e"
- line = line[2..-1]
- output.write(encode(line, offset))
- elsif line[0] == "d"
- line = line[2..-1]
- output.write(decode(line, offset))
- else
- puts "Not a thing"
- end
- end
- input.close
- output.close
- end
- code("decypher",13)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement