如何在 CodeIgniter 中使用 salt 正确实现加密库?
how to implement encryption library properly with salt in CodeIgniter?
美好的一天。
使用 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->CI->encryption->hkdf()
来创建 hmac key
,正如文档示例所说的那样。为了清楚起见,这里是 hkdf()
方法的参数和提供的示例。
此外,encryption_config()
函数中array data
关键字return
是加密库中encrypt()
方法的第二个参数。我使用 encryption->hkdf()
因为它上面有参数盐。我是 Codeigniter 中使用盐进行加密的新手,所以我真的在为如何实现这种加密而苦苦挣扎。所以我所做的是,上面的代码确实适用于加密和解密,但出于某种原因,我真的不明白为什么 return 值与普通加密不同。不同的是,这种加密方式$this->encryption->encrypt("some string");
returns but using the code above returns .
虽然我可以解密那个符号字符,但是这不会将这个加密数据以varchar数据类型保存到数据库中,而是将其保存为普通字符或字符串。这是保存到数据库 .
的数据
我的问题是,我做得对吗?如果不是,用盐实现这个库的正确方法是什么?我希望加密数据作为普通文本而不是符号字符,我可以实现这个目标吗?最后?如果字符串是否加密,是否有检查字符串的方法?请帮我。我只花了几天时间解决这个问题。我看了与加密相关的 youtube 教程,但运气不好。
好的。在互联网上搜索后,我找到了不使用此 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;
}
}
美好的一天。
使用 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->CI->encryption->hkdf()
来创建 hmac key
,正如文档示例所说的那样。为了清楚起见,这里是 hkdf()
方法的参数和提供的示例。
此外,encryption_config()
函数中array data
关键字return
是加密库中encrypt()
方法的第二个参数。我使用 encryption->hkdf()
因为它上面有参数盐。我是 Codeigniter 中使用盐进行加密的新手,所以我真的在为如何实现这种加密而苦苦挣扎。所以我所做的是,上面的代码确实适用于加密和解密,但出于某种原因,我真的不明白为什么 return 值与普通加密不同。不同的是,这种加密方式$this->encryption->encrypt("some string");
returns
虽然我可以解密那个符号字符,但是这不会将这个加密数据以varchar数据类型保存到数据库中,而是将其保存为普通字符或字符串。这是保存到数据库
我的问题是,我做得对吗?如果不是,用盐实现这个库的正确方法是什么?我希望加密数据作为普通文本而不是符号字符,我可以实现这个目标吗?最后?如果字符串是否加密,是否有检查字符串的方法?请帮我。我只花了几天时间解决这个问题。我看了与加密相关的 youtube 教程,但运气不好。
好的。在互联网上搜索后,我找到了不使用此 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;
}
}