Hello Manfred,
Thank you for replying.
Here is what I found to be inconsistent.
B4a Code
Dim crypt AsCrypter
crypt.Initialize("fedcba9876543210","0123456789abcdef")
Dim teststring AsString = "Hello|"
teststring = crypt.encrypt(teststring)
Msgbox("Hello| = " & teststring,"")
Log("Hello| = " & teststring)
OUTPUT in B4a : Screenshot_2016-11-23-14-24-50.jpg
PHP Code
Test.php
<?php
include_once 'config.php';
include_once 'mcrypt.php';
$action = $_POST["action"];
switch ($action)
{
case "test_decrypt":
$NewCryptedText = $myClass->encrypt("Hello|",false);
print json_encode("Hello| = " . $NewCryptedText);
break;
}
?>
B4a Code that call a PHP to check the encrypted data
Sub Activity_Create(FirstTime AsBoolean)
GetPostTest("test")
EndSub
publicSub GetPostTest(JobName AsString )
Dim httpServer AsHttpJob
Dim StrApiServer AsString
Dim crypt AsCrypter
crypt.Initialize("fedcba9876543210","0123456789abcdef")
Dim teststring AsString = "Hello|"
StrApiServer = StrApiPath & "test.php"
httpServer.Initialize(JobName, Me)
teststring = crypt.encrypt(teststring )
httpServer.PostString(StrApiServer,"action=test_decrypt&encrypted_text=" & teststring)
EndSub
Sub JobDone(Job AsHttpJob)
If Job.Success Then
Dim res AsString
res = Job.GetString
Msgbox(res,"")
Else
ToastMessageShow( Job.ErrorMessage,True)
EndIf
EndSub
OUTPUT in B4a coming from PHP Web Server : 31633.jpg
What could be the possible issue since the output of the PHP encryption is lacking.
Thanks in advance.
BTW, here is the php code MCrypt.php
PHP Code For Encryption I downloaded:
<?php
class mcrypt
{
private $iv = 'fedcba9876543210'; #Same as in JAVA
private $key = '0123456789abcdef'; #Same as in JAVA
function __construct()
{
}
/**
* @param string $str
* @param bool $isBinary whether to encrypt as binary or not. Default is: false
* @return string Encrypted data
*/
function encrypt($str, $isBinary = false)
{
$iv = $this->iv;
$str = $isBinary ? $str : utf8_decode($str);
$td = mcrypt_module_open('rijndael-128', ' ', 'cbc', $iv);
mcrypt_generic_init($td, $this->key, $iv);
$encrypted = mcrypt_generic($td, $str);
mcrypt_generic_deinit($td);
mcrypt_module_close($td);
return $isBinary ? $encrypted : bin2hex($encrypted);
}
/**
* @param string $code
* @param bool $isBinary whether to decrypt as binary or not. Default is: false
* @return string Decrypted data
*/
function decrypt($code, $isBinary = false)
{
$code = $isBinary ? $code : $this->hex2bin($code);
$iv = $this->iv;
$td = mcrypt_module_open('rijndael-128', ' ', 'cbc', $iv);
mcrypt_generic_init($td, $this->key, $iv);
$decrypted = mdecrypt_generic($td, $code);
mcrypt_generic_deinit($td);
mcrypt_module_close($td);
return $isBinary ? trim($decrypted) : utf8_encode(trim($decrypted));
}
protected function hex2bin($hexdata)
{
$bindata = '';
for ($i = 0; $i < strlen($hexdata); $i += 2) {
$bindata .= chr(hexdec(substr($hexdata, $i, 2)));
}
return $bindata;
}
}