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 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值