PHP中的Vigenere

PHP中的Vigenere,php,encryption,vigenere,Php,Encryption,Vigenere,有人能帮我用PHP修复这个Vigenere密码吗 很抱歉,代码被撕碎了,这是我几个小时来一直在剖析它的地方——试图修复它 无论如何,代码在应该输出“Abc”时输出“Ace” 有一些奇怪的双偏移量,我没有数学头脑去修正!谢谢你的阅读 代码源于自动热键脚本-我已尝试转录它。web上有一些PHP Vigenere示例(尽管没有,奇怪的是!)。。但无论如何,这一个被修改为接受小写字母和标准大写字母。谢谢 $key = "AAA"; $keyLength = 3; $keyIndex = 0;

有人能帮我用PHP修复这个Vigenere密码吗

很抱歉,代码被撕碎了,这是我几个小时来一直在剖析它的地方——试图修复它

无论如何,代码在应该输出“Abc”时输出“Ace”

有一些奇怪的双偏移量,我没有数学头脑去修正!谢谢你的阅读

代码源于自动热键脚本-我已尝试转录它。web上有一些PHP Vigenere示例(尽管没有,奇怪的是!)。。但无论如何,这一个被修改为接受小写字母和标准大写字母。谢谢

  $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
$key=“AAA”;
$keyLength=3;
$keyIndex=0;
$messageAsArray[0]=“A”;
$messageAsArray[1]=“b”;
$messageAsArray[2]=“c”;
foreach($messageAsArray作为$value)//循环输入字符串数组
{  
$thisValueASCII=ord($value);
如果($thisValueASCII>=65&&$thisValueASCII确定,我将读取您的代码

您正在编码,错误很简单:

$thisG = $thisF + $thisValueASCII  ;
在此步骤中,$thisF是加密的字母,其值介于0和25之间。您希望将其打印为ascii字符,而不是添加偏移量,而是添加未加密的ascii值,这毫无意义

你应该:

$thisG = $thisF + $thisValueASCIIOffset;
一些提示。 您不需要将文本或键作为数组,您可以像使用数组一样使用它

您可以使用%运算符而不是fmod。这使代码更易于阅读,但这只是个人的首选项

例如:

$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;
$key=“AAA”;
$keyLength=strlen($key);
$keyIndex=0;
$message=str_split(“Abc”);
$output='';
foreach($messageas$value)//循环输入字符串数组
{
$thisValueASCII=ord($value);

如果($thisValueASCII>=65&&$thisValueASCII=65&&$key\u ascii\u value您想在这里加密或解密吗?嗨,Kethryweryn。在我的实际代码和这个简化的示例代码中,我非常确定使用消息“Abc”和密钥“AAA”进行编码或解码应该会导致“Abc”作为输出。我已经在另一个实现中尝试过,例如这里的r示例:谢谢。哦,顺便说一句,这段代码不会使用密钥是大写或小写的事实。这意味着一个大写未加密的字母每次都是大写加密的字母。不要忘记检查密钥偏移量,而不是有一个固定的65。谢谢。非常彻底的回答。对我来说非常有用。