B4A Library Decrypter - De-/Encrypt String to base64 (including compatible php code to de/Encrypt)

Decrypter
Version:
1.05

This Library can be used to
- Encrypt a string to a Hex-View of the encrypted string.. The resulting string only contains the character 0 to 9 and A to F. So it is save to be used in any httputils-call-parameter for example.
- Decrypt a String to its Original String. The string to decode must be the output of the Encrypt-Method.

For the php-side you can use the attached php-class to En- and de-crypt strings which are encrypted by the b4a-library (or by the php itself)

The Library is a wrapper for this Github-Project.

Please note that the Key must be 16 Characters long! Or maybe a multiple of 16. Just want to warn about an Exception i got while developing the wrapper


Decrypter
Version:
1.05

  • Methods:
    • bytesToHex (buf() As Byte) As String
    • decrypt (code As String) As Byte[]
    • encrypt (text As String) As Byte[]
    • encrypt2 (toencrypt() As Byte) As Byte[]
    • hexToBytes (str As String) As Byte[]
  • Crypter
    Methods:
    • Initialize (passphrase As String, Secret As String)
    • decrypt (encrypted As String) As String
      Decode a encrypted base64 string and return the decrypted string
    • encrypt (toEncrypt As String) As String
      Encrypt a string and give back an encrypted and base64 encoded string
    • encrypt2 (toEncrypt() As Byte) As String

This library is Donationware. You can download the library, you can test the library. But if you want to USE the library in your App you need to Donate for it.
Please click here to donate (You can donate any amount you want to donate for the library (or my work) :)




Please note that this library is Compatible with B4J too!
 

Attachments

  • DecrypterExample.zip
    11 KB · Views: 1,108
  • libDecrypterV1.0.1.zip
    3.5 KB · Views: 767
  • MCryptPHP.zip
    671 bytes · Views: 902
  • libDecrypterV1.0.5.zip
    3.6 KB · Views: 868
  • DecrypterV1.06.zip
    3.7 KB · Views: 874
Last edited:

DonManfred

Expert
Licensed User
Longtime User
include("MCrypt.php")
$AndroidValue= mysql_real_escape_string($_GET['AndroidValue']);
$AndroidValue = decrypt($AndroidValue);
i just see the problem. mcrypt is a class. You need to initialize a new instance of the class and use the method decrypt

HOW TO USE IT (PHP)

See here:
PHP:
include("MCrypt.php");
$mcrypt = new MCrypt();
/* Encrypt */
$encrypted = $mcrypt->encrypt("Text to encrypt");
/* Decrypt */
$decrypted = $mcrypt->decrypt($encrypted);
 

gameone

Member
Licensed User
Oh okay, yeah i coded alot into PHP but no with PDO.
Now it's work thanks u, time to learn too PDO/Class related PHP stuff.

Thanks for your help & for your library, you make a good stuff ;)
 

gameone

Member
Licensed User
Well i go back here because they are some problem.
This look that your Library don't support unicode.

If i put non latin character, then he will be broken.
Any idea ?


-------------------------------------------------------------

EDIT: Well see that he work too with chinese character, but i don't know why when i use it
for send Data over HTTP (Using: "InitializeGet") and your PHP Code.
Then unicode is broken.

Any idea ?
 
Last edited:

gameone

Member
Licensed User
Well so i checked abit where was the issue and look like it's when PHP Code is doing decrypt stuff:
DonManfred 123 #@ Ğ 假借字 ----> A Get value on PHP without crypting (using echo on php)
234e07f42bd9740e196e0e18e53ecd78e42a951bd26c3fa72d7283c69fafe3b605ded2078eb6379ee5bff96ff59f612c -> our Value with default key encryption
DonManfred 123 #@ Ğ å‡å€Ÿå— ----> After Decrypted on PHP.


So look PHP Decrypt stuff don't work with UniCode or i doing something wrong, will check tomorrow if i can fix myself this issue.
 

Argonc

Member
Licensed User
Longtime User
Hi, I have this ERRROR: <b>Fatal error</b>: Call to undefined function mcrypt_module_open() in <b>MCrypt.php</b> on line <b>24</b><br />
So I don't have the mcrypt library installed? I have PHP 5.6 server and I am on shared hosting, so I must ask me provider to install mcrypt library?
 

park jae hyun

Member
Licensed User
Longtime User
See the updated Version 1.0.5 in post #1

Yes, it is possible.

See this example code to do this

B4X:
Dim fsize As Int = File.Size (File.Combine(File.DirRootExternal,"Download/"),"filetoencrypt.mp3")
  Log (fsize)
  ' proves that the file exists
  Dim data() As Byte = Bit.InputStreamToBytes(File.OpenInput(File.Combine(File.DirRootExternal,"Download/"),"09 - Dire Straits - Brothers in Arms.mp3"))
    Dim t As String = crypt.encrypt2(data) ' e
    Log(t)

Please note that encrypting a file will need a lot of memory as the hole file will be hold in memory. Dont try to encrypt large files! You will run into OOM problems :D



How do I decrypt ??
 

Valeriy Lakhtin

Member
Licensed User
Hi! I'm trying to use the CRYPT library at B4A, but it does not work without B4XEncryption. I installed B4XEncryption. But all the same error occurs
B4X:
B4A version: 5.80
Parsing code.    (0.10s)
Compiling code.    (0.10s)
Compiling layouts code.    (0.00s)
Generating R file.    (0.03s)
Compiling generated Java code.    Error
Cannot find: D:\ANDROID\Basic4android\libraries\com.android.support:support-v4.jar
I need to encrypt and decrypt XML file. I can hope that the crypts Lib will do the job?
 

Valeriy Lakhtin

Member
Licensed User
Thank you so much , I found a mistake now everything works fine.
In the initialization string two words must necessarily be the same and mirror-symmetric?
 

Cassie

Member
Licensed User
Longtime User
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;
}

}


 

Attachments

  • Screenshot_2016-11-23-14-24-50.jpg
    Screenshot_2016-11-23-14-24-50.jpg
    394.1 KB · Views: 500
  • 31633.jpg
    31633.jpg
    73.8 KB · Views: 513
Last edited:

DonManfred

Expert
Licensed User
Longtime User
What could be the possible issue since the output of the PHP encryption is lacking.
You are using poststring but the php does not accept this.
Change
B4X:
httpServer.Initialize(JobName, Me)
teststring = crypt.encrypt(teststring )
httpServer.PostString(StrApiServer,"action=test_decrypt&encrypted_text=" & teststring)
to
B4X:
httpServer.Initialize(JobName, Me)
teststring = crypt.encrypt(teststring )
httpServer.Download2(StrApiServer,Array as String("action","test_decrypt","encrypted_text",teststring))

And you need to change
PHP:
$action = $_POST["action"]
to
PHP:
$action = $_GET["action"]
as download2 uses GET
 
Last edited:

Cassie

Member
Licensed User
Longtime User
You are using poststring but the php does not accept this.
Change
B4X:
httpServer.Initialize(JobName, Me)
teststring = crypt.encrypt(teststring )
httpServer.PostString(StrApiServer,"action=test_decrypt&encrypted_text=" & teststring)
to
B4X:
httpServer.Initialize(JobName, Me)
teststring = crypt.encrypt(teststring )
httpServer.Download2(StrApiServer,Array as String("action","test_decrypt","encrypted_text",teststring))

And you need to change
PHP:
$action = $_POST["action"]
to
PHP:
$action = $_GET["action"]
as download2 uses GET

Hi thanks for the reply, I think there is no problem with $_POST and $_GET method since the PHP accepts the passed values. What the difference is that the output of this two :

PHP

$myClass = new mcrypt();
$NewCryptedText = $myClass->encrypt("Hello|",false);

Output is : 8fd711a3eb3916a3a5729a56d3628a5b (32 Characters)

While the output in

B4a

Dim crypt As Crypter
crypt.Initialize("fedcba9876543210","0123456789abcdef")
Dim TextToCrypt As String = "Hello|"
NewCryptedText = crypt.encrypt(TextToCrypt)

Output is : 8fd711a3eb3916a3a5729a56d3628a5bab502c914ea0e69c91b3dcbc16af982e (64 Characters)

It means that the function in B4a library output a 36 characters while in PHP function outputs only a 32 character.

I use same value "Hello|" to be encrypted in both PHP and B4a Decrypter Library.

Thank you once again.
 

Noelkman

Member
Licensed User
Longtime User
Hi thanks for the reply, I think there is no problem with $_POST and $_GET method since the PHP accepts the passed values. What the difference is that the output of this two :

PHP

$myClass = new mcrypt();
$NewCryptedText = $myClass->encrypt("Hello|",false);

Output is : 8fd711a3eb3916a3a5729a56d3628a5b (32 Characters)

While the output in

B4a

Dim crypt As Crypter
crypt.Initialize("fedcba9876543210","0123456789abcdef")
Dim TextToCrypt As String = "Hello|"
NewCryptedText = crypt.encrypt(TextToCrypt)

Output is : 8fd711a3eb3916a3a5729a56d3628a5bab502c914ea0e69c91b3dcbc16af982e (64 Characters)

It means that the function in B4a library output a 36 characters while in PHP function outputs only a 32 character.

I use same value "Hello|" to be encrypted in both PHP and B4a Decrypter Library.

Thank you once again.


Hi there,
is there any progress on this I'm having the the same issue.

Same conditions on both sides App and PHP (e.g. IV and KEY) but the result on either side is totally different.

Running the App on Nougat 7.0 and IIS is a 6.5

Thanks.
 

Cassie

Member
Licensed User
Longtime User
Hi Noel,

I don't have any updates on this yet, will wait until the someone could have any issues like this can post a solution.

The library is very useful and I would like to donate for it but it seems I'm having a hard time pointing what really the issue which affects the output.

Hi there,
is there any progress on this I'm having the the same issue.

Same conditions on both sides App and PHP (e.g. IV and KEY) but the result on either side is totally different.

Running the App on Nougat 7.0 and IIS is a 6.5

Thanks.
 

DonManfred

Expert
Licensed User
Longtime User
I don't have any updates on this yet, will wait until the someone could have any issues like this can post a solution.
Try V1.06 in post #1

http://www.basic4android.de/crypt.php
will output: Hello| = 8fd711a3eb3916a3a5729a56d3628a5b

The output from b4a is
B4X:
    crypt.Initialize("fedcba9876543210","0123456789abcdef")
    Dim teststring As String = "Hello|"
    Dim crypted As String = crypt.encrypt(teststring)
    Log(crypted)

** Activity (main) Create, isFirst = true **
8fd711a3eb3916a3a5729a56d3628a5b
Hello|

PHP:
<?
include_once './MCrypt.php';
$mcrypt = new MCrypt();
$action = $_GET["action"];
$NewCryptedText = bin2hex ( $mcrypt->encrypt("Hello|",true));
echo "Hello| = " . $NewCryptedText."<br />";
$Text = $mcrypt->decrypt("8fd711a3eb3916a3a5729a56d3628a5b",false);
echo "Decrypted: ".$Text;
?>
 
Last edited:

Cassie

Member
Licensed User
Longtime User
Try V1.06 in post #1

http://www.basic4android.de/crypt.php
will output: Hello| = 8fd711a3eb3916a3a5729a56d3628a5b

The output from b4a is
B4X:
    crypt.Initialize("fedcba9876543210","0123456789abcdef")
    Dim teststring As String = "Hello|"
    Dim crypted As String = crypt.encrypt(teststring)
    Log(crypted)



PHP:
<?
include_once './MCrypt.php';
$mcrypt = new MCrypt();
$action = $_GET["action"];
$NewCryptedText = bin2hex ( $mcrypt->encrypt("Hello|",true));
echo "Hello| = " . $NewCryptedText."<br />";
$Text = $mcrypt->decrypt("8fd711a3eb3916a3a5729a56d3628a5b",false);
echo "Decrypted: ".$Text;
?>
Hi Manfred,

Thanks for the reply, I'll work it as soon as I have free time.

Thanks for the reply.
 
Top