当前位置: 首页>>代码示例>>PHP>>正文


PHP Encryption::getMode方法代码示例

本文整理汇总了PHP中Encryption::getMode方法的典型用法代码示例。如果您正苦于以下问题:PHP Encryption::getMode方法的具体用法?PHP Encryption::getMode怎么用?PHP Encryption::getMode使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Encryption的用法示例。


在下文中一共展示了Encryption::getMode方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。

示例1: confidentialString

/**
 * Function to encrypt the sensitive data on its first run. For rest of the run, this function decrypts the encrypted data for use.
 * @return string       The string in plain-text
 * @throws FileNotWritable  Thrown when the file is not writable
 */
function confidentialString()
{
    $trace = debug_backtrace();
    //get the trace of this function call.
    //From this trace, find the proper sub-array which contains this function call. That call would be when the array's function parameter would contain this __FUNCTION__ value.
    $arraySlot = null;
    foreach ($trace as $count => $oncCall) {
        if ($oncCall['function'] == __FUNCTION__) {
            $arraySlot = $count;
            break;
        }
    }
    //If no value is passed to this function, then there is nothing to protect. Hence exit.
    if (count($trace[$arraySlot]['args']) == 0) {
        return "";
    }
    //Every encrypted string will contain ":" in the beginning. If this character is found in the string, then this is an encrypted string.
    if ($trace[$arraySlot]['args'][0][0] == ":") {
        $decodedString = substr($trace[$arraySlot]['args'][0], 1);
        //remove the ":" character form the string.
        $decodedString = base64_decode($decodedString);
        //the string was base64 encoded. Hence decode it back.
        $decryptedString = mcrypt_decrypt(Encryption::getCipher(), Encryption::getKey(), $decodedString, Encryption::getMode(), Encryption::getIV());
        //decrypt the string.
        return unserialize(rtrim($decryptedString, ""));
        //return the decrypted string.
    } else {
        $origString = $trace[$arraySlot]['args'][0];
        //store the original value.
        $encryptedString = mcrypt_encrypt(Encryption::getCipher(), Encryption::getKey(), serialize($origString), Encryption::getMode(), Encryption::getIV());
        //encrypt the value.
        $encryptedString = base64_encode($encryptedString);
        //base 64 encode it.
        $encryptedString = ":" . $encryptedString;
        //append ":" at the beginning of the encrypted string.
        $fileData = file($trace[$arraySlot]['file']);
        //get file contents as an array.
        $prevLine = $fileData[(int) $trace[$arraySlot]['line'] - 1];
        //get the line that needs to be replaced i.e. the string that contains the plain-text sensitive data.
        $functionName = str_replace(__NAMESPACE__ . "\\", '', __FUNCTION__);
        //calculate the function name of this function (without any namespace).
        $pos = strpos($prevLine, $functionName);
        //find the position of this function-name in the original string.
        $endPos = strpos($prevLine, ")", $pos);
        //search where this function ends, but start the search from the start of the function.
        $newLine = substr($prevLine, 0, $pos) . $functionName . "('{$encryptedString}')";
        //generate the new line i.e. with encrypted String.
        $fileData[(int) $trace[$arraySlot]['line'] - 1] = $newLine . substr($prevLine, $endPos + 1);
        //replace the old line with the new line.
        $fileData = implode("", $fileData);
        //get the data from the array.
        //check if file is writable or not.
        if (!is_writable($trace[$arraySlot]['file'])) {
            throw new FileNotWritable("ERROR: This file is not Writable!!");
        }
        //write this new data to file.
        $fp = fopen($trace[$arraySlot]['file'], 'w');
        fwrite($fp, $fileData);
        fclose($fp);
        //return the un-encrypted string for use.
        return $origString;
    }
}
开发者ID:sachintaware,项目名称:phpsec,代码行数:68,代码来源:confidentialstring.php


注:本文中的Encryption::getMode方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。