Simplest two-way encryption using PHP

前端 未结 6 1530
半阙折子戏
半阙折子戏 2020-11-22 07:11

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

6条回答
  •  情话喂你
    2020-11-22 07:26

    PHP 7.2 moved completely away from Mcrypt and the encryption now is based on the maintainable Libsodium library.

    All your encryption needs can be basically resolved through Libsodium library.

    // 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 documentation: https://github.com/paragonie/pecl-libsodium-doc

提交回复
热议问题