【问题标题】:how to implement encryption library properly with salt in CodeIgniter?如何在 CodeIgniter 中使用 salt 正确实现加密库?
【发布时间】:2021-06-12 17:07:06
【问题描述】:

美好的一天。

使用 CodeIgniter 中的加密库,我可以使用 $this->encryption->encrypt('string'); 加密任何字符串,这是加密字符串的最简单方法。但是,我认为这种方法并不是保护数据的最安全方法,因此我决定使用 salt 进行更好的加密。我阅读了 Codeigniter 提供的关于 Encryption Library 的文档,但我并不真正了解加密和盐的真正工作原理。

这是我的加密代码。

<?php
defined('BASEPATH') or exit('No direct script access allowed');

class MyEncryption {
    //please do not change or delete this saltKey. "5948356750394856"
    private static $saltKey = "5948356750394856";
    // ================
    public $CI;
    function __construct()
    {
        $this->CI =& get_instance();
        $this->CI->load->library('encryption');
    }

    public function encryptStringWithSalt($string) {
        $p = $this->CI->encryption->encrypt(
            $string,
            $this->encryption_config()
        );

        return $p;
    }

    public function decryptEncryptedStringWithSalt($encryptedString) {
        return $this->CI->encryption->decrypt(
            $encryptedString,
            $this->encryption_config()
        );
    }

    private function encryption_config(){
        $key = $this->CI->config->item('encryption_key');

        $hmac_key = $this->CI->encryption->hkdf(
        $key,
        'sha512',
        MyEncryption::$saltKey,
        10,
        'authentication'
        );

        return array(
            'cipher' => 'aes-128',
            'mode' => 'CTR',
            'key' => MyEncryption::$saltKey,
            'hmac' => true,
            'hmac_digest' => 'sha224',
            'hmac_key' => $hmac_key
        );
    }

}

如我们所见,我创建了一个收集加密配置的函数。在该函数内部,我调用了方法$this-&gt;CI-&gt;encryption-&gt;hkdf() 来创建hmac key,正如文档示例所说。为了清楚起见,这里是hkdf() 方法的参数和提供的示例。

另外,encryption_config()函数中array datareturn关键字是加密库中encrypt()方法的第二个参数。我使用了encryption-&gt;hkdf(),因为它上面有参数salt。我是 Codeigniter 中使用盐加密的新手,所以我真的很想如何实现这种加密。所以我所做的是上面的代码确实适用于加密和解密,但由于某种原因,我真的不明白为什么返回值与普通加密不同。不同的是,这种加密方法$this-&gt;encryption-&gt;encrypt("some string");返回,但使用上面的代码返回

虽然我可以解密该符号字符,但这不会将此加密数据保存到数据类型为 varchar 的数据库中,而是将其保存为普通字符或字符串。这是保存到数据库的数据。

我的问题是,我做得对吗?如果没有,用盐实现这个库的正确方法是什么?我希望加密数据作为普通文本而不是符号字符,我可以实现那个目标吗?最后?如果字符串是否加密,是否有检查字符串?请帮我。我只花几天时间解决这个问题。我观看了与加密相关的 youtube 教程,但没有运气。

【问题讨论】:

    标签: php codeigniter encryption cryptography


    【解决方案1】:

    好的。在互联网上搜索后,我找到了一个不使用此 CI 加密库的解决方案。我通过使用这段代码实现了我的目标

    <?php 
    defined('BASEPATH') or exit('No direct script access allowed');
    /**
     * 
     */
    
    class SaltEncryption
    {
        public $CI;
    
        function __construct()
        {
            $this->CI =& get_instance();
        }
    
        
    public function encrypt($data){
            $password = "any string";
            $iv = substr(sha1(mt_rand()), 0, 16);
            $password = sha1($password);
    
            $salt = sha1(mt_rand());
            $saltWithPassword = hash('sha256', $password.$salt);
    
            $encrypted = openssl_encrypt(
              "$data", 'aes-256-cbc', "$saltWithPassword", null, $iv
            );
            $msg_encrypted_bundle = "$iv:$salt:$encrypted";
            return $msg_encrypted_bundle;
        }
    
    
    public function decrypt($msg_encrypted_bundle){
            $password = "any string";
            $password = sha1($password);
    
            $components = explode( ':', $msg_encrypted_bundle );
            if (
                count($components)=== 3 &&
                strlen($components[0]) === 16
            ) {
    
                $iv            = $components[0];
                $salt          = hash('sha256', $password.$components[1]);
                $encrypted_msg = $components[2];
    
                $decrypted_msg = openssl_decrypt(
                  $encrypted_msg, 'aes-256-cbc', $salt, null, $iv
                );
    
                if ( $decrypted_msg === false )
                    return false;
    
                $msg = substr( $decrypted_msg, 41 );
                return $decrypted_msg;
    
            }
            return false;
    
            
        }
    }
    

    【讨论】:

    • 我会让其他人指出这方面的技术问题,但我会说在不知道是否有好处的情况下获取随机的安全关键代码是个坏主意。那里有很多垃圾。
    猜你喜欢
    • 1970-01-01
    • 2013-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-09
    • 1970-01-01
    • 2020-09-05
    • 2016-12-21
    相关资源
    最近更新 更多