php - 从 PHP 到 Ruby 的加密算法(Vignere 变体)

标签 php ruby code-translation

我有点被这个困住了。我必须与一个使用加密算法版本的 api 交互,他们似乎从 Ari Kuorikoski 编写的 Typo3 中提取了该版本。

我需要创建一个 ruby​​ 库来与他们的 api 接口(interface),所以必须将他们的算法移植到 ruby​​ 中,而且在加密方面我有点不知所措。

这是代码:

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)); 
}

让我难过的部分是,我必须为 ruby​​ 1.8.6 编写它,因为它将在服务器上运行。而且字符串没有 XOR。并不是说如果有的话我会理解的。

我们将不胜感激任何帮助、指示和想法。

附录:

我意识到,我没有放任何代码,唯一的困难实际上是异或问题,但这是我目前的代码:

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

最佳答案

来自 Erik Veenstra :

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))

String#xor 的替代实现,由 jolierouge 建议和 David Garamond :

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

关于php - 从 PHP 到 Ruby 的加密算法(Vignere 变体),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2057601/

相关文章:

ruby-on-rails - Rails 模型中的邻近地区

ruby-on-rails - Ruby 2.5.1、Ruby Java Bridge (RJB) 和 Ubuntu 18.04 : Getting `Constants DL and Fiddle is not defined.`

java - 是否有一种类似 C 的迷你语法/语言可以同时翻译为 native C/C++ 和 Java?

c - 汇编将 MOV/MOVZX 和 MOVSX 转换为 C 代码(无内联汇编)

php - shell 相当于 php preg_match?

php - SilverStripe - 自定义分面搜索导航

ruby - 检查 "command"的输出应该包含 NilClass 的意外崩溃

php - SQL:如何在sql中实现特定表的多条数据的插入/更新

php - 当在 DDBB 上的浮点字段上记录时,MySql 将浮点值转换为整数

javascript - 是否可以将 C 代码编译为 Javascript 代码?