PHP暗号化

WBOY
リリース: 2024-08-29 13:02:19
オリジナル
1112 人が閲覧しました

PHP 暗号化は、ハッシュ アルゴリズムとも呼ばれるいくつかのアルゴリズムを利用して暗号化コードを実現することに他なりません。これらのアルゴリズムは通常、文字列またはその他の入力を取得することで機能し、文字列から一意のフィンガープリントを作成するのに役立ちます。 /他の。暗号化には、特定のテキストやその他のテキストを別のコード テキストなどに変更することが含まれ、アクセス権のある一部の人を除いてほとんどの人に公開されることなくデータを安全にします。以下のさまざまな種類の PHP 暗号化方式を確認してください。

広告 このカテゴリーの人気コース PHP 開発者 - 専門分野 | 8コースシリーズ | 3 つの模擬テスト

無料ソフトウェア開発コースを始めましょう

Web 開発、プログラミング言語、ソフトウェア テスト、その他

PHP 暗号化の種類

現在使用されている暗号化方式にはさまざまな種類がありますが、一般的にはハッシュによるもので、2 番目は秘密キー暗号化、3 番目はエンベロープ暗号化方式です。それぞれの暗号化方式には、複数のアルゴリズムまたは暗号化があり、それぞれから選択できます (それぞれに独自の弱点と独自の強みがあります)。ここでは、ハッシュ化と秘密鍵暗号化の実装に焦点を当てます。

1.ハッシュ

PHP プログラミング言語のハッシュ アルゴリズムは通常、1 つの入力値を受け取り、それを 1 つのメッセージ ダイジェストに変換します。一言で言えば、プレーン テキストの値は固定長のハッシュに変換され、元の 1 つの値を独自のハッシュ アルゴリズムにバイパスするだけで検証できます。これにより、すべてのユーザーのパスワードを保存するためだけにハッシュ化が完璧になります。

ハッシュはクエリに対する完全な解決策ではありませんが、一部のハッシュ アルゴリズムは同じではないことに注意してください。私たちは、効率的で高速な MD5 および SHA1 アルゴリズムを検討し、それらすべてをファイル検証とチェックサムに最適なものにしました。それらの速度のせいで、ほとんどはユーザーのパスワードをハッシュするのには適していません。最新の GPU の計算能力を使えば、パスワードは総当たりの力を借りて数分で元の平文パスワードと意図的に bcrypt のような低速のハッシュ アルゴリズムを明らかにするだけで解読されます。または Argon2 が使用されます。

アルゴリズムで生成されたパスワードをハッシュすると、実際の元のデータが確実に隠蔽され、攻撃者の速度が低下しますが、開発者は利用可能な最強のアルゴリズムを 1 つ試す必要があります。 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 の秘密キー暗号化では、通常、データの暗号化と復号化の両方に 1 つの単一キーが使用されます。対称暗号化とも呼ばれます。このため、古いバージョンの PHP プログラミング言語を実行している場合は、PECL 経由で PHP プログラミング言語のナトリウムをインストールします。

最初に、random_bytes() 関数コードを使用するだけで実際に生成される暗号化キーが必要です。ただし、通常は 1 回だけ行う必要があり、それを環境変数として保存するだけです。これは私たちが秘密として守らなければならない鍵です。秘密鍵が知られている場合、暗号化された鍵も侵害されます。

$secret_key = random_bytes (SODIUM_CRYPTO_SECRETBOX_KEYBYTES);
ログイン後にコピー

sodium_crypto_secretbox() 関数に渡される値をキー/秘密鍵と $nonce で暗号化するため。次に、random_bytes() 関数を使用して nonce 変数が生成されますが、これは単に同じ/類似の nonce を使用できないためです。

$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()関数を実装する例です。ここでは、文字列内のランダムなバイトを保護するために、ランダムな文字列の長さがバイナリ 16 進数を使用して変換されます。

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 中国語 Web サイトの他の関連記事を参照してください。

関連ラベル:
php
ソース:php
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
最新の問題
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!