将VB函数转换为Perl

时间:2012-02-02 17:14:35

标签: perl vbscript

有人可以帮助我将这个VB函数转换为Perl(PHP,Ruby或Python也会这样做)吗?

Public Function CFUSION_ENCRYPT(ByVal Password As String, ByVal Key As String) As String
  Dim NewValue As String
  Dim TempValue As String
  NewValue = ""
  For i = 1 To Len(Password)
   TempValue = Asc(Mid(Key, i, 1)) Xor Asc(Mid(Password, i, 1))
   NewValue = NewValue & Format(Hex(TempValue), "00")
  Next
  CFUSION_ENCRYPT = NewValue
End Function

非常感谢!

4 个答案:

答案 0 :(得分:2)

嗯,到底是什么...... perl:

sub cfusion_encrypt
{
    my ($password, $key) = @_;

    my @p = split( //, $password );
    my @k = split( //, $key );
    my $end = $#p < $#k ? $#p : $#k;  # which is shorter, key or password?
    my @result = ();

    for my $i ( 0 .. $end ) 
    {
        push @result, sprintf('%0.2x', ord($p[$i] ^ $k[$i]));
    }

    join( '', @result );
}
像@Cameron所说,不要以为这是好加密。此外,您可能希望确保密钥至少与密码一样长。

答案 1 :(得分:2)

与theglauber一起打高尔夫球:

join( '', List::MoreUtils::pairwise { 
                $a and $b and $a ^ $b or ''; 
          } 
            @{[ split( //, $password ) ]}
          , @{[ split( //, $key ) ]} 
       );

答案 2 :(得分:0)

在PHP中:

function cfusion_encrypt($password, $key) {
  $new = '';
  $tmp = '';
  for($i = 0, $l = strlen($password); $i < $l; ++i) {
    $tmp = ord(substr($key, $i, 1)) ^ ord(substr($password, $i, 1));
    $new &= sprintf('%02X', $tmp);
  }
  return $new;
}

请测试结果是否真的相同

答案 3 :(得分:0)

我的Perl有点生疏,这是一个Python版本(未经测试):

def CFUSION_ENCRYPT(password, key):
    if len(password) > len(key):
        raise Exception('Key must be at least as long as password')

    result = ''
    for password_char, key_char in zip(password, key):
        result += '%0.2X' % (ord(key_char) ^ ord(password_char))
    return result

我希望你不要把它用于那些本来应该加密的东西......