Skip to main content
removed tag from title
Link
Quill
  • 12.1k
  • 5
  • 41
  • 94

Caesar Cipher Encryptor in Rubyencryptor using ASCII

deleted 51 characters in body
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

As the title says, I have built a Caesar Cipher in Ruby that utilizes ASCII for the encryption. I am not sure whether or not this was the best way to go about the task but this way made the most sense to me. My code is below. Please give me any feedback that comes to mind. Thanks

As the title says, I have built a Caesar Cipher in Ruby that utilizes ASCII for the encryption. I am not sure whether or not this was the best way to go about the task but this way made the most sense to me. My code is below. Please give me any feedback that comes to mind. Thanks

I have built a Caesar Cipher in Ruby that utilizes ASCII for the encryption. I am not sure whether or not this was the best way to go about the task but this way made the most sense to me. Please give me any feedback that comes to mind.

Source Link
scobo
  • 601
  • 5
  • 11

Caesar Cipher Encryptor in Ruby using ASCII

As the title says, I have built a Caesar Cipher in Ruby that utilizes ASCII for the encryption. I am not sure whether or not this was the best way to go about the task but this way made the most sense to me. My code is below. Please give me any feedback that comes to mind. Thanks

puts 'Please enter your string to be coded'
    string = gets.chomp.split("")

puts 'Please enter your number key'
    key = gets.chomp.to_i

    if (key > 26) 
        key = key % 26
    end

    string.map! do |i|
        i.ord
    end

    string.map! {|i| i = i + key}.map! {|i| 
        if (i > 122)
            then i = i - 26
        elsif (90 < i && i < 97)
            then i = i - 26
        elsif (i > 96 && (i - key) < 91 && (i - key) > 64)
            then i = i - 26
        elsif (i < 65 )
            then i = 32
        else
            i
        end
    }

    string.map! do |i|
        i.chr 
    end

    puts "Your coded word is #{string.join("")}"

puts 'Return back to original string? (Yes or No)'
    response = gets.chomp.downcase

    if response == 'yes'
        string.to_s.split("")
        string.map! do |i|
            i.ord
        end
    
        string.map! {|i| i - key}.map! {|i| 
            if (90 < i && i < 97)
                then i = i + 26
            elsif ((i+key) > 96 && i < 91 && i > 64)
                then i = i + 26
            elsif (i < 65 && (i+key) < 91 && (i + key) > 64)
                then i = i + 26
            elsif (i < 65)
                then i = 32
            else
                i
            end
        }

        string.map! do |i|
            i.chr
        end

        puts string.join("")
        
    end