Search code examples
phpencryptionvigenere

Vigenere in PHP


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

Solution

  • 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 !