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

DonManfred

Expert
Licensed User
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

Last edited:

incendio

Well-Known Member
Licensed User
Thanks for sharing.

Not familiar with base 64, Is the encrypted string only contains chars A-Z, a-z, & 1-9 only and not some funny chars like ,'/ and so on?
 

leitor79

Active Member
Licensed User
Hi DonManfred, thank you for your library!

Could you explain what is "passphrase" and "secret"? I've only used one single password/phrase to crypt and decrypt...

Thank you!
 

swissmade

Well-Known Member
Licensed User
Just needed this,

Thanks Don
 

DonManfred

Expert
Licensed User
can i use it for encrypting files ? images for example ?
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
 

Devv

Active Member
Licensed 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

thanks a lot for the new update.
for a 1gb ram phone what is the maximum file i could encrypt ? about how much ?
 

Devv

Active Member
Licensed User
It depends on the free ram. I would say a few mb should be possible. I tried with an 4mb picture with success
B4X:
crypt.Initialize("fedcba9876543210","0123456789abcdef")

Dim fsize As Int = File.Size (File.DirRootExternal,"colored.jpg")
  Log (fsize)
  ' proves that the file exists
  Dim data() As Byte = Bit.InputStreamToBytes(File.OpenInput(File.DirRootExternal,"colored.jpg"))
    Dim t As String = crypt.encrypt2(data) ' e
    Log(t)
ok i had crypted my file sucessfully with the code you provided how can i write write the encrypted file to the sdcard ?
 

Kwame Twum

Active Member
Licensed User
Hello @DonManfred and everyone else, pls after updating to the latest version of Decrypter, I keep getting the following whenever I try decrypting data:
B4X:
java.lang.Exception: [decrypt] error:06065064:digital envelope routines:EVP_DecryptFinal_ex:bad decrypt
can anyone please tell me where the problem lies?
 
Last edited:

ivanomonti

Well-Known Member
Licensed User
I have the problem that when I try to modify data array, app closes without giving me a crash or Catch, therefore I can not understand where wrong

  • The image is generated
  • It is saved
  • date, is populated by the reading of the same
  • but when I get here ---> = XImage cp.encrypt2 (date) <--- get the error without trace.


B4X:
Sub CenterBitmap(iv As View, bmpT As Bitmap)
   
    imv.Bitmap = Null
   
    Private bmp As Bitmap = bmpT
    Private cvs As Canvas
   
    cvs.Initialize(iv)
   
    Dim rectDest As Rect
    Dim delta As Int
   
    If bmp.Width / bmp.Height > iv.Width / iv.Height Then
        delta = (iv.Height - bmp.Height / bmp.Width * iv.Width) / 2
        rectDest.Initialize(0, 0,iv.Width, iv.Height - delta)
        ' rectDest.Initialize(0, delta,iv.Width, iv.Height - delta) ' centra in verticale e orizzontale
    Else
        delta = (iv.Width - bmp.Width / bmp.Height * iv.Height) / 2
        rectDest.Initialize(delta, 0, iv.Width - delta, iv.Height)
    End If
   
    imv.Height = rectDest.Bottom
   
    sv.Panel.Height = imv.Top + imv.Height + 10dip
   
   
    Dim re As Rect
    re.Initialize(0,0,iv.Width,iv.Height)
    cvs.DrawRect(re,Colors.White,True,0)
    cvs.DrawBitmap(bmp, Null, rectDest)
   
    ' intestazione immagine
   
    Dim l,t,w,h As Float
    l = (iv.Width-0dip)/2
    t = (iv.Height-0dip)/2
    w = 48dip
    h = 48dip
    re.Initialize(0,0,48dip,48dip)
    rectDest.Initialize(l,t,w+l,h+t)
    cvs.DrawText("Costola",l,t,Typeface.DEFAULT_BOLD,(42dip*90/100)/2.5,Colors.ARGB(32,255,255,255),"CENTER")
    iv.Invalidate
   
    ' salvo immagine su dispositivo
   
    If File.Exists(File.DirRootExternal,"DCIM/COSTOLA") = False Then
        File.MakeDir(File.DirRootExternal,"DCIM/COSTOLA")
    End If
   
    ' creo parametri per salvataggio file image
   
    Dim filename As String = "imagetemp.jpg"
    Dim dir As String = File.Combine(File.DirRootExternal,"DCIM/COSTOLA/")
   
    ' scrivo image nella folder
   
    Dim out As OutputStream
    out = File.OpenOutput(dir,filename,True)
    cvs.Bitmap.WriteToStream(out,100,"JPEG")
   
    If File.Exists(dir,filename) = True Then
   
        Dim fsize As Int = File.Size(dir,filename)
         
        If fsize > 0 Then
       
            Dim data() As Byte = Bit.InputStreamToBytes(File.OpenInput(dir,filename))
       
            Dim cp As Crypter
            cp.Initialize("fedcba9876543210","0123456789abcdef")
       
        Try
            ximage = cp.encrypt2(data) ' error
        Catch
            ToastMessageShow("Errore di scrittura",True)
        End Try
       
        Else
            ToastMessageShow("Il file è vuoto",True)
        End If
   
    End If
   
   
End Sub
 

swissmade

Well-Known Member
Licensed User
I think you have to check if no value is 0. 100 / 0 go wrong.
Also all the Functions need a Try Catch

I hope it helps
 

gameone

Member
Licensed User
Hi, i guess i am stupid.
I don't know much PHP PDO so i guess it's my code error i do in php:

<?php
include("MCrypt.php")
$AndroidValue= mysql_real_escape_string($_GET['AndroidValue']);
$AndroidValue = decrypt($AndroidValue);

?>


But i got this error:
Fatal error: Call to undefined function decrypt() in file.php on line 24


Of course Mcrypt is installed on my server (i have checked it though phpinfo() )
 
Last edited:
Top