262

What is the simplest way of doing two way encryption in common PHP installs?

I need to be able to encrypt data with a string key, and use the same key to decrypt on the other end.

The security isn't as big of a concern as the portability of the code, so I'd like to be able to keep things as simple as possible. Currently, I am using an RC4 implementation, but if I can find something natively supported I figure I can save a lot of unnecessary code.

4

6 に答える 6

207

編集:

本当にopenssl_encrypt()openssl_decrypt( )を使用する必要があります

Scottが言うように、Mcrypt は 2007 年以来更新されていないため、良い考えではありません。

Mcrypt を PHP から削除する RFC もあります - https://wiki.php.net/rfc/mcrypt-viking-funeral

于 2012-02-13T14:26:22.083 に答える
22

Use mcrypt_encrypt() and mcrypt_decrypt() with corresponding parameters. Really easy and straight forward, and you use a battle-tested encryption package.

EDIT

5 years and 4 months after this answer, the mcrypt extension is now in the process of deprecation and eventual removal from PHP.

于 2012-02-13T14:25:29.673 に答える
3

PHP 7.2は完全に廃止され、暗号化は保守可能なライブラリMcryptに基づいています。Libsodium

暗号化のすべてのニーズは、基本的にLibsodiumライブラリを介して解決できます。

// On Alice's computer:
$msg = 'This comes from Alice.';
$signed_msg = sodium_crypto_sign($msg, $secret_sign_key);


// On Bob's computer:
$original_msg = sodium_crypto_sign_open($signed_msg, $alice_sign_publickey);
if ($original_msg === false) {
    throw new Exception('Invalid signature');
} else {
    echo $original_msg; // Displays "This comes from Alice."
}

Libsodium ドキュメント: https://github.com/paragonie/pecl-libsodium-doc

于 2018-06-04T19:10:50.623 に答える
1

重要: この回答は PHP 5 でのみ有効です。PHP 7 では、組み込みの暗号化関数を使用します。

シンプルですが、十分に安全な実装を次に示します。

  • CBC モードでの AES-256 暗号化
  • プレーンテキストのパスワードから暗号化キーを作成する PBKDF2
  • 暗号化されたメッセージを認証するための HMAC。

コードと例はこちら: https://stackoverflow.com/a/19445173/1387163

于 2015-06-23T08:33:35.317 に答える