I want to connect to ssh via PHP. I can do that easily but the problem is the integrity of the login details (username, password). When I first tried the code I stored my username and password in plain sight. I thought about encrypting those variables. By looking at other questions I found libsodium
: https://github.com/jedisct1/libsodium-php
I am using their first example of encrypting a string:
$secret_key = sodium_crypto_secretbox_keygen();
$message = 'Sensitive information';
$nonce = random_bytes(SODIUM_CRYPTO_SECRETBOX_NONCEBYTES);
$encrypted_message = sodium_crypto_secretbox($message, $nonce, $secret_key);
Decryption:
$decrypted_message = sodium_crypto_secretbox_open($encrypted_message, $nonce, $secret_key);
By looking at the example you can see $encrypted_message
, $secret_key
, $nonce
. I am inserting these variables in the database when I am encrypting the login details and then I am fetching them when needed in the decryption code.
I am also storing my database connection script (PDO
) in a folder where there is a .htaccess
file with the following content:
order deny,allow
deny from all
allow from 127.0.0.1
1. Is my approach safe and hack-proof?
2. If somebody breaches my database will they be able to use my users' data?
3. How effective is the .htaccess file and can it stop hackers from accessing the database connection file?