<?php
	/*  PHP-mcrypt Decrypt Test 1.0.2 */
	//
	//
	// 2020-0609 for Troi Encryptor Plug-in 6.0 for FileMaker Pro 19
	// by Troi Automatisering
	//
	// This example assumes to be run on OS X (on Windows it needs to be modified).
	// You also need to export the data from FileMaker with the EncryptAES.fmp12 example (the export to desktop button)
	//
	// INSTRUCTIONS:
	// - In FileMaker Pro open the EncryptAES.fmp12 example.
	// - Go to the tab: encrypt with keys.
	// - Generate a key and encrypt some data: use AES-256 and Zero Padding
	// - Export the data from FileMaker with the EncryptAES.fmp12 example (use the button : export to desktop)
	// 
	// - Install php and mcrypt (if not installed already).
	// - Place this php example script on your Desktop.
	// - Open terminal.app
	// - Run this php script with:
	//		"php -f ~/Desktop/decrypt-from-encryptor-AES256.php"  (without the quotes)
	//
	// You can tweak some of the variables to vary the output you get.
	//
    echo "\n------------------------------------------------------\n";
    echo "TROI test of Troi Encryptor Plug-in 6.0 generated data \n";
    echo "------------------------------------------------------\n";
    echo "Using as input a text file, which must be encrypted with AES-256; Zero Padding.\n";
    echo "NOTE this is the same as Rijndael-128 with key size 256.\n";
    echo "\n";

	
	// Calc the path to the users home folder:
	$home_path = getenv('HOME'); // if this does not work fill in next line
	//$home_path = '/Users/YOUR_USER_NAME';


	// First get the file with the Encrypted_data:
	$InFileDataPath = 			$home_path.'/Desktop/Encrypted_data.txt';
	
	// Get the file with the extra Encrypted_info.
	//
	// This info also contains the password: for testing only!!!
	// NEVER ADD THIS IN REAL USES!!!
	// in real applications the receiving party will have either the password or the key.

	$InFileExtraInfoPath = 	$home_path.'/Desktop/Encrypted_info.txt';
	

	// Get Data file content as string
	$InFileExtraInfo = file_get_contents($InFileExtraInfoPath);
   	
	$ExtraInfoLen = strlen($InFileExtraInfo); // get string length
	if($ExtraInfoLen > 0 )
	{
	    echo "Found the extra info file.\n";
	    echo "NOTE This also contains the password, for testing only: NEVER ADD THIS IN REAL IMPLEMENTATIONS!\n\n";

		// Extract info from the extra info file: //
		$searchstr = 'Initialization Vector: "';
		$pos = strpos($InFileExtraInfo, $searchstr);
		$ivBase64 = substr($InFileExtraInfo, $pos+strlen($searchstr), strpos($InFileExtraInfo, '"', $pos+strlen($searchstr)) - $pos - strlen($searchstr));
		//
		$searchstr = 'Passphrase used: "';
		$pos = strpos($InFileExtraInfo, $searchstr);
		$password = substr($InFileExtraInfo, $pos+strlen($searchstr), strpos($InFileExtraInfo, '"', $pos+strlen($searchstr)) - $pos - strlen($searchstr));
		//
		$searchstr = 'Salt used: "';
		$pos = strpos($InFileExtraInfo, $searchstr);
		$salt = substr($InFileExtraInfo, $pos+strlen($searchstr), strpos($InFileExtraInfo, '"', $pos+strlen($searchstr)) - $pos - strlen($searchstr));
		
		echo "Imported: ivBase64 = '" . trim($ivBase64) . "'\n";
		echo "Imported: password = '" . trim($password) . "'\n";
		echo "Imported: salt = '" . trim($salt) . "'\n";
		$useOwnPassword = 1;
  	}
  	else
  	{
		// extra info file not found: you have to fill this in manually:
		// fill in your key etc. as used in the FileMaker example here:
		$keyBase64 				= '<your EncryptionKey here>';  // KEEP Secret!!
		$ivBase64 				= '<your InitializationVector here>';  // change every time
		$salt 					= '<your Salt here>';

		$useOwnPassword 		= 0;
		if($useOwnPassword)
			$password = 'very secret key';
			//$password = 'tester';
		else
			$password = '';
  	}

	// * get data file content as string
	$InFileBase64 = file_get_contents($InFileDataPath);
  	$InFileData = base64_decode($InFileBase64);
   	// get string length
	$FileLen = strlen($InFileBase64);
	echo "\nFound the data file. \n";

   	echo "File path = " . trim($InFileDataPath) . "\n";
   	echo "File len = " . trim($FileLen) . "\n";
	if($FileLen == 0)
	{
	 	exit;
	}
    $InFileBase64Clean = str_replace("\r", "\n", $InFileBase64);
   	echo "File data (Base64) = \n" . trim($InFileBase64Clean) . "\n";
    echo "\n";

	/* Open the cipher */
	echo "Decrypting the data.\n";
	$td = mcrypt_module_open('rijndael-128', '', 'cbc', '');

    $ivSize = mcrypt_enc_get_iv_size($td);
    $blockSize = mcrypt_enc_get_block_size($td);
    $ks = mcrypt_enc_get_key_size($td);

  	echo "Block size = " . trim($blockSize) . "\n";
  	echo "IV size = " . trim($ivSize) . "\n";
  	echo "Key size = " . trim($ks) . "\n";

    if($useOwnPassword)
    {
		echo "We use own password + salt to generate the key.\n";
		/* Create key */
		$sha1_result = sha1($salt .$password);
		echo "Used sha1 = " . $sha1_result . "\n";
		$key = substr($sha1_result, 0, $ks);
		$keyBase64 = base64_encode($key);
    }
    
 	echo "Used Key (Base64) = " . trim($keyBase64) . "\n";
 	echo "Used IV (Base64) = " . trim($ivBase64) . "\n";
   	$key = base64_decode($keyBase64);
   	$iv  = base64_decode($ivBase64);

	//echo "Key = " . trim($key) . "\n";  	// you can uncomment this.
	//echo "IV  = " . trim($iv) . "\n"; 	// you can uncomment this.
    
	$encrypted = $InFileData;
    // Initialize encryption module for decryption
    mcrypt_generic_init($td, $key, $iv);

    // Decrypt encrypted string
    $decrypted = mdecrypt_generic($td, $encrypted);

    // Terminate decryption handle and close module
    mcrypt_generic_deinit($td);
    mcrypt_module_close($td);
    
  	//  echo "Decrypted  = " . trim($decrypted) . "\n\n";
    $clean_decrypted = str_replace("\r", "\n", $decrypted); // replace \r with \n
	echo "The decrypted data (clean): \n";
    echo "------------------------------------------------------\n";
   	echo trim($clean_decrypted);
    echo "\n------------------------------------------------------\n";
    
exit(0);


?>