01.
<?php
02.
03.
$gKey
=
'welcometoapicodesdotcomthisiskey'
;
04.
05.
function
decode(
$pData
)
06.
{
07.
global
$gKey
;
08.
09.
$lData
=
str_replace
(
' '
,
'+'
,
$pData
);
10.
11.
$lBase64Decoded_Payload
=
base64_decode
(
$lData
);
12.
13.
$lEncrypted_PlainText
=
substr
(
$lBase64Decoded_Payload
, 16);
14.
15.
$lIV
=
substr
(
$lBase64Decoded_Payload
, 0, 16);
16.
17.
$lDecrypted_PlainText
= mcrypt_decrypt(MCRYPT_RIJNDAEL_128,
$gKey
,
$lEncrypted_PlainText
, MCRYPT_MODE_CBC,
$lIV
);
18.
19.
$lBase64Decoded_PlainText
=
base64_decode
(
$lDecrypted_PlainText
);
20.
21.
return
$lBase64Decoded_PlainText
;
22.
}
23.
24.
function
encode(
$pData
)
25.
{
26.
global
$gKey
;
27.
28.
$lBase64Encoded_PlainText
=
base64_encode
(
$pData
);
29.
30.
$lIV
= GenerateIV();
31.
32.
$lEncrypted_PlainText
= mcrypt_encrypt(MCRYPT_RIJNDAEL_128,
$gKey
,
$lBase64Encoded_PlainText
, MCRYPT_MODE_CBC,
$lIV
);
33.
34.
$lPayload
=
$lIV
.
$lEncrypted_PlainText
;
35.
36.
$lBase64Encoded_Payload
=
base64_encode
(
$lPayload
);
37.
38.
return
$lBase64Encoded_Payload
;
39.
}
40.
41.
function
GenerateIV()
42.
{
43.
$lIV
= mcrypt_create_iv(16, MCRYPT_DEV_URANDOM);
44.
while
(
strlen
(
$lIV
) < 16)
45.
{
46.
$lIV
.=
"\0"
;
47.
}
48.
return
$lIV
;
49.
}
50.
51.
?>