PHP加密

WBOY
发布: 2024-08-29 13:02:19
原创
1076 人浏览过

PHP 加密只不过是借助一些算法来实现加密代码,这些算法有时称为哈希算法,它们通常通过获取字符串或其他一些输入来工作,然后它将有助于从字符串创建唯一的指纹/其他。加密涉及将特定文本/其他文本更改为某种不同的代码文本或其他文本,只是为了使数据安全,而不暴露给大多数人(除了一些有权访问的人)。查看下面不同类型的 PHP 加密方法。

广告 该类别中的热门课程 PHP 开发人员 - 专业化 | 8 门课程系列 | 3次模拟测试

开始您的免费软件开发课程

网络开发、编程语言、软件测试及其他

PHP 加密类型

目前使用的加密方法有很多种,但最常见的是散列法,第二种是密钥加密法,第三种是信封加密法。对于每种加密方法,都有多种算法或加密可供选择(每种加密方法都有自己的弱点和优点)。这里我们将重点关注散列和密钥加密的实现。

1.哈希

PHP 编程语言的哈希算法通常采用一个输入值,然后将其转换为一个消息摘要。简而言之,纯文本值将被转换为固定长度的哈希值,并且只能绕过一个原始值到唯一的哈希算法来验证它。这将使哈希完美地存储所有用户密码。

值得注意的是,哈希对于我们的查询来说根本不是万无一失的解决方案,但有些查询不会拥有所有相同的哈希算法。我们考虑了高效且快速的 MD5 和 SHA1 算法,然后使所有这些成为文件验证和校验和的理想选择。它们的速度使得它们中的大多数不适合对用户密码进行哈希处理。凭借现代 GPU 的计算能力,只需在几分钟内使用暴力破解,只需揭示原始明文密码和故意使用较慢的哈希算法(例如 bcrypt)即可破解密码。或将使用Argon2。

对用算法生成的密码进行哈希处理,那么它肯定会掩盖实际和原始数据,然后会减慢攻击者的速度,而开发人员应该尝试一种可用的最强算法。 PHP 语言有一个基本语法,即password_hash()。

这是使用哈希技术“bcrypt”的示例。

代码:

<?php
$str1 = 'Password';
$options1 = [
'cost1' => 10,
'salt1' => '$P27r06o9!nasda57b2M22'
];
echo sprintf("The Result of crypt() function on %s is %s\n",
$str1, crypt($str1, $options1['salt1']));
echo "<br>";
echo sprintf("The Result of DEFAULT function on %s is %s\n",
$str1, password_hash($str1, PASSWORD_DEFAULT));
echo "<br>";
echo sprintf("The Result of BCRYPT function on %s is %s\n", $str1,
password_hash($str1, PASSWORD_BCRYPT, $options1));
echo "<br>";
?>
登录后复制

输出:

PHP加密

2.密钥加密

PHP 的密钥加密通常使用一个密钥来加密和解密数据。它也称为对称加密。为此,如果您运行的是旧版本的 PHP 编程语言,请通过 PECL 安装 PHP 编程语言的钠。

首先,您需要使用 random_bytes() 函数代码实际生成的加密密钥。但通常,您只需执行一次并将其存储为环境变量。这是我们必须保守秘密的关键。如果密钥已知,那么加密的内容也会被泄露。

$secret_key = random_bytes (SODIUM_CRYPTO_SECRETBOX_KEYBYTES);
登录后复制

为了使用我们的密钥/秘密密钥和 $nonce 加密将要传递给odium_crypto_secretbox() 函数的值。然后使用 random_bytes() 函数生成随机数变量,这只是因为不能使用相同/相似的随机数。

$nonce = random_bytes(SODIUM_CRYPTO_NONCEBYTES);
$ciphertext = sodium_crypto_secretbox(‘This is secret key!!!’, $nonce, $secret_key);
登录后复制

这将代表问题,因为我们需要那个随机数,它将稍后/之后解密该值。但是 NONCE/NONCES 没有任何密钥可以保密,因此我们可以轻松地将其作为 $ciphertext 前置,然后 base64_encode() 函数是将其保存到实际数据库之前发生的值。

$encoded1 = base64_encode($nonce . $ciphertext);
Var_dump($encoded1);
登录后复制

以上语法仅用于加密值。为了解码/解密编码值,请使用以下语法。

$decoded1 = base64_decode($encoded1);
登录后复制

在解密值之前,将使用 mb_substr() 函数提取随机数变量或函数的长度。

这是实现 random_bytes() 函数的示例,以了解密钥加密。这里随机字符串的长度将借助二进制十六进制进行转换,以确保字符串中的随机字节。

Code:

<?php
$length1 = random_bytes('3');
var_dump(bin2hex($length1));
?>
登录后复制

Output:

PHP加密

3. Envelope Encryption

We all know that our data is vulnerable if our secret key is known/compromised. Just consider that a malicious user/attacker got access to the server point which is actually hosting our application/other. In this situation, attacker most of the times gets/discovers out the secret key which should be kept as secret. If it is discovered then our data may be in danger of exposing to the malicious user/attacker.

We can use Cloud KMS service which is actually provided by the Google Cloud Key Management Service. It provided a wide variety of so many useful features. It includes automatic key rotation and also the delayed key destruction capability.

Before sending plaintext to the CLOUD KMS, generate a unique encryption key each and every time when we actually write the data to the database. This key is called DEK (Data Encryption Key) which is actually used in encrypting the data. DEK which is sent to the Google Cloud KMS will be encrypted and then it will return the KEK (key encryption key). At last, KEK will be stored side by side in the actual database point which is next to the encrypted data and then the DEK will be destroyed.

Check out the data encryption and decryption as below:

Encryption process
  • It will generate unique DEK – Data Encryption Key
  • Using the SKE (Secret Key Encryption) the data will be encrypted
  • Now the DEK – Data Encryption Key will be sent to the Cloud KMS for encryption purpose which actually returns the KEY – Key Encryption Key.
  • Then the encrypted data will be stored and KEK side by side
  • Then the destruction of the DEK will be done
Decryption Process
  • Now at first, from the database encrypted and KEK (Key Encrypted Key) will be retrieved.
  • Then send KEK for the Cloud KMD for decryption which actually returns the DEK – Data Encryption Key
  • Now the DEK – Data Encryption Key will be used to decrypt the encrypted data.
  • Now DEK – Data Encryption Key will be destroyed.

Conclusion

I hope you learned what is the definition of PHP Encryption and along with the various types of PHP Encryption methods along with the examples by mentioning their brief description. An example is shown here for envelope encryption because it is cloud-based encryption.

以上是PHP加密的详细内容。更多信息请关注PHP中文网其他相关文章!

相关标签:
php
来源:php
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!