Encrypt/Secure DB connection string in php

前端 未结 3 1829
日久生厌
日久生厌 2021-01-07 10:10

one of the project required me to encrypt DB connection string (username & password).

Usually I use .htaccess to restrict user to access from web. But this pro

3条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-07 10:23

    if the extension is available on your server you can use mcrypt Store your username and password crypted in a file outside the web root and read/write it with mcrypt

    For exemple :

    $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
    $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
    $key = "This is a very secret key";
    $text = "myusername";
    echo $text . "\n";
    
    $crypttext = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $text, MCRYPT_MODE_ECB, $iv);
    echo $crypttext . "\n";
    

    End of course mcrypt_decrypt let you get the original string.

提交回复
热议问题