{"id":1676,"date":"2014-04-02T21:00:58","date_gmt":"2014-04-03T04:00:58","guid":{"rendered":"http:\/\/www.virendrachandak.com\/techtalk\/\/?p=1676"},"modified":"2019-12-22T15:31:11","modified_gmt":"2019-12-22T23:31:11","slug":"encryption-using-php-openssl","status":"publish","type":"post","link":"https:\/\/www.virendrachandak.com\/techtalk\/encryption-using-php-openssl\/","title":{"rendered":"Encryption using PHP and OpenSSL"},"content":{"rendered":"<p>In this post we will see how to encrypt and decrypt data using <a href=\"http:\/\/php.net\/manual\/en\/book.openssl.php\" rel=\"external nofollow\">PHP OpenSSL<\/a>. We will be using asymmetric (public\/private key) encryption. In this encryption a user generates a pair of public \/ private keys and gives the public key to anyone who wants to send the data. The sender of the data will encrypt the data using the public key of the receiver. The receiver will then decrypt the received data using his own private key. The data encrypted using the public key can only be decrypted using the corresponding private key.<br \/>\n<!--more--><br \/>\nThe amount of data you can encrypt and the size of the resulting encrypted data are both determined by the size of the key. The size of the encrypted data is the number of bytes in the key (rounded up). So for a 1024-bit key this will be 128 bytes (1024 divided by 8). Even if you were to encrypt a string with a single byte in it, the resulting encrypted data would still be 128 bytes long. The maximum amount of data that can be encrypted is 11 bytes less than this, since OPENSSL uses a padding scheme (if no padding scheme is used by using OPENSSL_NO_PADDING flag then we can use the entire length). So for a 1024-bit key, up to 117 bytes can be encrypted. <\/p>\n<p>We cannot encrypt and send large amount of data at once, so we need to divide it into smaller chunks. In the following example we divide the data into multiple chunks before encrypting it and then combine the encrypted data and send it. The receiver then breaks the encrypted data into chunks and decrypts it.<\/p>\n<h3>Generating public \/ private Keys<\/h3>\n<p>We will first need a pair of public \/ private keys. Here is a sample PHP code to generate the public \/ private Keys.<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n&lt;?php\r\n$privateKey = openssl_pkey_new(array(\r\n    'private_key_bits' =&gt; 2048,      \/\/ Size of Key.\r\n    'private_key_type' =&gt; OPENSSL_KEYTYPE_RSA,\r\n));\r\n\/\/ Save the private key to private.key file. Never share this file with anyone.\r\nopenssl_pkey_export_to_file($privateKey, 'private.key');\r\n\r\n\/\/ Generate the public key for the private key\r\n$a_key = openssl_pkey_get_details($privateKey);\r\n\/\/ Save the public key in public.key file. Send this file to anyone who want to send you the encrypted data.\r\nfile_put_contents('public.key', $a_key&#x5B;'key']);\r\n\r\n\/\/ Free the private Key.\r\nopenssl_free_key($privateKey);\r\n<\/pre>\n<p>The above code will generate a pair of public \/ private keys. Never share the private key with anyone. Give the public key to anyone who will send you encrypted data.<\/p>\n<h3>Encrypting data<\/h3>\n<p>Here is the code that can be used to encrypt the data. This code assumes that we already have the recipient&#8217;s public key.<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n&lt;?php\r\n\/\/ Data to be sent\r\n$plaintext = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean eleifend vestibulum nunc sit amet mattis. Nulla at volutpat nulla. Pellentesque sodales vel ligula quis consequat. Suspendisse dapibus dolor nec viverra venenatis. Pellentesque blandit vehicula eleifend. Duis eget fermentum velit. Vivamus varius ut dui vel malesuada. Ut adipiscing est non magna posuere ullamcorper. Proin pretium nibh nec elementum tincidunt. Vestibulum leo urna, porttitor et aliquet id, ornare at nibh. Maecenas placerat justo nunc, varius condimentum diam fringilla sed. Donec auctor tellus vitae justo venenatis, sit amet vulputate felis accumsan. Aenean aliquet bibendum magna, ac adipiscing orci venenatis vitae.';\r\n\r\necho 'Plain text: ' . $plaintext;\r\n\/\/ Compress the data to be sent\r\n$plaintext = gzcompress($plaintext);\r\n\r\n\/\/ Get the public Key of the recipient\r\n$publicKey = openssl_pkey_get_public('file:\/\/\/path\/to\/public.key');\r\n$a_key = openssl_pkey_get_details($publicKey);\r\n\r\n\/\/ Encrypt the data in small chunks and then combine and send it.\r\n$chunkSize = ceil($a_key&#x5B;'bits'] \/ 8) - 11;\r\n$output = '';\r\n\r\nwhile ($plaintext)\r\n{\r\n\t$chunk = substr($plaintext, 0, $chunkSize);\r\n\t$plaintext = substr($plaintext, $chunkSize);\r\n\t$encrypted = '';\r\n\tif (!openssl_public_encrypt($chunk, $encrypted, $publicKey))\r\n\t{\r\n\t\tdie('Failed to encrypt data');\r\n\t}\r\n\t$output .= $encrypted;\r\n}\r\nopenssl_free_key($publicKey);\r\n\r\n\/\/ This is the final encrypted data to be sent to the recipient\r\n$encrypted = $output;\r\n<\/pre>\n<h3>Decrypting data<\/h3>\n<p>Once a user receives encrypted data using his public key, the user can decrypt it using his own private key. Here is sample code to decrypt the encrypted data.<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n&lt;?php\r\n\/\/ Get the private Key\r\nif (!$privateKey = openssl_pkey_get_private('file:\/\/\/path\/to\/private.key'))\r\n{\r\n\tdie('Private Key failed');\r\n}\r\n$a_key = openssl_pkey_get_details($privateKey);\r\n\r\n\/\/ Decrypt the data in the small chunks\r\n$chunkSize = ceil($a_key&#x5B;'bits'] \/ 8);\r\n$output = '';\r\n\r\nwhile ($encrypted)\r\n{\r\n\t$chunk = substr($encrypted, 0, $chunkSize);\r\n\t$encrypted = substr($encrypted, $chunkSize);\r\n\t$decrypted = '';\r\n\tif (!openssl_private_decrypt($chunk, $decrypted, $privateKey))\r\n\t{\r\n\t\tdie('Failed to decrypt data');\r\n\t}\r\n\t$output .= $decrypted;\r\n}\r\nopenssl_free_key($privateKey);\r\n\r\n\/\/ Uncompress the unencrypted data.\r\n$output = gzuncompress($output);\r\n\r\necho '&lt;br \/&gt;&lt;br \/&gt; Unencrypted Data: ' . $output;\r\n<\/pre>\n<p>List of all PHP functions for OpenSSL can be found at <a href=\"http:\/\/www.php.net\/manual\/en\/ref.openssl.php\" rel=\"external nofollow\">OpenSSL Functions<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this post we will see how to encrypt and decrypt data using PHP OpenSSL. We will be using asymmetric (public\/private key) encryption. In this encryption a user generates a pair of public \/ private keys and gives the public key to anyone who wants to send the data. The sender of the data will [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[143,131],"tags":[130,132],"class_list":["post-1676","post","type-post","status-publish","format-standard","hentry","category-php","category-security","tag-encryption","tag-openssl"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.1 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Encryption using PHP and OpenSSL - Virendra&#039;s TechTalk<\/title>\n<meta name=\"description\" content=\"In this post we will see how to encrypt and decrypt data using PHP OpenSSL. We will be using asymmetric (public\/private key) encryption. In this\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.virendrachandak.com\/techtalk\/encryption-using-php-openssl\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Encryption using PHP and OpenSSL - Virendra&#039;s TechTalk\" \/>\n<meta property=\"og:description\" content=\"In this post we will see how to encrypt and decrypt data using PHP OpenSSL. We will be using asymmetric (public\/private key) encryption. In this\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.virendrachandak.com\/techtalk\/encryption-using-php-openssl\/\" \/>\n<meta property=\"og:site_name\" content=\"Virendra&#039;s TechTalk\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/virendrachandak\" \/>\n<meta property=\"article:author\" content=\"https:\/\/www.facebook.com\/virendrachandak\" \/>\n<meta property=\"article:published_time\" content=\"2014-04-03T04:00:58+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2019-12-22T23:31:11+00:00\" \/>\n<meta name=\"author\" content=\"Virendra Chandak\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@virendrachandak\" \/>\n<meta name=\"twitter:site\" content=\"@virendrachandak\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Virendra Chandak\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.virendrachandak.com\\\/techtalk\\\/encryption-using-php-openssl\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.virendrachandak.com\\\/techtalk\\\/encryption-using-php-openssl\\\/\"},\"author\":{\"name\":\"Virendra Chandak\",\"@id\":\"https:\\\/\\\/www.virendrachandak.com\\\/techtalk\\\/#\\\/schema\\\/person\\\/63f7ffa1ea125e32af9618d188349e17\"},\"headline\":\"Encryption using PHP and OpenSSL\",\"datePublished\":\"2014-04-03T04:00:58+00:00\",\"dateModified\":\"2019-12-22T23:31:11+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.virendrachandak.com\\\/techtalk\\\/encryption-using-php-openssl\\\/\"},\"wordCount\":801,\"commentCount\":10,\"publisher\":{\"@id\":\"https:\\\/\\\/www.virendrachandak.com\\\/techtalk\\\/#\\\/schema\\\/person\\\/63f7ffa1ea125e32af9618d188349e17\"},\"keywords\":[\"encryption\",\"OpenSSL\"],\"articleSection\":[\"PHP\",\"Security\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.virendrachandak.com\\\/techtalk\\\/encryption-using-php-openssl\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.virendrachandak.com\\\/techtalk\\\/encryption-using-php-openssl\\\/\",\"url\":\"https:\\\/\\\/www.virendrachandak.com\\\/techtalk\\\/encryption-using-php-openssl\\\/\",\"name\":\"Encryption using PHP and OpenSSL - Virendra&#039;s TechTalk\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.virendrachandak.com\\\/techtalk\\\/#website\"},\"datePublished\":\"2014-04-03T04:00:58+00:00\",\"dateModified\":\"2019-12-22T23:31:11+00:00\",\"description\":\"In this post we will see how to encrypt and decrypt data using PHP OpenSSL. We will be using asymmetric (public\\\/private key) encryption. In this\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.virendrachandak.com\\\/techtalk\\\/encryption-using-php-openssl\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.virendrachandak.com\\\/techtalk\\\/encryption-using-php-openssl\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.virendrachandak.com\\\/techtalk\\\/encryption-using-php-openssl\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"TechTalk\",\"item\":\"https:\\\/\\\/www.virendrachandak.com\\\/techtalk\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"PHP\",\"item\":\"https:\\\/\\\/www.virendrachandak.com\\\/techtalk\\\/category\\\/php\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Encryption using PHP and OpenSSL\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.virendrachandak.com\\\/techtalk\\\/#website\",\"url\":\"https:\\\/\\\/www.virendrachandak.com\\\/techtalk\\\/\",\"name\":\"Virendra's TechTalk\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.virendrachandak.com\\\/techtalk\\\/#\\\/schema\\\/person\\\/63f7ffa1ea125e32af9618d188349e17\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.virendrachandak.com\\\/techtalk\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\\\/\\\/www.virendrachandak.com\\\/techtalk\\\/#\\\/schema\\\/person\\\/63f7ffa1ea125e32af9618d188349e17\",\"name\":\"Virendra Chandak\",\"logo\":{\"@id\":\"https:\\\/\\\/www.virendrachandak.com\\\/techtalk\\\/#\\\/schema\\\/person\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.virendrachandak.com\",\"https:\\\/\\\/www.facebook.com\\\/virendrachandak\",\"https:\\\/\\\/www.linkedin.com\\\/in\\\/virendrachandak\\\/\",\"https:\\\/\\\/x.com\\\/virendrachandak\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Encryption using PHP and OpenSSL - Virendra&#039;s TechTalk","description":"In this post we will see how to encrypt and decrypt data using PHP OpenSSL. We will be using asymmetric (public\/private key) encryption. In this","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.virendrachandak.com\/techtalk\/encryption-using-php-openssl\/","og_locale":"en_US","og_type":"article","og_title":"Encryption using PHP and OpenSSL - Virendra&#039;s TechTalk","og_description":"In this post we will see how to encrypt and decrypt data using PHP OpenSSL. We will be using asymmetric (public\/private key) encryption. In this","og_url":"https:\/\/www.virendrachandak.com\/techtalk\/encryption-using-php-openssl\/","og_site_name":"Virendra&#039;s TechTalk","article_publisher":"https:\/\/www.facebook.com\/virendrachandak","article_author":"https:\/\/www.facebook.com\/virendrachandak","article_published_time":"2014-04-03T04:00:58+00:00","article_modified_time":"2019-12-22T23:31:11+00:00","author":"Virendra Chandak","twitter_card":"summary_large_image","twitter_creator":"@virendrachandak","twitter_site":"@virendrachandak","twitter_misc":{"Written by":"Virendra Chandak","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.virendrachandak.com\/techtalk\/encryption-using-php-openssl\/#article","isPartOf":{"@id":"https:\/\/www.virendrachandak.com\/techtalk\/encryption-using-php-openssl\/"},"author":{"name":"Virendra Chandak","@id":"https:\/\/www.virendrachandak.com\/techtalk\/#\/schema\/person\/63f7ffa1ea125e32af9618d188349e17"},"headline":"Encryption using PHP and OpenSSL","datePublished":"2014-04-03T04:00:58+00:00","dateModified":"2019-12-22T23:31:11+00:00","mainEntityOfPage":{"@id":"https:\/\/www.virendrachandak.com\/techtalk\/encryption-using-php-openssl\/"},"wordCount":801,"commentCount":10,"publisher":{"@id":"https:\/\/www.virendrachandak.com\/techtalk\/#\/schema\/person\/63f7ffa1ea125e32af9618d188349e17"},"keywords":["encryption","OpenSSL"],"articleSection":["PHP","Security"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.virendrachandak.com\/techtalk\/encryption-using-php-openssl\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.virendrachandak.com\/techtalk\/encryption-using-php-openssl\/","url":"https:\/\/www.virendrachandak.com\/techtalk\/encryption-using-php-openssl\/","name":"Encryption using PHP and OpenSSL - Virendra&#039;s TechTalk","isPartOf":{"@id":"https:\/\/www.virendrachandak.com\/techtalk\/#website"},"datePublished":"2014-04-03T04:00:58+00:00","dateModified":"2019-12-22T23:31:11+00:00","description":"In this post we will see how to encrypt and decrypt data using PHP OpenSSL. We will be using asymmetric (public\/private key) encryption. In this","breadcrumb":{"@id":"https:\/\/www.virendrachandak.com\/techtalk\/encryption-using-php-openssl\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.virendrachandak.com\/techtalk\/encryption-using-php-openssl\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.virendrachandak.com\/techtalk\/encryption-using-php-openssl\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"TechTalk","item":"https:\/\/www.virendrachandak.com\/techtalk\/"},{"@type":"ListItem","position":2,"name":"PHP","item":"https:\/\/www.virendrachandak.com\/techtalk\/category\/php\/"},{"@type":"ListItem","position":3,"name":"Encryption using PHP and OpenSSL"}]},{"@type":"WebSite","@id":"https:\/\/www.virendrachandak.com\/techtalk\/#website","url":"https:\/\/www.virendrachandak.com\/techtalk\/","name":"Virendra's TechTalk","description":"","publisher":{"@id":"https:\/\/www.virendrachandak.com\/techtalk\/#\/schema\/person\/63f7ffa1ea125e32af9618d188349e17"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.virendrachandak.com\/techtalk\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/www.virendrachandak.com\/techtalk\/#\/schema\/person\/63f7ffa1ea125e32af9618d188349e17","name":"Virendra Chandak","logo":{"@id":"https:\/\/www.virendrachandak.com\/techtalk\/#\/schema\/person\/image\/"},"sameAs":["https:\/\/www.virendrachandak.com","https:\/\/www.facebook.com\/virendrachandak","https:\/\/www.linkedin.com\/in\/virendrachandak\/","https:\/\/x.com\/virendrachandak"]}]}},"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p2vTtQ-r2","jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":1673,"url":"https:\/\/www.virendrachandak.com\/techtalk\/using-php-create-passwords-for-htpasswd-file\/","url_meta":{"origin":1676,"position":0},"title":"How to generate passwords for .htpasswd using PHP","author":"Virendra Chandak","date":"March 2, 2014","format":false,"excerpt":"In my earlier post about .htaccess I had described about authentication using .htaccess and command to generate .htpasswd file. However, when we want to add passwords for many users that method will take too long, since we will have to add passwords for each user one at a time. However,\u2026","rel":"","context":"In &quot;Security&quot;","block_context":{"text":"Security","link":"https:\/\/www.virendrachandak.com\/techtalk\/category\/security\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":2196,"url":"https:\/\/www.virendrachandak.com\/techtalk\/using-php-bcrypt-algorithm-for-htpasswd-generation\/","url_meta":{"origin":1676,"position":1},"title":"Generating bcrypt .htpasswd passwords using PHP","author":"Virendra Chandak","date":"September 23, 2019","format":false,"excerpt":"In my previous post we saw how to generate .htpasswd file using crypt and apr1-md5 algorithm in PHP. However, now there is a more secure BCRYPT algorithm that can be used since apache 2.4 for passwords in .htpasswd. In this post we will generate .htpasswd file using the BCRYPT algorithm\u2026","rel":"","context":"In &quot;Security&quot;","block_context":{"text":"Security","link":"https:\/\/www.virendrachandak.com\/techtalk\/category\/security\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1714,"url":"https:\/\/www.virendrachandak.com\/techtalk\/php-5-5-password-hashing-api\/","url_meta":{"origin":1676,"position":2},"title":"PHP 5.5 Password Hashing API","author":"Virendra Chandak","date":"June 1, 2014","format":false,"excerpt":"Most of the applications or websites today have a user registration system which requires storing usernames, passwords etc. A developer of the application should always store passwords securely and never in plain text. There are many methods to encrypt or hash passwords and store in the database but which method\u2026","rel":"","context":"In &quot;PHP&quot;","block_context":{"text":"PHP","link":"https:\/\/www.virendrachandak.com\/techtalk\/category\/php\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1968,"url":"https:\/\/www.virendrachandak.com\/techtalk\/creating-csv-file-using-php-and-mysql\/","url_meta":{"origin":1676,"position":3},"title":"How to create CSV file using PHP","author":"Virendra Chandak","date":"April 19, 2015","format":false,"excerpt":"CSV (comma-separated values) is one of the most popular methods for transferring tabular data between applications. Lot of applications want to export data in a CSV file. In this article we will see how we can create CSV file using PHP. We will also see how to automatically download the\u2026","rel":"","context":"In &quot;PHP&quot;","block_context":{"text":"PHP","link":"https:\/\/www.virendrachandak.com\/techtalk\/category\/php\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1972,"url":"https:\/\/www.virendrachandak.com\/techtalk\/how-to-sort-a-multi-dimension-array-by-value-in-php\/","url_meta":{"origin":1676,"position":4},"title":"How to sort a multi-dimension array by value in PHP","author":"Virendra Chandak","date":"February 16, 2015","format":false,"excerpt":"In this article we will see how to sort a multi-dimension array by value of one of the keys of the array. We can use a few different methods to do this. One way to to use usort() function. Another way is to just identify the values and create another\u2026","rel":"","context":"In &quot;PHP&quot;","block_context":{"text":"PHP","link":"https:\/\/www.virendrachandak.com\/techtalk\/category\/php\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1239,"url":"https:\/\/www.virendrachandak.com\/techtalk\/how-to-get-size-of-blob-in-mysql\/","url_meta":{"origin":1676,"position":5},"title":"How to get size of BLOB in MySQL","author":"Virendra Chandak","date":"December 23, 2012","format":false,"excerpt":"Recently I wanted to get the size of the data stored in the BLOB field of a MySQL table. BLOB is a field which can be used to store variable amount of data. There is a simple MySQL String function, to find the size of BLOB data, OCTET_LENGTH. This function\u2026","rel":"","context":"In &quot;MySQL&quot;","block_context":{"text":"MySQL","link":"https:\/\/www.virendrachandak.com\/techtalk\/category\/mysql\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"_links":{"self":[{"href":"https:\/\/www.virendrachandak.com\/techtalk\/wp-json\/wp\/v2\/posts\/1676","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.virendrachandak.com\/techtalk\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.virendrachandak.com\/techtalk\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.virendrachandak.com\/techtalk\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.virendrachandak.com\/techtalk\/wp-json\/wp\/v2\/comments?post=1676"}],"version-history":[{"count":0,"href":"https:\/\/www.virendrachandak.com\/techtalk\/wp-json\/wp\/v2\/posts\/1676\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.virendrachandak.com\/techtalk\/wp-json\/wp\/v2\/media?parent=1676"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.virendrachandak.com\/techtalk\/wp-json\/wp\/v2\/categories?post=1676"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.virendrachandak.com\/techtalk\/wp-json\/wp\/v2\/tags?post=1676"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}