@MarcusTettmar/

php_aes_256_cbc_1

PHP (Legacy)

PHP AES_256_CBC example with SHA256 key and IV.

fork
loading

This Plugin Crashed!

Error: Error: must not create an existing file {"type":"CREATE_FILE","wid":"0.059007591442037155","path":"main.php","file":{"path":"main.php","content":{"asEncoding":{"base64":"LyoKQ29tcGF0aWJsZSBNYWNybyBTY2hlZHVsZXIgKHd3dy5tanRuZXQuY29tKSBjb2RlOgoKICBMZXQ+QUVTX0FMRz1BRVNfMjU2X0NCQwogIEFFU0VuY3J5cHQ+aGVsbG8gd29ybGQsbXlwYXNzd29yZCxFTkNSWVBULHJlc3VsdAogIEFFU0VuY3J5cHQ+cmVzdWx0LG15cGFzc3dvcmQsREVDUllQVCxvcmlnaW5hbAoKKi8KCiRkYXRhID0gImhlbGxvIHdvcmxkIjsKCiRtZXRob2QgPSAnQUVTLTI1Ni1DQkMnOwoKLy8gc2ltcGxlIHBhc3N3b3JkIGhhc2gKJHBhc3N3b3JkID0gJ215cGFzc3dvcmQnOwoka2V5ID0gaGV4MmJpbihzdWJzdHIoaGFzaCgnc2hhMjU2JywgJHBhc3N3b3JkKSwwLDY0KSk7CgplY2hvICJNZXRob2Q6ICIgLiAkbWV0aG9kIC4gIlxuIjsKJGVuY3J5cHRlZCA9IGVuY3J5cHQoJGRhdGEsICRrZXksICRtZXRob2QpOwplY2hvICJFbmNyeXB0ZWQ6ICIuICRlbmNyeXB0ZWQgLiAiXG4iOwokZGVjcnlwdGVkID0gZGVjcnlwdCgkZW5jcnlwdGVkLCAka2V5LCAkbWV0aG9kKTsKZWNobyAiRGVjcnlwdGVkOiAiLiAgJGRlY3J5cHRlZCAuICJcbiI7IC8vIHBsYWluIHRleHQKCmZ1bmN0aW9uIGVuY3J5cHQoc3RyaW5nICRkYXRhLCBzdHJpbmcgJGtleSwgc3RyaW5nICRtZXRob2QpOiBzdHJpbmcKewogICAgJGl2ID0gIjAwMDAwMDAwMDAwMDAwMDAiOwogICAgJGVuY3J5cHRlZCA9IG9wZW5zc2xfZW5jcnlwdCgkZGF0YSwgJG1ldGhvZCwgJGtleSwgT1BFTlNTTF9SQVdfREFUQSwgJGl2KTsKICAgICRlbmNyeXB0ZWQgPSBiYXNlNjRfZW5jb2RlKCRlbmNyeXB0ZWQpOwoKICAgIHJldHVybiAkZW5jcnlwdGVkOwp9CgpmdW5jdGlvbiBkZWNyeXB0KHN0cmluZyAkZGF0YSwgc3RyaW5nICRrZXksIHN0cmluZyAkbWV0aG9kKTogc3RyaW5nCnsKICAgICRkYXRhID0gYmFzZTY0X2RlY29kZSgkZGF0YSk7CiAgICAkaXYgPSAiMDAwMDAwMDAwMDAwMDAwMCI7CiAgICAkZGF0YSA9IG9wZW5zc2xfZGVjcnlwdCgkZGF0YSwgJG1ldGhvZCwgJGtleSwgT1BFTlNTTF9SQVdfREFUQSwkaXYpOwoKICAgIHJldHVybiAkZGF0YTsKfQ=="},"asBuffer":null},"loaded":true}}
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 (Legacy: use the following for new features: Command-line PHP: https://repl.it/languages/php_cli PHP Web Server: https://repl.it/languages/php7