Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP Otp库中的base64_Php_Base64_One Time Password - Fatal编程技术网

PHP Otp库中的base64

PHP Otp库中的base64,php,base64,one-time-password,Php,Base64,One Time Password,我试图用OTP方法在PHP中创建一些简单的文件加密库。我的问题是,解密代码中的某些字符与原始字符不同。我花了将近一个星期的时间做这件事,但没有结果。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','

我试图用OTP方法在PHP中创建一些简单的文件加密库。我的问题是,解密代码中的某些字符与原始字符不同。我花了将近一个星期的时间做这件事,但没有结果。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);
最终类Otp
{
私有静态$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’;
公共静态函数encryptFile($originalFilePath、$encryptedFilePath、$keyFilePath)
{
如果(!self::existsFile($keyFilePath)| |!self::existsFile($encryptedFilePath)){
if($originalFileData=self::existsFile($originalFilePath)){
$originalFileBase64Data=base64_编码($originalFileData);
$originalFileBase64DataLength=strlen($originalFileBase64Data)-1;
$originalFileBase64DataArray=str_split($originalFileBase64Data);
$encryptedData=NULL;
$encryptedDataKey=NULL;
对于($i=0;$i$arrayKey){
$str='-'。($randKey-$arrayKey);
}elseif($randKey<$arrayKey){
$str=($randKey+$arrayKey);
}否则{
$str=$randKey;
}
$encryptedData.=self::$charSet[$randKey];
$encryptedDataKey.=$str.;';
}
$encryptedDataString=$encryptedData;
$encryptedDataKeyString=$encryptedDataKey;
如果(!self::existsFile($keyFilePath)){
文件内容($keyFilePath,$encryptedDataKeyString);
}
如果(!self::existsFile($encryptedFilePath)){
文件内容($encryptedFilePath,$encryptedDataString);
}
返回“OK”;
}否则{
返回“源文件不存在”;
}
}否则{
返回“加密数据已存在”;
}
}
公共静态函数decryptFile($encryptedFilePath、$keyFilePath、$decryptedFilePath)
{
$keyFileData=self::existsFile($keyFilePath);
$encryptedFileData=self::existsFile($encryptedFilePath);
$encryptedFileDataLength=strlen($encryptedFileData)-1;
if($encryptedFileData&&$keyFileData){
$encryptedFileDataArray=str_split($encryptedFileData);
$keyFileDataArray=分解(“;”,$keyFileData);
$decryptedData=NULL;
对于($i=0;$i 0){
$move=$poziciasifrovana-$poziciaaktualneho;
}否则{
$move='0';
}
$decryptedData.=self::$charSet[$move];
}
如果(!self::existsFile($decryptedFilePath)){
文件内容($decryptedFilePath,base64_decode($decryptedData));
返回“OK”;
}否则{
返回“已解密数据已存在”;
}
}
}
私有静态函数existsFile($filePath)
{
$fileData=@file\u get\u contents($filePath);
如果($fileData){
返回$fileData;
}
返回FALSE;
}
}
$originalFilePath='original.jpg';
$keyFilePath='Otp\U Key\U'$原始路径;
$encryptedFilePath='Otp\U数据\uU'$原始路径;
$decryptedFilePath='Otp\u Decrypted\u'$原始路径;
echo Otp::encryptFile($originalFilePath、$encryptedFilePath、$keyFilePath);
echo Otp::decryptFile($encryptedFilePath、$keyFilePath、$decryptedFilePath);

问题似乎只有在
$poziciaaktualneho
等于
$poziciasifrovana
时才会发生,因此通过在第78行添加另一条if语句来检查这一点,并将
$move
设置为
$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);
最终类Otp
{
私有静态$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’;
公共静态函数encryptFile($originalFilePath、$encryptedFilePath、$keyFilePath)
{
如果(!self::existsFile($keyFilePath)| |!self::existsFile($encryptedFilePath)){
if($originalFileData=self::existsFile($originalFilePath)){
$originalFileBase64Data=base64_编码($originalFileData);
$originalFileBase64DataLength=strlen($originalFileBase64Data)-1;
$originalFileBase64DataArray=str_split($originalFileBase64Data);
$encryptedData=NULL;
$encryptedDataKey=NULL;
对于($i=0;$i$arrayKey){
$str='-'。($randKey-$arrayKey);
}elseif($randKey<$arrayKey){
$str=($randKey+$arrayKey);
}否则{
$str=$randKey;
}
$encryptedData.=self::$charSet[$randKey];
$encryptedDataKey.=$str.;';
}
$encryptedDataString=$encryptedData;
$encryptedDataKeyString=$encryptedDataKey;
如果(!self::existsFile($keyFilePath)){
文件内容($keyFilePath,$encryptedDataKeyString);
}
如果(!self::existsFile($encryptedFilePath)){
文件内容($encryptedFilePath,$encryptedDataString)