Logo
    • Register
    • Login
    • Search
    • Recent
    • Tags
    • Users
    • air-Q Shop

    Encrypt und Decrypt mit PHP

    Software
    2
    2
    326
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • M
      Micha last edited by

      Es existiert ja Code in Python und Javascript zur Ver- und Entschlüsselung (https://air-q-updates.s3.eu-central-1.amazonaws.com/dokumentation/index.html). Das lässt sich auch auf PHP portieren.
      Hierzu nutzt man in PHP OpenSSL.

      PHP-decrypt:

      function decrypt($msgb64,$password)
      {
          $airqpass = $password;
      	if (strlen($airqpass) < 32) {
      		for ($i = strlen($airqpass); $i < 32; $i++) {
      			$airqpass = $airqpass . '0';
      		}
      	} else {
      		if (strlen($airqpass) > 32) {
      			$airqpass = substr($airqpass,0,32);
      		}
      	}
      
      	$key = utf8_encode ($airqpass);
      //	$cyphertext = base64_decode ($msgb64);
      //	But with verly long messages there could be some problems in base64_decode
      	$decoded = "";
      	for ($i=0; $i < ceil(strlen($msgb64)/256); $i++)
      	   $decoded = $decoded . base64_decode(substr($msgb64,$i*256,256));
      	$cyphertext = $decoded;
      
      	$iv = substr($cyphertext,0,16);
      	$cyphertext = substr($cyphertext,16,strlen($cyphertext));
      
      	$decrypted = openssl_decrypt($cyphertext, 'aes-256-cbc', $key, OPENSSL_RAW_DATA, $iv);
      	
      	return utf8_encode($decrypted);
      }
      

      PHP-encrypt:

      function encrypt($msgb64,$password)
      {
          $airqpass = $password;
      	if (strlen($airqpass) < 32) {
      		for ($i = strlen($airqpass); $i < 32; $i++) {
      			$airqpass = $airqpass . '0';
      		}
      	} else {
      		if (strlen($airqpass) > 32) {
      			$airqpass = substr($airqpass,0,32);
      		}
      	}
      
      	$key = utf8_encode ($airqpass);
      	
      	$iv = substr(openssl_random_pseudo_bytes(32),0,16);
      
      	$encrypted = openssl_encrypt($msgb64, 'aes-256-cbc', $key, OPENSSL_RAW_DATA, $iv);
      	return base64_encode($iv . $encrypted);
      }
      
      1 Reply Last reply Reply Quote 0
      • T
        tuxbox last edited by

        <klugscheissermodus> 😉 Ja, so ähnlich mache ich es auch. Aber kleiner Tip, mit str_pad(..,32,"0",STR_PAD_RIGHT) kannst Du Dir die eine Schleife sparen und substr braucht den strlen als dritten Parameter nicht für den String nach dem 16ten Zeichen.

        Die openssl_random_pseudo_bytes Länger kann man natürlich auch hart kodiert angeben, aber etwas besser lesbar ist es evtl. mit openssl_cipher_iv_length('AES-256-CBC').

        Wie ich bei Dir sehe hast du das Padding und Unpadding von den Beispielen nicht mit eingebaut..?
        </klugscheiserfragenmodus>

        1 Reply Last reply Reply Quote 0
        • Referenced by  M Micha 
        • 1 / 1
        • First post
          Last post



         |   |   | 

        © 2023 air-Q