<?php
$aes256 = file_get_contents("php://input");
//print "Decrypted: ".AES256_Decrypt($aes256, hex2bin(hash('sha256',"Secret"))); //just for a test
print AES256_Encrypt("Hi from PHP", hex2bin(hash('sha256',"Secret")));
function AES256_Decrypt($string, $pw)
{
$dec = false;
$encrypt_method = "AES-256-CBC";
$total=base64_decode($string);
$id=substr($total, 0, 8);
$salt=substr($total, 8, 32);
$iv=substr($total, 40, 16);
$string=Base64_Encode(substr($total, 56, strlen($total)-56));
$dec = openssl_decrypt($string, $encrypt_method, $pw, 0,$iv);
return $dec;
}
function AES256_Encrypt($string, $pw)
{
$encrypt_method = "AES-256-CBC";
$id=openssl_random_pseudo_bytes(8, $securityok);
$IV=openssl_random_pseudo_bytes(16, $securityok);
$Salt=openssl_random_pseudo_bytes(32, $securityok);
$enc = openssl_encrypt($string, $encrypt_method, $pw, 0,$IV);
$enc=Base64_encode($id.$Salt.$IV.Base64_decode($enc));
return $enc;
}
?>