Note that there are some explanatory texts on larger screens.

plurals
  1. POEncryption Algorithm from PHP to Ruby (Vignere variant)
    primarykey
    data
    text
    <p>I am a bit stuck with this. I have to interface with an api that uses a version of an encryption algorithm that they seem to have ripped from Typo3 written by Ari Kuorikoski.</p> <p>I need to create a ruby lib to interface with their api, so have to port their algorithm into ruby, and I am a bit out of my depth when it comes to encryption.</p> <p>This is the code:</p> <pre><code>private function keyED($txt) { $encrypt_key = md5($this-&gt;encrypt_key); $ctr=0; $tmp = ""; for ($i=0;$i&lt;strlen($txt);$i++) { if ($ctr==strlen($encrypt_key)) $ctr=0; $tmp.= substr($txt,$i,1) ^ substr($encrypt_key,$ctr,1); $ctr++; } return $tmp; } private function encrypt($txt){ srand((double)microtime()*1000000); $encrypt_key = md5(rand(0,32000)); $ctr=0; $tmp = ""; for ($i=0;$i&lt;strlen($txt);$i++){ if ($ctr==strlen($encrypt_key)) $ctr=0; $tmp.= substr($encrypt_key,$ctr,1) . (substr($txt,$i,1) ^ substr($encrypt_key,$ctr,1)); $ctr++; } return base64_encode($this-&gt;keyED($tmp)); } </code></pre> <p>The part that has me stumped is this, I have to write it for ruby 1.8.6 as that's the server that it will be on. And there's no XOR for strings. Not that I would understand it if there was.</p> <p>Any help, pointers, ideas would be much much appreciated.</p> <p>Addendum:</p> <p>I realize, I didn't put any code up, the only difficulty is actually the xor problem, but here is my code so far:</p> <pre><code>def xor(s1,s2) if s2.empty? then return s1 else a1 = s1.unpack("c*") a2 = s2.unpack("c*") a2 *= 2 while a2.length &lt; a1.length return a1.zip(a2).collect {|c1,c2| c1 ^ c2}.pack("c*") end end def keyED(str) encrypt_key = Digest::MD5.digest(@key) ctr = 0 tmp = '' for i in 0...str.length do ctr = 0 if ctr == encrypt_key.length tmp &lt;&lt; xor(str.slice(i,1), encrypt_key.slice(ctr,1)).to_s ctr = ctr + 1 end return tmp end # === Ported Code # This code was ported from Ari's Typo 3 Session Encryption def encrypt(str) encrypt_key = Digest::MD5.digest(rand(32000).to_s) ctr = 0 tmp = '' for i in 0...str.length do ctr=0 if ctr==encrypt_key.length tmp &lt;&lt; encrypt_key.slice(ctr,1) &lt;&lt; xor(str.slice(i,1), encrypt_key.slice(ctr,1)) ctr = ctr + 1 end return Base64.encode64(keyED(tmp)) end def decrypt(str) txt = keyED(str) tmp = '' for i in 0...txt.length do md = txt.slice(i,1) i = i + 1 tmp &lt;&lt; xor(txt.slice(i,1),md) end puts "Decrypte string:#{Base64.decode64(tmp)}EOSTRING" end </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload