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));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 vb net 使用 sendMessage 如何输入鼠标坐标
  • ¥200 求能开发抖音自动回复卡片的软件
  • ¥15 关于freesurfer使用freeview可视化的问题
  • ¥100 谁能在荣耀自带系统MagicOS版本下,隐藏手机桌面图标?
  • ¥15 求SC-LIWC词典!
  • ¥20 有关esp8266连接阿里云
  • ¥15 C# 调用Bartender打印机打印
  • ¥15 我这个代码哪里有问题 acm 平台上显示错误 90%,我自己运行好像没什么问题
  • ¥50 C#编程中使用printDocument类实现文字排版打印问题
  • ¥15 找会编程的帅哥美女 可以用MATLAB里面的simulink编程,用Keil5编也可以。