User Tools

Site Tools


uphp:functions:aes_decrypt

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
uphp:functions:aes_decrypt [2019/08/16 06:36]
admin
uphp:functions:aes_decrypt [2019/08/16 08:24]
admin
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 as the strlen() function +  // we will calculated the length from the base64 string rather than the encrypted  
-  // 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.
      
-  // decryption  routine +  $len = intval(strlen($base64_encrypted) * 0.75);
-   +
-  $plain_data = aes_decrypt(&$base64_decrypted,$len,$key,$iv); +
-   +
-  print("\r\nYour decrypted data is: ".$plain_data); +
-   +
-?> +
-</code> +
- +
-The above example will output something similar to: +
-<code> +
-Your encrypted data is: fcPkxhW0UM4VIYB1CsbK/7wEBuC4WAwcO5tDBkcMXbckp5yUp4a92BeD7VpVGwd1 +
-Your decrypted data is: This is my super secret encrypted string  +
-</code> +
- +
-====See Also==== +
- +
-[[aes_encrypt()]] - Encrypt data using the AES algorithm</html>======aes_decrypt====== +
- +
-<badge>WMMEGA FW >= 2.1147</badge> <badge>WM-M2 FW >= 3.1147</badge> +
- +
-Decrypt AES-encrypted data in <html><b><span style="color:green">string<span style="color:black"></b></html> +
- +
-====Description==== +
- +
-<well size="sm"><html> +
-<span style="font-size:125%;color:green">string +
-<span style="color:black">aes_decrypt ( +
-<span style="color:green">string +
-<span style="color:blue">$data +
-<span style="color:black">, +
-<span style="color:green">int +
-<span style="color:blue">$length +
-<span style="color:black">, +
-,<span style="color:green">string +
-<span style="color:blue">$key +
-<span style="color:black">, +
-,<span style="color:green">string +
-<span style="color:blue">$iv +
- +
-<span style="color:black">+
-</html></well> +
- +
-Decrypts the string $data with length $length bytes.  Pass the encryption key and iv value in the corresponding fields. +
-====Parameter==== +
- +
-<html><b><span style="color:blue">$data<span style="color:black"></b>: AES-encrypted string - it should be a multiple of 16 bytes long </html> +
- +
-<html><b><span style="color:blue">$length<span style="color:black"></b>: length of encrypted string </html> +
- +
-<html><b><span style="color:blue">$key<span style="color:black"></b>: Encryption key - this should be multiples of 16 bytes long. A 16 character string will yield 128-bit encryption. </html> +
- +
-<html><b><span style="color:blue">$iv<span style="color:black"></b>: This should be a unique 16 character string which will be used for the iv table</html> +
- +
- +
-====Return Values==== +
- +
-<html>Decrypted <b><span style="color:green">string<span style="color:black"></b> (or <b><span style="color:green">int<span style="color:black"></b> 0 for error)</html> +
- +
-====Example==== +
- +
-<code php> +
-<pre><?  +
- +
-// create your custom key and IV value +
- +
-  $key="0123456789abcdef";  // this is 16 bytes, or 128 bits +
-  $iv="abcdef0123456789";   // this needs to be 16 bytes long +
-   +
- +
-  $data="This is my super secret encrypted string"; +
-   +
-  // round up the string length to the nearest multiple of 16 +
-   +
-  $len=intval((strlen($data)+1)/16)*16; +
- +
-  // encrypt the data   +
-   +
-  $encrypted_data = aes_encrypt($data,$len,$key,$iv); +
-   +
-  // store encrypted data into a base64-encoded string for easy tranport +
- +
-  $base64_encrypted = base64_encode($encrypted_data); +
-   +
-  print("Your encrypted data is: ".$base64_encrypted); +
-   +
-  // this will yield:  +
-  // fcPkxhW0UM4VIYB1CsbK/7wEBuC4WAwcO5tDBkcMXbfmf/gOHqdnrz5qHBRVY8Ls +
-   +
-   +
-  // 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  routine   // decryption  routine
Line 193: Line 100:
 ====See Also==== ====See Also====
  
-[[aes_encrypt()]] - Encrypt data using the AES algorithm</html>+[[aes_encrypt()]] - Encrypt data using the AES algorithm
uphp/functions/aes_decrypt.txt · Last modified: 2021/09/13 05:57 (external edit)