douwen5833 2015-08-28 00:46
浏览 77
已采纳

将弱加密算法从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 2015-08-28 01:03
    关注

    Remove your ord function.

    This line in your CF:

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

    should be:

    char = Chr(Asc(char) + Asc(keychar));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog