Simplest two-way encryption using PHP

前端 未结 6 1532
半阙折子戏
半阙折子戏 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

    Encrypting using openssl_encrypt() The openssl_encrypt function provides a secured and easy way to encrypt your data.

    In the script below, we use the AES128 encryption method, but you may consider other kind of encryption method depending on what you want to encrypt.

    
    

    Here is an explanation of the variables used :

    message_to_encrypt : the data you want to encrypt secret_key : it is your ‘password’ for encryption. Be sure not to choose something too easy and be careful not to share your secret key with other people method : the method of encryption. Here we chose AES128. iv_length and iv : prepare the encryption using bytes encrypted_message : the variable including your encrypted message

    Decrypting using openssl_decrypt() Now you encrypted your data, you may need to decrypt it in order to re-use the message you first included into a variable. In order to do so, we will use the function openssl_decrypt().

    
    

    The decrypt method proposed by openssl_decrypt() is close to openssl_encrypt().

    The only difference is that instead of adding $message_to_encrypt, you will need to add your already encrypted message as the first argument of openssl_decrypt().

    That is all you have to do.

提交回复
热议问题