2016-04-14 5 views
0

OTPメソッドを使用してPHPでファイルを暗号化するための簡単なライブラリを作成しようとしています。私の問題は、復号化されたコードの一部の文字が元のものと異なることです。私はほぼ一週間でしたが、結果はありませんでした。 base64文字やエンコーディング/デコードのメカニズムに問題はありますか?PHP Otpライブラリのbase64

多くの回答に感謝します。

final class Otp 
{ 

    private static $charSet = array('+','/','0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F','G','H','I','J','K','L', 
    'M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r', 
    's','t','u','v','w','x','y','z'); 

    public static function encryptFile($originalFilePath, $encryptedFilePath, $keyFilePath) 
    { 

     if(!self::existsFile($keyFilePath) || !self::existsFile($encryptedFilePath)) { 

      if($originalFileData = self::existsFile($originalFilePath)) { 

       $originalFileBase64Data = base64_encode($originalFileData); 
       $originalFileBase64DataLength = strlen($originalFileBase64Data) - 1; 
       $originalFileBase64DataArray = str_split($originalFileBase64Data); 

       $encryptedData = NULL; 
       $encryptedDataKey = NULL; 
       for ($i = 0; $i <= $originalFileBase64DataLength; $i++) { 

        $randKey = rand(0, sizeOf(self::$charSet) - 1); 
        $arrayKey = array_search($originalFileBase64DataArray[$i], self::$charSet); 

        if($randKey > $arrayKey) { 
         $str = '-' . ($randKey - $arrayKey); 
        } elseif($randKey < $arrayKey) { 
         $str = ($randKey + $arrayKey); 
        } else { 
         $str = $randKey; 
        } 

        $encryptedData .= self::$charSet[$randKey]; 
        $encryptedDataKey .= $str. ';'; 

       } 

       $encryptedDataString = $encryptedData; 
       $encryptedDataKeyString = $encryptedDataKey; 

       if(!self::existsFile($keyFilePath)) { 
        file_put_contents($keyFilePath, $encryptedDataKeyString); 
       } 

       if(!self::existsFile($encryptedFilePath)) { 
        file_put_contents($encryptedFilePath, $encryptedDataString); 
       } 

       return 'OK'; 

      } else { 
       return 'Source file not exists'; 
      } 

     } else { 
      return 'Encrypted data already exists'; 
     } 
    } 

    public static function decryptFile($encryptedFilePath, $keyFilePath, $decryptedFilePath) 
    { 

     $keyFileData = self::existsFile($keyFilePath); 
     $encryptedFileData = self::existsFile($encryptedFilePath); 
     $encryptedFileDataLength = strlen($encryptedFileData) - 1; 

     if($encryptedFileData && $keyFileData) { 

      $encryptedFileDataArray = str_split($encryptedFileData); 
      $keyFileDataArray = explode(';', $keyFileData); 

      $decryptedData = NULL; 
      for ($i = 0; $i <= $encryptedFileDataLength; $i++) { 

       $poziciaaktualneho = array_search($encryptedFileDataArray[$i], self::$charSet); 
       $poziciasifrovana = $keyFileDataArray[$i]; 
       if($poziciasifrovana < 0) { 
        $move = $poziciasifrovana + $poziciaaktualneho; 
       } elseif($poziciasifrovana > 0) { 
        $move = $poziciasifrovana - $poziciaaktualneho; 
       } else { 
        $move = '0'; 
       } 
       $decryptedData .= self::$charSet[$move]; 

      } 

      if(!self::existsFile($decryptedFilePath)) { 
       file_put_contents($decryptedFilePath, base64_decode($decryptedData)); 
       return 'OK'; 
      } else { 
       return 'Decrypted data already exists'; 
      } 

     } 

    } 

    private static function existsFile($filePath) 
    { 
     $fileData = @file_get_contents($filePath); 
     if($fileData) { 
      return $fileData; 
     } 
     return FALSE; 
    } 

} 


$originalFilePath = 'original.jpg'; 
$keyFilePath = 'Otp_Key_' . $originalFilePath; 
$encryptedFilePath = 'Otp_Data_' . $originalFilePath; 
$decryptedFilePath = 'Otp_Decrypted_' . $originalFilePath; 

echo Otp::encryptFile($originalFilePath, $encryptedFilePath, $keyFilePath); 
echo Otp::decryptFile($encryptedFilePath, $keyFilePath, $decryptedFilePath); 
+0

ようこそ!現在のコードと問題が発生している場所を表示して、問題の原因を突き止めることができますか? – brimstone

+0

はすでにここにあります。すみません、私はここで新しいです。 – tomdawayhet

+0

OTPではワンタイムパスワードを意味しますか? –

答えて

0

問題は、ライン78上の文はこれをチェックし、代わりに私は問題を解決することができました$poziciasifrovanaに等しい$moveを設定する場合$poziciaaktualnehoは別のものを追加することで$poziciasifrovanaなどに等しい場合にのみ起こっているようです。以下のスクリプトは動作するはずです:

final class Otp 
{ 

    private static $charSet = array('+','/','0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F','G','H','I','J','K','L', 
    'M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r', 
    's','t','u','v','w','x','y','z'); 

    public static function encryptFile($originalFilePath, $encryptedFilePath, $keyFilePath) 
    { 

     if(!self::existsFile($keyFilePath) || !self::existsFile($encryptedFilePath)) { 

      if($originalFileData = self::existsFile($originalFilePath)) { 

       $originalFileBase64Data = base64_encode($originalFileData); 
       $originalFileBase64DataLength = strlen($originalFileBase64Data) - 1; 
       $originalFileBase64DataArray = str_split($originalFileBase64Data); 

       $encryptedData = NULL; 
       $encryptedDataKey = NULL; 
       for ($i = 0; $i <= $originalFileBase64DataLength; $i++) { 

        $randKey = rand(0, sizeOf(self::$charSet) - 1); 
        $arrayKey = array_search($originalFileBase64DataArray[$i], self::$charSet); 

        if($randKey > $arrayKey) { 
         $str = '-' . ($randKey - $arrayKey); 
        } elseif($randKey < $arrayKey) { 
         $str = ($randKey + $arrayKey); 
        } else { 
         $str = $randKey; 
        } 

        $encryptedData .= self::$charSet[$randKey]; 
        $encryptedDataKey .= $str. ';'; 

       } 

       $encryptedDataString = $encryptedData; 
       $encryptedDataKeyString = $encryptedDataKey; 

       if(!self::existsFile($keyFilePath)) { 
        file_put_contents($keyFilePath, $encryptedDataKeyString); 
       } 

       if(!self::existsFile($encryptedFilePath)) { 
        file_put_contents($encryptedFilePath, $encryptedDataString); 
       } 

       return 'OK'; 

      } else { 
       return 'Source file not exists'; 
      } 

     } else { 
      return 'Encrypted data already exists'; 
     } 
    } 

    public static function decryptFile($encryptedFilePath, $keyFilePath, $decryptedFilePath) 
    { 

     $keyFileData = self::existsFile($keyFilePath); 
     $encryptedFileData = self::existsFile($encryptedFilePath); 
     $encryptedFileDataLength = strlen($encryptedFileData) - 1; 

     if($encryptedFileData && $keyFileData) { 

      $encryptedFileDataArray = str_split($encryptedFileData); 
      $keyFileDataArray = explode(';', $keyFileData); 

      $decryptedData = NULL; 
      for ($i = 0; $i <= $encryptedFileDataLength; $i++) { 

       $poziciaaktualneho = array_search($encryptedFileDataArray[$i], self::$charSet); 
       $poziciasifrovana = $keyFileDataArray[$i]; 
       if ($poziciasifrovana == $poziciaaktualneho) { 
        $move = $poziciasifrovana; 
       } elseif($poziciasifrovana < 0) { 
        $move = $poziciasifrovana + $poziciaaktualneho; 
       } elseif($poziciasifrovana > 0) { 
        $move = $poziciasifrovana - $poziciaaktualneho; 
       } else { 
        $move = '0'; 
       } 
       $decryptedData .= self::$charSet[$move]; 

      } 

      if(!self::existsFile($decryptedFilePath)) { 
       file_put_contents($decryptedFilePath, base64_decode($decryptedData)); 
       return 'OK'; 
      } else { 
       return 'Decrypted data already exists'; 
      } 

     } 

    } 

    private static function existsFile($filePath) 
    { 
     $fileData = @file_get_contents($filePath); 
     if($fileData) { 
      return $fileData; 
     } 
     return FALSE; 
    } 

} 


$originalFilePath = 'original.jpg'; 
$keyFilePath = 'Otp_Key_' . $originalFilePath; 
$encryptedFilePath = 'Otp_Data_' . $originalFilePath; 
$decryptedFilePath = 'Otp_Decrypted_' . $originalFilePath; 

echo Otp::encryptFile($originalFilePath, $encryptedFilePath, $keyFilePath); 
echo Otp::decryptFile($encryptedFilePath, $keyFilePath, $decryptedFilePath); 

警告:私はこれがあなたのスクリプトを修正するか、何と もともと間違っていた理由を私は知らないので、すべての場合は、エンタープライズ環境での私のソリューションを使用することはお勧めしませんそれは気密ではない可能性が最も高いです。

+0

多くのありがとうございます。それは素晴らしいです。私は今それをテストし、それは完璧に見える:) – tomdawayhet

関連する問題