main.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/*
Compatible Macro Scheduler (www.mjtnet.com) code:

  Let>AES_ALG=AES_256_CBC
  AESEncrypt>hello world,mypassword,ENCRYPT,result
  AESEncrypt>result,mypassword,DECRYPT,original

*/

$data = "hello world";

$method = 'AES-256-CBC';

// simple password hash
$password = 'mypassword';
$key = hex2bin(substr(hash('sha256', $password),0,64));

echo "Method: " . $method . "\n";
$encrypted = encrypt($data, $key, $method);
echo "Encrypted: ". $encrypted . "\n";
$decrypted = decrypt($encrypted, $key, $method);
echo "Decrypted: ".  $decrypted . "\n"; // plain text

function encrypt(string $data, string $key, string $method): string
{
    $iv = "0000000000000000";
    $encrypted = openssl_encrypt($data, $method, $key, OPENSSL_RAW_DATA, $iv);
    $encrypted = base64_encode($encrypted);

    return $encrypted;
}

function decrypt(string $data, string $key, string $method): string
{
    $data = base64_decode($data);
    $iv = "0000000000000000";
    $data = openssl_decrypt($data, $method, $key, OPENSSL_RAW_DATA,$iv);

    return $data;
}
PHP 7.0.8 (cli) (built: Jun 23 2016 23:39:14) ( NTS ) Copyright (c) 1997-2016 The PHP Group Zend Engine v3.0.0, Copyright (c) 1998-2016 Zend Technologies