4

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
Gleb Kemarsky
  • 8,401
  • 6
  • 37
  • 57
Openstar63
  • 159
  • 2
  • 3
  • 7
  • 1
    Are you trying to encrypt or decrypt here ? – Kethryweryn Sep 18 '13 at 16:15
  • Hi Kethryweryn. Both in my real code and this simplified example code, I am pretty sure that encoding or decoding with the message 'Abc' and the key 'AAA' should result in 'Abc' as the output. I've tried it on another implementations, for example here: http://sharkysoft.com/misc/vigenere/ Thank you. – Openstar63 Sep 18 '13 at 16:23

1 Answers1

2

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 !

Kethryweryn
  • 599
  • 4
  • 11
  • 1
    Oh and by the way, this code won't use the fact that the key is in uppercase or lowercase. Meaning that an uppercase uncrypted letter will be an uppercase encrypted letter every time. Don't forget to check the key offset too, instead of having a fixed 65. – Kethryweryn Sep 18 '13 at 16:51
  • Thank you. Very thorough answer. Works great for me. – Openstar63 Sep 18 '13 at 21:02