Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/253.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类转换为完整的静态PHP类_Php_Security_Encryption_Passwords - Fatal编程技术网

将此加密-解密PHP类转换为完整的静态PHP类

将此加密-解密PHP类转换为完整的静态PHP类,php,security,encryption,passwords,Php,Security,Encryption,Passwords,下面是一个很棒的php类,可以在php中实现安全的双向加密。我在一个需要对其方法进行数千次调用的项目中使用它 为了提高性能,我想将它转换成一个完整的静态类及其所有方法,并在每次新方法调用时避免新实例 /** * A class to handle secure encryption and decryption of arbitrary data * * Note that this is not just straight encryption. It also has a few o

下面是一个很棒的php类,可以在php中实现安全的双向加密。我在一个需要对其方法进行数千次调用的项目中使用它

为了提高性能,我想将它转换成一个完整的静态类及其所有方法,并在每次新方法调用时避免新实例

/**
 * A class to handle secure encryption and decryption of arbitrary data
 *
 * Note that this is not just straight encryption.  It also has a few other
 *  features in it to make the encrypted data far more secure.  Note that any
 *  other implementations used to decrypt data will have to do the same exact
 *  operations.  
 *
 * Security Benefits:
 *
 * - Uses Key stretching
 * - Hides the Initialization Vector
 * - Does HMAC verification of source data
 *
 */
class Encryption {

    /**
     * @var string $cipher The mcrypt cipher to use for this instance
     */
    protected $cipher = '';

    /**
     * @var int $mode The mcrypt cipher mode to use
     */
    protected $mode = '';

    /**
     * @var int $rounds The number of rounds to feed into PBKDF2 for key generation
     */
    protected $rounds = 100;

    /**
     * Constructor!
     *
     * @param string $cipher The MCRYPT_* cypher to use for this instance
     * @param int    $mode   The MCRYPT_MODE_* mode to use for this instance
     * @param int    $rounds The number of PBKDF2 rounds to do on the key
     */
    public function __construct($cipher, $mode, $rounds = 100) {
        $this->cipher = $cipher;
        $this->mode = $mode;
        $this->rounds = (int) $rounds;
    }

    /**
     * Decrypt the data with the provided key
     *
     * @param string $data The encrypted datat to decrypt
     * @param string $key  The key to use for decryption
     * 
     * @returns string|false The returned string if decryption is successful
     *                           false if it is not
     */
    public function decrypt($data, $key) {
        $salt = substr($data, 0, 128);
        $enc = substr($data, 128, -64);
        $mac = substr($data, -64);

        list ($cipherKey, $macKey, $iv) = $this->getKeys($salt, $key);

        if ($mac !== hash_hmac('sha512', $enc, $macKey, true)) {
             return false;
        }

        $dec = mcrypt_decrypt($this->cipher, $cipherKey, $enc, $this->mode, $iv);

        $data = $this->unpad($dec);

        return $data;
    }

    /**
     * Encrypt the supplied data using the supplied key
     * 
     * @param string $data The data to encrypt
     * @param string $key  The key to encrypt with
     *
     * @returns string The encrypted data
     */
    public function encrypt($data, $key) {
        $salt = mcrypt_create_iv(128, MCRYPT_DEV_URANDOM);
        list ($cipherKey, $macKey, $iv) = $this->getKeys($salt, $key);

        $data = $this->pad($data);

        $enc = mcrypt_encrypt($this->cipher, $cipherKey, $data, $this->mode, $iv);

        $mac = hash_hmac('sha512', $enc, $macKey, true);
        return $salt . $enc . $mac;
    }

    /**
     * Generates a set of keys given a random salt and a master key
     *
     * @param string $salt A random string to change the keys each encryption
     * @param string $key  The supplied key to encrypt with
     *
     * @returns array An array of keys (a cipher key, a mac key, and a IV)
     */
    protected function getKeys($salt, $key) {
        $ivSize = mcrypt_get_iv_size($this->cipher, $this->mode);
        $keySize = mcrypt_get_key_size($this->cipher, $this->mode);
        $length = 2 * $keySize + $ivSize;

        $key = $this->pbkdf2('sha512', $key, $salt, $this->rounds, $length);

        $cipherKey = substr($key, 0, $keySize);
        $macKey = substr($key, $keySize, $keySize);
        $iv = substr($key, 2 * $keySize);
        return array($cipherKey, $macKey, $iv);
    }

    /**
     * Stretch the key using the PBKDF2 algorithm
     *
     * @see http://en.wikipedia.org/wiki/PBKDF2
     *
     * @param string $algo   The algorithm to use
     * @param string $key    The key to stretch
     * @param string $salt   A random salt
     * @param int    $rounds The number of rounds to derive
     * @param int    $length The length of the output key
     *
     * @returns string The derived key.
     */
    protected function pbkdf2($algo, $key, $salt, $rounds, $length) {
        $size   = strlen(hash($algo, '', true));
        $len    = ceil($length / $size);
        $result = '';
        for ($i = 1; $i <= $len; $i++) {
            $tmp = hash_hmac($algo, $salt . pack('N', $i), $key, true);
            $res = $tmp;
            for ($j = 1; $j < $rounds; $j++) {
                 $tmp  = hash_hmac($algo, $tmp, $key, true);
                 $res ^= $tmp;
            }
            $result .= $res;
        }
        return substr($result, 0, $length);
    }

    protected function pad($data) {
        $length = mcrypt_get_block_size($this->cipher, $this->mode);
        $padAmount = $length - strlen($data) % $length;
        if ($padAmount == 0) {
            $padAmount = $length;
        }
        return $data . str_repeat(chr($padAmount), $padAmount);
    }

    protected function unpad($data) {
        $length = mcrypt_get_block_size($this->cipher, $this->mode);
        $last = ord($data[strlen($data) - 1]);
        if ($last > $length) return false;
        if (substr($data, -1 * $last) !== str_repeat(chr($last), $last)) {
            return false;
        }
        return substr($data, 0, -1 * $last);
    }
}
我想要像这样的东西

Encryption::encrypt($data, $key);
Encryption::decrypt($encryptedData, $key);

谢谢。

如果要查找静态方法,只需在方法声明中的
public
protected
之前添加
static
关键字即可

但是,请注意,您不能从静态方法调用非静态方法,因此您必须将所有实用工具方法声明为静态方法


有关更多信息,请参阅。

如果要查找静态方法,只需在方法声明中的
public
protected
之前添加
static
关键字即可

但是,请注意,您不能从静态方法调用非静态方法,因此您必须将所有实用工具方法声明为静态方法


有关更多信息,请参阅。

我将介绍一些解决方案和想法,让您朝着正确的方向前进。但是,我非常担心您的代码,必须首先要求您不要在实时环境中使用此代码

与许多密码学研究人员会告诉你“不要写密码”并就此罢休的不同,我选择了这条路

您询问Stack Overflow如何将类转换为使用静态方法,这一事实告诉我您可能还没有资格用PHP编写加密。即使你更熟悉这门语言,我也会强烈提醒你不要朝着你要去的方向走

请改用。我已经看过了。其他人也是如此。它还没有经过正式审核,但这更多地是由于作者没有投入数千美元进行正式审核,而不是缺乏对安全和高质量代码的投入

以下是我一眼就发现的一些对加密库来说是危险的东西。这并不意味着攻击;我只想向你们展示一些在这片土地上出现的危险

构造函数中没有健全性检查 您如何知道
$this->cipher
$this->mode
是有效的mcrypt常量

浪费熵
$salt=mcrypt\u create\u iv(128,mcrypt\u DEV\u uradom)

128字节的熵是1024位的熵;远远超过您对AES的需求

使用
substr()
被认为对加密有害 PHP有一个名为
mbstring.func_重载
的功能,它完全改变了
substr()
strlen()、
等函数的行为,以使用多字节(Unicode)逻辑而不是二进制字符串逻辑进行操作。这种行为的典型后果是导致生成的子字符串中的字节数不正确。这很糟糕。坏的程度取决于攻击者的创造力

在启用此功能的系统中,必须显式调用
mb_substr($string,'8bit')
以获取原始二进制字符串中的字节数。但是,并非所有系统都提供此功能

使用
substr()
随意分割原始二进制字符串是危险的

MAC验证中的定时攻击
if($mac!==hash_hmac($sha512',$enc,$macKey,true))

总之 我真诚地希望,这篇文章是以建设性的批评的口吻收到的,你会对密码工程中的大量疯狂细节有所启发

此外,我希望这不会使你对该领域的更多了解感到厌烦请了解更多信息这是一个令人兴奋的领域,如果甲板上有更多的人手,每个人都会过得更好!也许有一天你甚至可以教我一个我永远也想不到的复杂攻击


但如今,在生产系统中使用业余加密技术来保护敏感数据是危险的。你有责任自己(或你的客户/雇主,如果适用的话)决定什么更重要:实验,或确保他们的信息安全不被泄露。我不会为您做出选择。

我将介绍一些解决方案和想法,让您朝着正确的方向前进。但是,我非常担心您的代码,必须首先要求您不要在实时环境中使用此代码

与许多密码学研究人员会告诉你“不要写密码”并就此罢休的不同,我选择了这条路

您询问Stack Overflow如何将类转换为使用静态方法,这一事实告诉我您可能还没有资格用PHP编写加密。即使你更熟悉这门语言,我也会强烈提醒你不要朝着你要去的方向走

请改用。我已经看过了。其他人也是如此。它还没有经过正式审核,但这更多地是由于作者没有投入数千美元进行正式审核,而不是缺乏对安全和高质量代码的投入

以下是我一眼就发现的一些对加密库来说是危险的东西。这并不意味着攻击;我只想向你们展示一些在这片土地上出现的危险

构造函数中没有健全性检查 您如何知道
$this->cipher
$this->mode
是有效的mcrypt常量

浪费熵
$salt=mcrypt\u create\u iv(128,mcrypt\u DEV\u uradom)

128字节的熵是1024位的熵;远远超过您对AES的需求

使用
substr()
被认为对加密有害 PHP有一个名为
mbstring.func_重载的特性,它完全改变了函数的行为,如
substr()

Encryption::encrypt($data, $key);
Encryption::decrypt($encryptedData, $key);