-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
executable file
·41 lines (33 loc) · 1.31 KB
/
index.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
41
<?php
function encryptString($plainText, $key) {
$ivSize = openssl_cipher_iv_length('aes-256-cbc');
$iv = openssl_random_pseudo_bytes($ivSize, $isStrong);
if (!$isStrong) {
throw new Exception('IV generation failed');
}
$cipherText = openssl_encrypt($plainText, 'aes-256-cbc', $key, OPENSSL_RAW_DATA, $iv);
return base64_encode($iv . $cipherText);
}
function stringToGoString($goStr, $string) {
$strChar = str_split($string);
$c = FFI::new('char[' . count($strChar) . ']', false);
foreach ($strChar as $i => $char) {
$c[$i] = $char;
}
$goStr->p = FFI::cast(FFI::type('char *'), $c);
$goStr->n = count($strChar);
return $goStr;
}
// Example usage:
$plaintextString = "I am here to solve the problem.";
$encryptionKey = "1234567890abcdef1234567890abcdef";
$encryptedString = encryptString($plaintextString, $encryptionKey);
echo "Encrypted String: " . $encryptedString. "\n";
$ffi = FFI::cdef("
typedef struct { const char *p; long n; } GoString;
GoString DecryptString(GoString encryptedBase64, GoString key);
", __DIR__ . "/decrypt.so");
$encryptedString = stringToGoString($ffi->new("GoString"), $encryptedString);
$encryptionKey = stringToGoString($ffi->new("GoString"), $encryptionKey);
print_r ($ffi->DecryptString($encryptedString , $encryptionKey))
?>