{"id":212,"date":"2015-01-16T17:04:41","date_gmt":"2015-01-16T17:04:41","guid":{"rendered":"https:\/\/www.kolkataonweb.com\/code-bank\/?p=212"},"modified":"2015-01-16T17:06:19","modified_gmt":"2015-01-16T17:06:19","slug":"php-encrypt-and-decrypt","status":"publish","type":"post","link":"https:\/\/www.kolkataonweb.com\/code-bank\/php\/php-encrypt-and-decrypt\/","title":{"rendered":"PHP Encrypt and Decrypt"},"content":{"rendered":"<p>The below can be used for encrypting something that needs to be decrypted again. It can be used for implementing a simple &#8220;remember login&#8221; also.<\/p>\n<p>The first parameter of mcrypt_encrypt or mcrypt_decrypt is\u00a0 &#8220;<em>One of the MCRYPT_ciphername constants, or the name of the algorithm as string.<\/em>&#8221; Details can be found here <a href=\"http:\/\/php.net\/manual\/en\/mcrypt.ciphers.php\">http:\/\/php.net\/manual\/en\/mcrypt.ciphers.php<\/a>\u00a0Change it as per suitability.<\/p>\n<p>Details of the fourth parameter in mcrypt_encrypt or mcrypt_decrypt can be found here <a href=\"http:\/\/php.net\/manual\/en\/mcrypt.constants.php\">http:\/\/php.net\/manual\/en\/mcrypt.constants.php<\/a><\/p>\n<h5><\/h5>\n<h5>Encryption<\/h5>\n<pre class='wp-code-highlight prettyprint'>$iv_size = mcrypt_get_iv_size(MCRYPT_CAST_256, MCRYPT_MODE_CFB);\r\n$iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);\r\n\r\n$key = \"dkareitz77g5f865q8lt5jvdo0$36iu\"; \/\/private key\r\n\r\n$temp = mcrypt_encrypt(MCRYPT_CAST_256, $key, $email, MCRYPT_MODE_CFB, $iv); \/\/encrypting the email id\r\n\r\nsetcookie(\"username\",base64_encode($temp),time()+60*60*24*30,\"\/\", \"domain.com\"); \/\/storing the username in Cookie after base64_encoding the above generated hash. base64_encoding not necessary and can be skipped\r\n\r\n$iv = mcrypt_encrypt(MCRYPT_BLOWFISH,\"abcdefgh\", $iv, MCRYPT_MODE_ECB); \/\/encrypting the iv also using a different method\r\n\r\nsetcookie(\"iv1\",base64_encode($iv),time()+60*60*24*30,\"\/\", \"domain.com\"); \/\/storing in cookie.<\/pre>\n<h5>Decryption<\/h5>\n<pre class='wp-code-highlight prettyprint'>$logEmail = base64_decode($_COOKIE['username']);\r\n$iv = base64_decode($_COOKIE['iv1']);\r\n\r\n$iv = mcrypt_decrypt(MCRYPT_BLOWFISH,\"abcdefgh\", $iv, MCRYPT_MODE_ECB); \/\/first decrypt the iv\r\n\r\n$key = \"dkareitz77g5f865q8lt5jvdo0$36iu\"; \/\/same private key used in encryption\r\n\r\n$email = mcrypt_decrypt(MCRYPT_CAST_256, $key, $logEmail, MCRYPT_MODE_CFB, $iv); \/\/decrypt the email<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>The below can be used for encrypting something that needs to be decrypted again. It can be used for implementing a simple &#8220;remember login&#8221; also. The first parameter of mcrypt_encrypt or mcrypt_decrypt is\u00a0 &#8220;One of the MCRYPT_ciphername constants, or the name of the algorithm as string.&#8221; Details can be found here http:\/\/php.net\/manual\/en\/mcrypt.ciphers.php\u00a0Change it as per&hellip; <a class=\"more-link\" href=\"https:\/\/www.kolkataonweb.com\/code-bank\/php\/php-encrypt-and-decrypt\/\">Continue reading <span class=\"screen-reader-text\">PHP Encrypt and Decrypt<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[9,7,8],"class_list":["post-212","post","type-post","status-publish","format-standard","hentry","category-php","tag-keep-logged-in","tag-remember-login","tag-remember-me","entry"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.kolkataonweb.com\/code-bank\/wp-json\/wp\/v2\/posts\/212","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.kolkataonweb.com\/code-bank\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.kolkataonweb.com\/code-bank\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.kolkataonweb.com\/code-bank\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.kolkataonweb.com\/code-bank\/wp-json\/wp\/v2\/comments?post=212"}],"version-history":[{"count":3,"href":"https:\/\/www.kolkataonweb.com\/code-bank\/wp-json\/wp\/v2\/posts\/212\/revisions"}],"predecessor-version":[{"id":215,"href":"https:\/\/www.kolkataonweb.com\/code-bank\/wp-json\/wp\/v2\/posts\/212\/revisions\/215"}],"wp:attachment":[{"href":"https:\/\/www.kolkataonweb.com\/code-bank\/wp-json\/wp\/v2\/media?parent=212"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kolkataonweb.com\/code-bank\/wp-json\/wp\/v2\/categories?post=212"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kolkataonweb.com\/code-bank\/wp-json\/wp\/v2\/tags?post=212"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}