This shows you the differences between two versions of the page.
| Both sides previous revision Previous revision Next revision | Previous revision | ||
|
uphp:functions:aes_decrypt [2019/08/16 06:36] admin |
uphp:functions:aes_decrypt [2021/09/13 05:57] (current) |
||
|---|---|---|---|
| Line 76: | Line 76: | ||
| $base64_decrypted = base64_decode($base64_encrypted); | $base64_decrypted = base64_decode($base64_encrypted); | ||
| | | ||
| - | // we will use the calculated length from the encryption | + | // we will calculated |
| - | // may not yield a valid result if the encrypted string has a zero in it. | + | // one as the strlen() function may not yield a valid result if the encrypted string |
| + | // has a zero in it. As base64 is 6 bits and our data is 8, we just need to multiply | ||
| + | // the the length by 6/8 or 0.75 to get the base64-decoded size. | ||
| | | ||
| - | | + | $len = intval(strlen($base64_encrypted) * 0.75); |
| - | + | ||
| - | $plain_data = aes_decrypt(& | + | |
| - | + | ||
| - | print(" | + | |
| - | + | ||
| - | ?> | + | |
| - | </ | + | |
| - | + | ||
| - | The above example will output something similar to: | + | |
| - | < | + | |
| - | Your encrypted data is: fcPkxhW0UM4VIYB1CsbK/ | + | |
| - | Your decrypted data is: This is my super secret encrypted string | + | |
| - | </ | + | |
| - | + | ||
| - | ====See Also==== | + | |
| - | + | ||
| - | [[aes_encrypt()]] - Encrypt data using the AES algorithm</ | + | |
| - | + | ||
| - | < | + | |
| - | + | ||
| - | Decrypt AES-encrypted data in < | + | |
| - | + | ||
| - | ====Description==== | + | |
| - | + | ||
| - | <well size=" | + | |
| - | <span style=" | + | |
| - | <span style=" | + | |
| - | <span style=" | + | |
| - | <span style=" | + | |
| - | <span style=" | + | |
| - | <span style=" | + | |
| - | <span style=" | + | |
| - | <span style=" | + | |
| - | ,<span style=" | + | |
| - | <span style=" | + | |
| - | <span style=" | + | |
| - | ,<span style=" | + | |
| - | <span style=" | + | |
| - | + | ||
| - | <span style=" | + | |
| - | </ | + | |
| - | + | ||
| - | Decrypts the string $data with length $length bytes. | + | |
| - | ====Parameter==== | + | |
| - | + | ||
| - | < | + | |
| - | + | ||
| - | < | + | |
| - | + | ||
| - | < | + | |
| - | + | ||
| - | < | + | |
| - | + | ||
| - | + | ||
| - | ====Return Values==== | + | |
| - | + | ||
| - | < | + | |
| - | + | ||
| - | ====Example==== | + | |
| - | + | ||
| - | <code php> | + | |
| - | < | + | |
| - | + | ||
| - | // create your custom key and IV value | + | |
| - | + | ||
| - | $key=" | + | |
| - | $iv=" | + | |
| - | + | ||
| - | + | ||
| - | $data=" | + | |
| - | + | ||
| - | // round up the string length to the nearest multiple of 16 | + | |
| - | + | ||
| - | | + | |
| - | + | ||
| - | // encrypt the data | + | |
| - | + | ||
| - | $encrypted_data = aes_encrypt($data, | + | |
| - | + | ||
| - | // store encrypted data into a base64-encoded string for easy tranport | + | |
| - | + | ||
| - | $base64_encrypted = base64_encode($encrypted_data); | + | |
| - | + | ||
| - | print(" | + | |
| - | + | ||
| - | // this will yield: | + | |
| - | // fcPkxhW0UM4VIYB1CsbK/ | + | |
| - | + | ||
| - | + | ||
| - | // base64 decode the string again | + | |
| - | + | ||
| - | $base64_decrypted = base64_decode($base64_encrypted); | + | |
| - | + | ||
| - | // we will use the calculated length from the encryption as the strlen() function | + | |
| - | // may not yield a valid result if the encrypted string has a zero in it. | + | |
| | | ||
| // decryption | // decryption | ||
| Line 193: | Line 100: | ||
| ====See Also==== | ====See Also==== | ||
| - | [[aes_encrypt()]] - Encrypt data using the AES algorithm</ | + | [[aes_encrypt()]] - Encrypt data using the AES algorithm |