Take the 2-minute tour ×
Cryptography Stack Exchange is a question and answer site for software developers, mathematicians and others interested in cryptography. It's 100% free, no registration required.
module SimpleCrypt

    class Map
        attr_accessor :letters

        def initialize
            @letters = ('a'..'z').to_a
            @letters << "_"
            @letters.shuffle!
        end

    end

    class Cipher

        def initialize(map)
            @letters = map.letters
        end

        def mapping(type, character)
            matches(@letters, type, character)
        end

        def matches(letters, type, character)
            letters_copy = letters.dup
            encrypt = Proc.new{|letter| "#{letter}#{character}#{letters_copy.shift}" }
            decrypt = Proc.new{|letter| "#{letters_copy.shift}#{character}#{letter}" }
            if type == "encrypt"
                encrypt_map = ('a'..'z').to_a.map{ |letter|  encrypt[letter] }
                encrypt_map << "_#{character}#{letters_copy.shift}"
            else
                encrypt_map = ('a'..'z').to_a.map{ |letter|  decrypt[letter] }.sort
                encrypt_map << "#{letters_copy.shift}#{character}_"
            end
        end
    end

    class Encrypter < Cipher

        def mapping
            super('encrypt', @character)
        end

        def initialize(map, character = "=>")
            super(map)
            @character = character
        end

    end

    class Decrypter < Cipher

        def mapping
            super('decrypt', @character)
        end

        def initialize(map, character = "=>")
            super(map)
            @character = character
        end

    end

end

map = SimpleCrypt::Map.new
encrypter = SimpleCrypt::Encrypter.new(map)
puts encrypter.mapping
puts ""
decrypter = SimpleCrypt::Decrypter.new(map)
puts decrypter.mapping
share|improve this question
    
It would be helpful if you could explain what the code is doing. The code has quite a bit of syntactic sugar. –  Seth Sep 4 '11 at 0:09
    
Here is the output: gist.github.com/1191944#gistcomment-49027 –  Tyler Gillies Sep 4 '11 at 0:13
    
@Seth looks like it is Ruby. A quick glance makes me think this is a simple substitution cipher. In which case if you're directly transform English then it should be about as difficult to break as solving the cryptoquip in the newspaper. –  John Downey Sep 4 '11 at 0:17
6  
We like the questions here on a more theoretic level - e.g., please describe your algorithm in English, instead of pasting a pile of code so we have to try to interpret it. –  Paŭlo Ebermann Sep 4 '11 at 0:39
    
This question appears to be off-topic because it asks to analyze code for security vulnerabilities. –  mikeazo Oct 3 '13 at 0:38
add comment

closed as off-topic by e-sushi, B-Con, rath, mikeazo Oct 3 '13 at 0:38

  • This question does not appear to be about cryptography within the scope defined in the help center.
If this question can be reworded to fit the rules in the help center, please edit the question.

1 Answer

up vote 7 down vote accepted

This appears to be a simple substitution cipher. Substitution ciphers are vulnerable to frequency analysis, and would never be used in practice.

It would be very easy to guess what character is being used to encrypt underscores (spaces), and from there an attacker could guess common one-, two-, and three-letter words and begin unraveling your messages from there.

See the Wikipedia article "Substitution_cipher ~ Security_for_simple_substitution_ciphers" for more information.

share|improve this answer
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.