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

Discussion in 'Additional libraries, classes and official updates' started by DonManfred, Apr 4, 2015.

  1. DonManfred

    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) :)
    [​IMG]



    Please note that this library is Compatible with B4J too!
     

    Attached Files:

    Last edited: Feb 6, 2017
  2. DonManfred

    DonManfred Expert Licensed User

    @KMatle You can use this in your b4a<->php-tutorials if you want. Could be useful for them i think ;-)
     
  3. DonManfred

    DonManfred Expert Licensed User

  4. incendio

    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?
     
  5. DonManfred

    DonManfred Expert Licensed User

    It is not really base64. It is a hex view of the bytearray. So only 0-9 and a-f are used. Can be perfectly used in any httputils-call as parameter for ex.
     
  6. leitor79

    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!
     
  7. swissmade

    swissmade Active Member Licensed User

    Just needed this,

    Thanks Don
     
  8. Devv

    Devv Active Member Licensed User

    can i use it for encrypting files ? images for example ?
     
  9. grafsoft

    grafsoft Well-Known Member Licensed User

  10. Erel

    Erel Administrator Staff Member Licensed User

    You can use B4XCipher to encrypt / decrypt any data you like.
     
  11. DonManfred

    DonManfred Expert Licensed User

    See the updated Version 1.0.5 in post #1

    Yes, it is possible.

    See this example code to do this

    Code:
    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 likes this.
  12. Devv

    Devv Active Member Licensed User


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

    DonManfred Expert 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
     
    Devv likes this.
  14. Devv

    Devv Active Member Licensed User

    Code:
    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 ?
     
  15. DonManfred

    DonManfred Expert Licensed User

    like you would do it with any other string....

    Open a file, save the string to the file. There are several ways to do this. Search the forum on how to do
     
    Devv likes this.
  16. Kwame Twum

    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:
    Code:
    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: Oct 7, 2015
  17. ivanomonti

    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.


    Code:
    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(
    00,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
     
  18. swissmade

    swissmade Active 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
     
  19. gameone

    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: Dec 18, 2015
  20. DonManfred

    DonManfred Expert Licensed User

    Then i guess the file does not exists. Or maybe it is just in the wrong path.

    Try to include the php file with an absolute path (or enable error_reporting)
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice