How to encrypt/decrypt data in php?

前端 未结 6 776
北恋
北恋 2020-11-22 07:17

I\'m currently a student and I\'m studying PHP, I\'m trying to make a simple encrypt/decrypt of data in PHP. I made some online research and some of them were quite confusin

6条回答
  •  盖世英雄少女心
    2020-11-22 07:22

    Here is an example using openssl_encrypt

    //Encryption:
    $textToEncrypt = "My Text to Encrypt";
    $encryptionMethod = "AES-256-CBC";
    $secretHash = "encryptionhash";
    $iv = mcrypt_create_iv(16, MCRYPT_RAND);
    $encryptedText = openssl_encrypt($textToEncrypt,$encryptionMethod,$secretHash, 0, $iv);
    
    //Decryption:
    $decryptedText = openssl_decrypt($encryptedText, $encryptionMethod, $secretHash, 0, $iv);
    print "My Decrypted Text: ". $decryptedText;
    

提交回复
热议问题