Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def encrypt(string, shift)
- string.map! do |char|
- if char.ord == 32
- char
- elsif (char.ord + shift) < 123
- (char.ord + shift).chr
- else
- (char.ord + shift - 26).chr
- end
- end
- string.join
- end
- def decrypt(string, shift)
- string.map! do |char|
- if char.ord == 32
- char
- elsif (char.ord - shift) > 96
- (char.ord - shift).chr
- else
- (char.ord - shift + 26).chr
- end
- end
- string.join
- end
- puts "Enter filename to encrypt and shift: "
- filename = gets.chomp
- shift = gets.to_i
- encrypted = ""
- File.open(filename, "r") do |f|
- f.each_line do |line|
- encrypted += encrypt(line.chomp.chars, shift) + "\n"
- end
- end
- puts encrypted
- File.open(filename, "w") do |line|
- line.puts "\r" + encrypted
- end
- puts "Enter filename to decrypt and shift: "
- filename = gets.chomp
- shift = gets.to_i
- decrypted = ""
- File.open(filename, "r") do |f|
- f.each_line do |line|
- decrypted += decrypt(line.chomp.chars, shift) + "\n"
- end
- end
- puts decrypted
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement