php - 如何在 CodeIgniter 中使用 salt 正确实现加密库?
问题描述
再会。
使用 CodeIgniter 中的加密库,我可以使用加密任何字符串$this->encryption->encrypt('string');
,这是加密字符串的最简单方法。但是,这种方法并不是我认为保护数据的最安全方法,因此我决定使用 salt 来更好地加密。我阅读了 Codeigniter 提供的有关加密库的文档,但我并不真正了解加密和盐的真正工作原理。
这是我的加密代码。
<?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()
方法的参数和提供的示例。
此外,return
关键字with array data
in encryption_config()
function是encrypt()
加密库中方法的第二个参数。我使用encryption->hkdf()
它是因为它的参数盐。我是 Codeigniter 中使用盐进行加密的新手,所以我真的很想知道如何实现这种加密。所以我所做的是上面的代码确实适用于加密和解密,但由于某种原因,我真的不明白为什么返回值与普通加密不同。不同的是,这种加密方法$this->encryption->encrypt("some string");
返回但使用上面的代码返回。
虽然我可以解密该符号字符,但这不会将此加密数据保存到数据类型为 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;
}
}
推荐阅读
- newline - 如何在 LibreOffice Calc 中用“新行”替换字符?
- javascript - 如何分配函数作为参数的变量
- python - Getting ValueError: too many values to unpack (expected 3) Using Python 3.6.3 and Open CV
- mips - 为什么这个加载指令会在跳转之后出现?
- c# - C# Form 在运行时添加控件
- android - 匕首:无法提供地图
没有 @Provides 注释的方法 - typescript - 是否可以将方法添加到 Iterable
TypeScript 中的接口? - javascript - 将状态保留在子级并更新父级
- python - Python3 Google App灵活引擎中的Numpy失败
- node.js - 节点ssh2客户端中的重复数据输出