<?php
	/*  PHP7 Encrypt Test 1.0.1 */
	//
	//
	// 2020-0609 for Troi Encryptor Plug-in 6.0 for FileMaker Pro 19
	// by Troi Automatisering  https://www.troi.com
	//
	// 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:
	//
	// Install php7 (if not installed already).
	//   This script was tested with: PHP 7.1.23 (cli) (built: Nov  7 2018 18:20:35)
	//   Note that the OpenSSL functions may also work with PHP 5.5 and later (we have not tested this).
	//
	// Encrypt some test data with this example file:
	// - Place this php example script on your Desktop.
	// - Open terminal.app
	// - Run this php script with:
	//		"php -f <path to the php file>"
	//   so if you have not renamed this example file it will be:
	//		"php -f ~/Desktop/php7-encrypt-for-encryptor-AES256.php"  (without the quotes)
	//
	// the encrypted data is written to this file on the desktop: "PHP_Encrypted_data.txt"
	// In FileMaker Pro:
	// - In FileMaker Pro open the OpenSSL.fmp12 example.
	// - Go to the tab: "Decrypt from PHP OpenSSL".
	// - Import the just encrypted data, or paste it into the field: "EncryptedText from PHP OpenSSL".
	// - Enter the password and decrypt the data with the button "Decrypt with AES from OpenSSL".
	//

	// You can tweak some of the variables to vary the output you get.
	//
    echo "\n---------------------------------------------------------------------------------------\n";
    echo "TROI test of php generated encrypted data to be decrypted with Troi Encryptor Plug-in 6.0\n";
    echo "-----------------------------------------------------------------------------------------\n";
    echo "\n";

	// ------------------------------------------------
	// INIT
	// ------------------------------------------------
	// first set the cipher variant we use and settings for the hash

	$cipher 	= 'AES-256-CBC';
	$blockSize 	= 16;
	$keySize 	= 32;

	$hash_algo 			= 'sha1';
	$hash_len   		= 20;
	$hash_iterations 	= 10000;
	
	// In OpenSSL the encrypted string starts with "Salted__" + 8 bytes salt
	$Salted__Str 		= 'Salted__';
	$Salted__StrLen 	= strlen($Salted__Str); // = 8
	$saltLen 			= 8;


	// ------------------------------------------------
	$textToEncrypt 		= "This text was encrypted by PHP7 and will be decrypted by Troi Encryptor plug-in.\nThis text is on a new line.";
	$password 			= 'secretpassword';
	$destFile_FileName 	= 'PHP_Encrypted_data.txt';
	// ------------------------------------------------

	// 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';
	$destFilePath = 	$home_path.'/Desktop/'.$destFile_FileName;

	// ------------------------------------------------
		echo "Generate the key.\n";
	// ------------------------------------------------
	echo "Generating the key with pbkdf2 and iterations = " . $hash_iterations . "\n";

	$salt = openssl_random_pseudo_bytes($saltLen);

 	$ivSize = openssl_cipher_iv_length($cipher);
	$keyAndIVSize = $keySize + $ivSize;
	
	// Generate a key and iv at the same time:
	$keyAndIV = openssl_pbkdf2($password, $salt, $keyAndIVSize, $hash_iterations, $hash_algo);
	$key = substr($keyAndIV, 0, $keySize);
	$iv = substr($keyAndIV, $keySize, $ivSize);

	$saltBase64 = base64_encode($salt);
	$keyBase64 = base64_encode($key);

//	echo "   Using Salt (Base64)    = " . trim($saltBase64) . "\n";
	echo "   Using Salt (hex)    = " . trim(bin2hex($salt)) . "\n";
 //	echo "   Generated Key (Base64) = " . trim($keyBase64) . "\n";
 	echo "   Generated Key (hex) = " . trim(bin2hex($key)) . "\n";

	$ivBase64 = base64_encode($iv);
 //	echo "   Using IV (Base64) = " . trim($ivBase64) . "\n";
 	echo "   Using IV (hex) = " . trim(bin2hex($iv)) . "\n";
  	echo "   Block size = " . trim($blockSize) . "\n";
  	echo "   IV size = " . trim($ivSize) . "\n";
  	echo "   Key size = " . trim($keySize) . "\n";

 	// ------------------------------------------------
		echo "\nEncrypting the data now.\n";
	// ------------------------------------------------
	$ciphertext = openssl_encrypt($textToEncrypt, $cipher, $key, $options=OPENSSL_RAW_DATA, $iv);
	$ciphertextBase64 = base64_encode($ciphertext);
	echo "The encrypted data (Base64), no salt added yet: \n";
    echo "------------------------------------------------------\n";
   	echo trim($ciphertextBase64);
    echo "\n------------------------------------------------------\n";

	// ------------------------------------------------
		echo "\nAdding salt string to the result.\n";
	// ------------------------------------------------
	$saltAndCiphertext = $Salted__Str . $salt. $ciphertext;
	$ciphertextFormattedBase64 = base64_encode($saltAndCiphertext);

	//echo "Decrypted  = " . trim($decrypted) . "\n\n";
  //  $clean_decrypted = str_replace("\r", "\n", $decrypted); // replace \r of FMP with \n
	echo "The encrypted data (formatted, Base64): \n";
    echo "------------------------------------------------------\n";
   	echo trim($ciphertextFormattedBase64);
    echo "\n------------------------------------------------------\n";

	// write the encrypted data to a file:
	if( file_put_contents($destFilePath , $ciphertextFormattedBase64))
	{
		echo "Written the encrypted data into this file: ".$destFilePath. "\n";
	}
	else
	    echo "Error writing the encrypted data into a file.\n";

	echo "\nEnd of script\n\n";
	exit(0);


?>
