{"id":691,"date":"2019-05-03T02:37:01","date_gmt":"2019-05-03T01:37:01","guid":{"rendered":"https:\/\/www.kolkataonweb.com\/code-bank\/?p=691"},"modified":"2019-03-12T11:48:14","modified_gmt":"2019-03-12T11:48:14","slug":"two-way-encryption-or-hashing-using-key","status":"publish","type":"post","link":"https:\/\/www.kolkataonweb.com\/code-bank\/php\/two-way-encryption-or-hashing-using-key\/","title":{"rendered":"Two Way Encryption or Hashing using Key"},"content":{"rendered":"<p>This is using PHP and openssl_decrypt\/openssl_encrypt<\/p>\n<p>Encrypting the string<\/p>\n<pre class='wp-code-highlight prettyprint'>$key = \"xxxxxxxxxxx\"; \/\/11 characters\r\n$ivlen = openssl_cipher_iv_length(\"aes-256-cbc-hmac-sha256\");\r\n$iv = openssl_random_pseudo_bytes($ivlen);\r\n$hash = openssl_encrypt(STRING TO HASH,\"aes-256-cbc-hmac-sha256\",$key,0,$iv);\r\n$iv = bin2hex($iv); \/\/ iv generated is in binary - converted to HEX for passing through SESSION or POST or URL<\/pre>\n<p>Decrypting back the string<\/p>\n<pre class='wp-code-highlight prettyprint'>$key = \"xxxxxxxxxxx\";\r\n$hash = HASH FROM ENCRYPTION;\r\n$iv = IV FROM ENCRYPTION STEP; \/\/note this is in HEX and needs to be converted back to BIN \r\n$iv = hex2bin($iv); \/\/convert the IV in HEX to BIN\r\n\r\n$decryptedString = openssl_decrypt($hash,\"aes-256-cbc-hmac-sha256\",$key,0,$iv);<\/pre>\n<p>Feel free to explore other algorithms<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This is using PHP and openssl_decrypt\/openssl_encrypt Encrypting the string $key = &#8220;xxxxxxxxxxx&#8221;; \/\/11 characters $ivlen = openssl_cipher_iv_length(&#8220;aes-256-cbc-hmac-sha256&#8243;); $iv = openssl_random_pseudo_bytes($ivlen); $hash = openssl_encrypt(STRING TO HASH,&#8221;aes-256-cbc-hmac-sha256&#8221;,$key,0,$iv); $iv = bin2hex($iv); \/\/ iv generated is in binary &#8211; converted to HEX for passing through SESSION or POST or URL Decrypting back the string $key = &#8220;xxxxxxxxxxx&#8221;; $hash =&hellip; <a class=\"more-link\" href=\"https:\/\/www.kolkataonweb.com\/code-bank\/php\/two-way-encryption-or-hashing-using-key\/\">Continue reading <span class=\"screen-reader-text\">Two Way Encryption or Hashing using Key<\/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":[183,182,185,184,186],"class_list":["post-691","post","type-post","status-publish","format-standard","hentry","category-php","tag-2-way-encryption","tag-2-way-hashing","tag-encryption","tag-hashing","tag-openssl_decrypt","entry"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.kolkataonweb.com\/code-bank\/wp-json\/wp\/v2\/posts\/691","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=691"}],"version-history":[{"count":2,"href":"https:\/\/www.kolkataonweb.com\/code-bank\/wp-json\/wp\/v2\/posts\/691\/revisions"}],"predecessor-version":[{"id":1056,"href":"https:\/\/www.kolkataonweb.com\/code-bank\/wp-json\/wp\/v2\/posts\/691\/revisions\/1056"}],"wp:attachment":[{"href":"https:\/\/www.kolkataonweb.com\/code-bank\/wp-json\/wp\/v2\/media?parent=691"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kolkataonweb.com\/code-bank\/wp-json\/wp\/v2\/categories?post=691"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kolkataonweb.com\/code-bank\/wp-json\/wp\/v2\/tags?post=691"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}