Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<h2>Answer Background and Explanation</h2> <p>To understand this question, you must first understand what SHA256 is. SHA256 is a <strong>Cryptographic Hash Function</strong>. A Cryptographic Hash Function is a one-way function, whose output is cryptographically secure. This means it is easy to compute a hash (equivalent to encrypting data), but hard to get the original input using the hash (equivalent to decrypting the data). Since using a Cryptographic hash function means decrypting is computationally infeasible, so therefore you cannot perform decryption with SHA256. </p> <p>What you want to use is a two-way function, but more specifically, a <strong>Block Cipher</strong>. A function that allows for both encryption and decryption of data. The functions <code>mcrypt_encrypt</code> and <code>mcrypt_decrypt</code> by default use the Blowfish algorithm. PHP's use of mcrypt can be found in this <a href="http://www.php.net/manual/en/intro.mcrypt.php">manual</a>. A list of <a href="http://www.php.net/manual/en/mcrypt.ciphers.php">cipher definitions</a> to select the cipher mcrypt uses also exists. A wiki on Blowfish can be found at <a href="http://en.wikipedia.org/wiki/Blowfish_%28cipher%29">Wikipedia</a>. A block cipher encrypts the input in blocks of known size and position with a known key, so that the data can later be decrypted using the key. This is what SHA256 cannot provide you.</p> <h2>Code</h2> <pre><code>$key = 'ThisIsTheCipherKey'; $ciphertext = mcrypt_encrypt(MCRYPT_BLOWFISH, $key, 'This is plaintext.', MCRYPT_MODE_CFB); $plaintext = mcrypt_decrypt(MCRYPT_BLOWFISH, $key, $encrypted, MCRYPT_MODE_CFB); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload