2010-01-13 5 views
2

나는 이것으로 조금 붙어있다. 나는 그들이 Ario Kuorikoski가 쓴 Typo3에서 찢어진듯한 암호화 알고리즘 버전을 사용하는 API와 인터페이스해야한다.PHP에서 Ruby (Vignere 변형) 암호화 알고리즘

API와 인터페이스하기 위해 루비를 만들어야하기 때문에 루비로 자신의 알고리즘을 포팅해야합니다. 암호화에 관해서는 약간 깊이 있습니다. 날이, 나는 그게가 될 서버의로 루비 1.8.6를 위해 그것을 쓸 필요가있다 난처한 상황에 빠진

private function keyED($txt) { 
$encrypt_key = md5($this->encrypt_key); 
$ctr=0; 
$tmp = ""; 
for ($i=0;$i<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<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->keyED($tmp)); 
} 

부분 :

는 코드입니다. 문자열에는 XOR이 없습니다. 그게 내가 이해할 수 없다면.

어떤 도움, 조언, 아이디어라도 많이 감사하겠습니다.

부록 :

내가 어떤 코드까지를 두지 않았다 실현, 유일한 어려움은 실제로 XOR 문제이지만, 여기에 지금까지 내 코드입니다 : Erik Veenstra에서

def xor(s1,s2) 
     if s2.empty? then 
      return s1 
     else 
      a1 = s1.unpack("c*") 
      a2 = s2.unpack("c*") 
      a2 *= 2 while a2.length < 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 << 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 << encrypt_key.slice(ctr,1) << 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 << xor(txt.slice(i,1),md) 
     end 
     puts "Decrypte string:#{Base64.decode64(tmp)}EOSTRING" 
    end 
+0

한 가지, 당신은 STR [I]와 ENCRYPT_KEY [CTR]을 시도하여 Fixnum이라는를 얻고, 하나가 작동하지 않았다 그 ... 그상의 XOR를 수행하지만 수도 수 있다는 것입니다 코드의 다른 부분을 어색하게 만들었습니다. –

+0

업데이트로, 나는 php 함수를 사용하여 tokenmaker.php 파일을 만들고 net/http를 사용하여이를 호출함으로써 문제를 해결합니다. 나는 이런 식으로하는 것이 싫지만 일정대로 진행된다. 정말이 문제에 대한 해결책을 찾고 싶습니다. –

답변

2

:

class String 
    def xor(other) 
    if other.empty? 
     self 
    else 
     a1  = self.unpack("c*") 
     a2  = other.unpack("c*") 
     a2 *= 2 while a2.length < a1.length 
     a1.zip(a2).collect{|c1,c2| c1^c2}.pack("c*") 
    end 
    end 
end 

는 그런 다음 코드는

tmp << str.slice(i,1).xor(encrypt_key.slice(i,1)) 
,369된다

jolierougeDavid Garamond에 의해 제안 String#xor의 또 다른 구현 :

class String 
    def xor(other) 
    raise ArgumentError, "Can't bitwise-XOR a String with a non-String" unless other.kind_of? String 
    raise ArgumentError, "Can't bitwise-XOR strings of different length" unless self.length == other.length 
    (0..self.length-1).collect { |i| self[i]^other[i] }.pack("C*") 
    end 
end 
+0

이 변형을 시도했는데 문제는 문자열이 PHP xor와 같은 방식으로 작동하지 않는 이유를 설명 할 지식이 없다는 것입니다. 변형없이 PHP 함수에서 수행하는 작업을 정확히 수행해야합니다. PHP에서 이식 된 해독 함수를 작성 했으므로 문자열을 넣을 수 있어야하고 동일한 문자열을 가져올 수 있어야합니다. –

+0

또는 뭔가 다른 것이 틀릴 수도 있으므로 지금까지 루비 소스를 업데이트 할 것입니다. –

+0

다른 사람들이 읽을 수 있도록 업데이트 및 작업 소스를 게시합니다. –

0

편집자 주 : 마지막 작업 코드는 자신의 대답 질문에서 이동되었습니다.

제임스 유용한 답변, 주요 소품을 기반으로하는 최종 작업 소스! 그리고 David Garamound에게. 내가 찾았어요

def xor(s1,s2) 
    raise ArgumentError, "Can't bitwise-XOR a String with a non-String" unless s2.kind_of? String 
    raise ArgumentError, "Can't bitwise-XOR strings of different length" unless s1.length == s2.length 
    (0..s1.length-1).collect { |i| s1[i]^s2[i] }.pack("C*") 
end 

def keyED(txt,key) 
    ctr,tmp = 0,'' 
    key = Digest::MD5.hexdigest(key) 
    for i in 0...txt.length do 
     ctr = 0 if ctr == key.length 
     str = xor(txt.slice(i,1),key.slice(ctr,1)) 
     tmp << str 
     ctr = ctr + 1 
    end 

    return tmp 
end 
def encrypt(txt,key) 
    ctr,tmp = 0,'' 
    ekey = Digest::MD5.hexdigest(rand(32000).to_s) 
    for i in 0...txt.length do 
     ctr = 0 if ctr == ekey.length 
     str = xor(txt.slice(i,1), ekey.slice(ctr,1)) 
     tmp << ekey.slice(ctr,1) << str 
     ctr = ctr + 1 
    end 
    return Base64.encode64(keyED(tmp,key)) 
end