douwen5833
douwen5833
2015-08-28 00:46

将弱加密算法从PHP转换为Coldfusion或Java

已采纳

I'm working with someone that is having me 'encrypt' some text I send to his webservice using the function listed below. I've tried to translate these into Coldfusion but my output doesn't match his version. I would rather simply use AES 256 and call it a day, but I literally have no choice as this is what they use. Can someone please help me translate this into either Coldfusion or Java.

function encrypt($string, $key) {
  $result = '';
  for($i=0; $i<strlen($string); $i++) {
    $char = substr($string, $i, 1);
    $keychar = substr($key, ($i % strlen($key))-1, 1);
    $char = chr(ord($char)+ord($keychar));
    $result.=$char;
}

  return base64_encode($result);
}

function decrypt($string, $key) {
  $result = '';
  $string = base64_decode($string);

  for($i=0; $i<strlen($string); $i++) {
    $char = substr($string, $i, 1);
    $keychar = substr($key, ($i % strlen($key))-1, 1);
    $char = chr(ord($char)-ord($keychar));
    $result.=$char;
  }

    return $result;
} 

My Coldfusion versions:

function hideText(argsString, key) {
  var result = '';
  for(i=1; i <= len(argsString); i++) {
    char = mid(argsString, i, 1);
    keychar = mid(key, i, 1);
    char = asc(ord(char) & ord(keychar));
    result &= char;
  }
    return toBase64(result);
}

function unHideText(argsString, key) {
  result = '';
  string = toString( ToBinary( argsString ) );

  for(i=1; i<= strlen(argsString); i++) {
    char = mid(argsString, i, 1);
    keychar = mid(key, i, 1);
    char = asc(ord(char) - ord(keychar));
    result &= char;
  }
    return result;
}

function ord(any argString){
    return Left(argString, 1);
}

My CF version isn't using the % as I think that might just evaluate to the same thing as i does anyway - and it actually produces a result without it. Though I may be missing why it was there in the first place. Anyone use both PHP and CF have any insight into a better translation? If its easier to explain in java I'm perfectly comfortable with that as well.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • duan7264 duan7264 6年前

    Remove your ord function.

    This line in your CF:

    char = asc(ord(char) & ord(keychar));
    

    should be:

    char = Chr(Asc(char) + Asc(keychar));
    
    点赞 评论 复制链接分享

相关推荐