Data Encryption Using PHP openssl_pkey

Simple and secure method for encryption and decryption using open_ssl in php. It is very useful for securing data over your php application.
Data Encryption Using PHP Web Development
Data Encryption Using PHP Web Development
1163 Views

The simple method of Data Encryption and Decryption using openssl_pkey.

Hello! Folks, This blog is about encrypting and decrypting your data using PHP openssl_pkey methods. Before moving on to the main topics of the article make sure your server has enabled open_ssl and working properly. [read the article here]. There are other methods available in PHP for encryption and decryption. Once the configuration is done just follow the below methods:

In this blog post, we will discuss how to use PHP openssl_pkey to encrypt and decrypt data. It is an encryption library that uses the OpenSSL library. It provides an easy-to-use interface for data encryption and decryption.

First, we will discuss how to generate a public and private key pair using PHP openssl_pkey. Next, we will discuss how to use these keys to encrypt and decrypt data. Finally, we will discuss how to use PHP openssl_pkey to sign data.

There are other methods or functions to encrypt and decrypt your data as mentioned here.

Step 1:

Create a php file and add below codes:

class CryptGen{
// generate private and public key 
    public static function genKey(){
        $res = openssl_pkey_new([
            "private_key_bits" => 2048,
            "private_key_type" => OPENSSL_KEYTYPE_RSA,
        ]);
        
        openssl_pkey_export($res,$privatekey);
        return [$privatekey, openssl_pkey_get_details($res)['key']];
    }
    

//encryption method
    public static function getEncrypt($messge, $privateKey){
        openssl_private_encrypt($messge, $encrypted, $privateKey);
        return base64_encode($encrypted);
    }

//decryption method    
    public static function getDecrypt($encrypted, $publickey){
        openssl_public_decrypt(base64_decode($encrypted), $decrypted, $publickey);
        return $decrypted;
    }
    
// validate encrypted string
    public static function isValid($message,$encrypted, $publicKey){
        return $message == self::getDecrypt($encrypted,$publicKey);
    }

 
}

Now you nee to execute the class and its methods to get the result.

//get the key pairs
[$privateKey, $publickey] = CryptGen::genKey();

// Simple string
$message = "Hi There"."<br/>";

// Now encrypt
$encrypted_message = CryptGen::getEncrypt($message,$privateKey);

// encrypted message
echo $encrypted_message."<br/>";

//decrypted message
echo CryptGen::getDecrypt($encrypted_message, $publickey)."<br/>";

// vlidate message (returns bool(true) or bool(false))
var_dump(CryptGen::isValid($message,$encrypted_message,$publickey));

Hope it will work for you! 🙂

If somebody wants to develop a blockchain project [demo project] then it might help to build basic encryption and decryption.

Total
0
Shares
Previous Post
Security Tips for PHP Web Applications

Tips for PHP Web Application Security

Next Post
Python Desktop GUI

Python Desktop GUI – Part 1

Related Posts