duanmo6937 2013-09-18 16:05
浏览 62
已采纳

PHP中的Vigenere

could anyone help me fix this Vigenere cypher in PHP?

Sorry for the ripped up code, that's from where I have been dissecting it for hours - trying to fix!

Anyhow, the code outputs 'Ace' when it should output 'Abc'.

There is some weird double offset which I don't have the maths brain to fix! Thanks for reading.

The code originates from here in AutoHotkey script - I have attempted to transcribe it. There are PHP Vigenere examples on the web (although not on Rosetta Code, weirdly!).. but anyhow, this one is modified to accept lower case as well as the standard capitals. Thanks.

  $key = "AAA";
  $keyLength = 3;
  $keyIndex = 0;
  $messageAsArray[0] = "A";
  $messageAsArray[1] = "b";
  $messageAsArray[2] = "c";

  foreach ($messageAsArray as $value)    //Loop through input string array
  {  
      $thisValueASCII = ord($value);
      if ($thisValueASCII >= 65 && $thisValueASCII <= 90) //if is uppercase                                  
      { 
        $thisValueASCIIOffset = 65; 
      }
      else //if is lowercase
      {
        $thisValueASCIIOffset = 97;  
      }
      $thisA = $thisValueASCII - $thisValueASCIIOffset;
      $thisB = fmod($keyIndex,$keyLength);
      $thisC = substr($key, $thisB, 1);
      $thisD = ord($thisC) - 65;
      $thisE = $thisA + $thisD;
      $thisF = fmod($thisE,26);
      $thisG = $thisF + $thisValueASCII  ;
      $thisOutput = chr($thisG); 
      $output = $output . $thisOutput  ; 
      $keyIndex++;
  }
  echo $output
  • 写回答

1条回答 默认 最新

  • dongyong1942 2013-09-18 16:49
    关注

    Ok, I read your code.

    You're encoding, and your error is quite simple :

    $thisG = $thisF + $thisValueASCII  ;
    

    In this step, $thisF is your encrypted letter, which value is between 0 and 25. You want to print it as an ascii char and, instead of adding the offset, you're adding the uncrypted ascii value, which makes no sense.

    You should have :

    $thisG = $thisF + $thisValueASCIIOffset;
    

    A few tips. You don't need to have your text or key as an array, you can use it as if it was one.

    You can use the % operator instead of fmod. Makes the code easier to read, but it is just a personnal preference.

    For instance :

    $key = "AAA";
    $keyLength = strlen($key);
    $keyIndex = 0;
    
    $message = str_split("Abc");
    
    $output = '';
    
    foreach($message as $value) // Loop through input string array
    {
        $thisValueASCII = ord($value);
    
        if($thisValueASCII >= 65 && $thisValueASCII <= 90) // if is uppercase
        {
            $thisValueASCIIOffset = 65;
        } else // if is lowercase
        {
            $thisValueASCIIOffset = 97;
        }
    
        $letter_value_corrected = $thisValueASCII - $thisValueASCIIOffset;
        $key_index_corrected = $keyIndex % $keyLength; // This is the same as fmod but I prefer this notation.
    
        $key_ascii_value = ord($key[$key_index_corrected]);
    
        if($key_ascii_value >= 65 && $key_ascii_value <= 90) // if is uppercase
        {
            $key_offset = 65;
        } else // if is lowercase
        {
            $key_offset = 97;
        }
    
        $final_key = $key_ascii_value - $key_offset;
    
        $letter_value_encrypted = ($letter_value_corrected + $final_key)%26;
    
        $output = $output . chr($letter_value_encrypted + $thisValueASCIIOffset);
        $keyIndex++;
    }
    
    echo $output;
    

    Have fun and good luck for your implementation !

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?