Web Niraj
  • Facebook
  • Flickr
  • Github
  • Linkedin
  • Twitter
  • YouTube
Online portfolio, code examples and developer blog
  • About
  • Contact
  • Portfolio
  • WordPress
Search the site...
  • Home
  • Blog
  • Create Public / Privacy Encryption Keys and Encrypt / Decrypt Data using PHP

Create Public / Privacy Encryption Keys and Encrypt / Decrypt Data using PHP

3

In a recent project, I’ve been using public/private key encryption to transmit data from a mobile application to an API, keeping sensitive information secure as a result. As the API is built in PHP, I was necessary to use PHP to decrypt the incoming data so that it could be processed and stored in the database. This tutorial explains how you can create your own keys, and then use them to encrypt or decrypt data using PHP.

In order to use the below code, your PHP installation needs to support OpenSSL, which is compatible with PHP4.2.0+ and PHP5.

It’s also worth noting that only limited length data can be encrypted using this method. As I’m using a 4096 bit key, I would be able to encrypt a maximum of 512 bytes (if you include the OPENSSL_NO_PADDING flag, but this means that it’s does not conform to the PKCS#1 standard). As padding takes up a minimum of 11 bytes, the maximum data you can encrypt (using the default OPENSSL_PKCS1_PADDING flag) will be 501 bytes (or 0.5 KiB).

Creating the Keys

The first step is to create a pair of keys that will encrypt / decrypt the data for you.

https://gist.github.com/niraj-shah/81877fcf5eca02b08f50

Encrypting / Decrypting Data

Using the newly created keys, you can then use the openssl_public_encrypt and openssl_private_decrypt functions to encrypt or decrypt some data, respectively. The below example shows a string that is encrypted using the public key, and decrypted using the private one.

https://gist.github.com/niraj-shah/060797d0eaecf98d9601

You can also do the opposite – encrypt data using the private key (using openssl_private_encrypt) and decrypt data using the public one (using openssl_public_decrypt)

Encryption, PHP, Public / Private Keys, Security, Tutorial

3 comments on “Create Public / Privacy Encryption Keys and Encrypt / Decrypt Data using PHP”

  1. Amit Kumar says:
    November 19, 2022 at 6:24 AM

    data is encrypted using public key but decrypt is not happening

    it’s showing-> openssl_private_decrypt(): key parameter is not a valid private key

    Reply
    • Niraj Shah says:
      November 19, 2022 at 9:27 PM

      Have you verified that the path to the key is correct?

      Reply
  2. Amit Kumar says:
    November 22, 2022 at 9:36 AM

    Thanks now it’s working , that time some error in my private key i solved this , now working fine thanks for sharing helpful information.

    🙂

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

StackExchange / StackOverflow

profile for Niraj Shah on Stack Exchange, a network of free, community-driven Q&A sites

Support Me

PSN Profile

Tags

ACL Amazon Amazon Web Services Android Android 4.4 KitKat Android 5.0 Lollipop Apache Backup Bug Command Line Cordova cPanel / WHM Facebook Facebook Graph API Facebook PHP SDK 4.0 Facebook Social Plugins Fan Page Firewall Flash Gadget Geolocation Google Nexus 5 Hacking HTML5 iOS JavaScript jQuery Laravel 5 Linux NodeJS Parse PDF PHP Plugin Portfolio PS4 Review Security Server SSH SSL Sysadmin Tutorial WordPress WordPress Plugins
© 2011-2025 Niraj Shah
  • Blog
  • Portfolio
  • WordPress
  • About Me
  • Contact Me
  • Privacy Policy