#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2008
    Posts
    3
    Rep Power
    0

    How would I express this in Ruby?


    How would I express this line in Ruby?

    PHP
    PHP Code:
    $output.= chr(ord(substr($InString$i1)) ^ ($KeyList[$i strlen($Key)])); 
    ASP
    Code:
    strReturn = strReturn & chr( asc(mid(strIn,iInIndex,1)) XOR asc(mid(strKey,iKeyIndex,1)))
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2004
    Location
    Constant Limbo
    Posts
    989
    Rep Power
    363
    Code:
    output << (str[i] ^ keylist[i % key.size]).chr
    Of course, this is just a guess. I know nothing of PHP or ASP, I am only inferring what I can from the syntax of your examples.
    True happiness is not getting what you want, it's wanting what you've already got.

    My Blog
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2008
    Posts
    3
    Rep Power
    0
    Just for a little more background... I am trying to port this function from PHP to RoR.

    Heres the original PHP function.

    PHP Code:
    public function protx_simple_xor($in_string$key
        {
            
    $key_list = array(); // --- init key array
            
    $output ""// --- init output
            
    for($i 0$i strlen($key); $i++) // --- convert key to ascii
            
    {
                
    $key_list[$i] = ord(substr($key$i1));
             }
            for(
    $i 0$i strlen($in_string); $i++) // --- step thru key 1 char
            
    {
                
    // Get ASCII code from string, get ASCII code from key (loop through with MOD), XOR the two, get the character from the result
                // % is MOD (modulus), ^ is XOR
                
    $output .= chr(ord(substr($in_string$i1)) ^ ($key_list[$i strlen($key)]));
            }
            return 
    $output;
        } 
    And heres my attempted port.

    Code:
      def simple_xor(str = "test", key = ENCRYPTION_PASSWORD)
    		key_list = Array.new
    		out = ""
    		key.size.times do |i|
    			key_list[key_list.size + 1] = key.slice(i, 1).to_s[0]
    		end
    		str.size.times do |i|
    ## problem lies here somewhere
    			out << ((str.slice(i, 1))[0] ^ (key_list[i % key.size])).chr
    ## PHP: $output.= chr(ord(substr($InString, $i, 1)) ^ ($KeyList[$i % strlen($Key)]));
    ## ASP: strReturn = strReturn & chr( asc(mid(strIn,iInIndex,1)) XOR asc(mid(strKey,iKeyIndex,1)))
    		end
    		@out = out
      end
    the error i'm currently getting is 'no implicit conversion from nil to integer'
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2004
    Location
    Constant Limbo
    Posts
    989
    Rep Power
    363
    This follows exactly your code...
    Code:
    #!/usr/bin/env ruby
    
    key_list, output = [], ""
    key, str = "key", "input string"
    
    key.size.times { |i| key_list << key[i] }
    str.size.times do |i|
        output << (str[i] ^ key_list[i % key.size]).chr
    end
    But there are unnecessary steps in there. Consider...
    Code:
    output = ""
    key, str = "key", "input string"
    str.size.times { |i| output << (str[i] ^ key[i % key.size]).chr }
    Also, this type of stuff
    Code:
    str.slice(i, 1))[0]
    is wasteful since you generate an entire new string to only index that string (of length 1) for a single item. Why not just index the original string?

    As a side note, yout nil conversion seems to be coming from teh fact that you are filling the array starting at index 1 (key_list[key_list.size + 1]) but when you loop through and use the values later, you try to access index 0 (key_list[i % key.size])

    Comments on this post

    • sfusion agrees : thanks for your help!
    True happiness is not getting what you want, it's wanting what you've already got.

    My Blog
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2008
    Posts
    3
    Rep Power
    0
    you sir/madam are my hero of the day!

    thanks so much for your help, i've been struggling with this for days!
  10. #6
  11. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Alternatively, if you don't want to manually push characters onto a string one by one you could use map&join together.

    Code:
    k, s = "key", "string"
    
    (0...s.size).map{ |i| (s[i] ^ k[i % k.size]).chr }.join
    Take care,

    Mark.

    Comments on this post

    • L7Sqr agrees : Nice and elegant.
    programming language development: www.netytan.com Hula

  12. #7
  13. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    If that looks a little messy to you then you can always abstract the selection processes into something prettier, ala:

    Code:
    map("string", "key") { |a, b| (a ^ b).chr }.join
    Which is enabled by

    Code:
    def map(a, b)
      
      (0 ... a.size).map do |i|
        
         yield(a[i], b[i % b.size])
         
      end
    
    end
    You could make this more generally useful by ensuring that it maps over the largest of the input enumerable

    Code:
    def map(a, b)
      
      size = [a.size, b.size].max
      
      (0 ... size).map do |i|
        
         yield(a[i % a.size], b[i % b.size])
         
      end
    
    end
    If you wanted to take this further you could also make it an n-ary method and put it in a mixin class.

    Anyway, enjoy.

    Mark.
    programming language development: www.netytan.com Hula


IMN logo majestic logo threadwatch logo seochat tools logo