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 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)