B4A Library Base64 and Encryption library

Here's a library that, for the moment, can perform Base64 encoding and decoding and symmetric algorithm encryption and decryption. Tested symmetric algorithms are DES, Triple DES and AES (Rijndael).

As the Java encryption rountines are all byte array oriented you will need my ByteConverter library to run the demo.

EDIT :- Version 1.1 posted. Asymmetric algorithms, Signing and MACs now implemented. See post#2 for details.
 

Attachments

  • Encryption1.1.zip
    25.4 KB · Views: 7,026
  • Encryption_java_source.zip
    27.2 KB · Views: 1,020
Last edited by a moderator:

Locutus

Member
Licensed User
Happy new year!

Can you start a new thread for this question and upload a small project that demonstrates this issue?
I will try.

Edit:
No need! In the process of creating the example project I found out that the IV being different was what caused the issue. It was the one thing I missed checking (grumbles about signed verses unsigned bytes).

Thank you for your interest.
 
Last edited:

MaxApps

Active Member
Licensed User
Hi

Is there a way to check a file, to see if it is encrypted?

Kind regards
Jakob
 

MaxApps

Active Member
Licensed User
Ok.
I have made an app, where the user can save encrypted notes. These notes are saved to text files and can be opened with the app, when needed.
The problem is, when you open a text file, which is not encrypted and try to decrypt it, the app will crash. Is there a way to catch this error and avoid the app crashes?

B4X:
java.lang.RuntimeException: Bad Base64 input character decimal 45 in array position 8


    at anywheresoftware.b4a.agraham.encryption.Base64.DecodeBtoB(Base64.java:325)
    at anywheresoftware.b4a.agraham.encryption.Base64.DecodeStoS(Base64.java:368)
    at dk.maxapps.cryptonote.main._afkod(main.java:541)
    at dk.maxapps.cryptonote.main._loadinit(main.java:1001)
    at dk.maxapps.cryptonote.main._slidemenu_click(main.java:1173)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:525)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:173)
    at anywheresoftware.b4a.keywords.Common.CallSub4(Common.java:858)
    at anywheresoftware.b4a.keywords.Common.CallSubNew2(Common.java:815)
    at dk.maxapps.cryptonote.slidemenu._mlistview_itemclick(slidemenu.java:187)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:525)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:173)
    at anywheresoftware.b4a.BA$2.run(BA.java:283)
    at android.os.Handler.handleCallback(Handler.java:730)
    at android.os.Handler.dispatchMessage(Handler.java:92)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:5419)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:525)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
    at dalvik.system.NativeStart.main(Native Method)
java.lang.RuntimeException: Bad Base64 input character decimal 45 in array position 8


Kind regards
Jakob
 

MaxApps

Active Member
Licensed User
Thanks Erel.

That works like a charm :)

B4X:
    Try
      Dim B64 As Base64
   
      kode = B64.DecodeStoS(edtMain.Text, "UTF8")
      edtMain.Text=(kode)
    Catch

    End Try

Kind regards
Jakob
 

yazak

Member
Licensed User
Ok. Let me ask in another way.

Initialize sub only accepts NoPadding,ISO10126Padding,PKCS1Padding,PKCS5Padding and SSL3Padding.
But I need PKCS7Padding (because the data I am processing is encrypted that way). When I give this parameter Initialize sub doesn't give me any error probably defaults to some other padding algorithm.

So how can I get this library to understand this parameter and work correctly. I thought you ported this lib into B4A so you have the source code. Am I right?

PKCS#5 padding is identical to PKCS#7 padding . so just use PKCS5Padding see this link http://en.wikipedia.org/wiki/Padding_(cryptography)

Also you can always implement your own padding scream using NoPadding
 
Last edited:

GaNdAlF89

Active Member
Licensed User
Hi, how can I do this php line in B4A?

B4X:
base64_encode(hash_hmac('sha256', $s, "HJpolkdjglkjdg"));

Thanks
 
Last edited:

Robert Grimmett

Member
Licensed User
Im having troubles with this. I can encrypt and decrypt its own data.. for testing purposes i am using a static key and IV. using AES/CBC/PKCS7Padding

this works just fine until i try to decrypt an encrypted string using the same key and IV with the same AES/CBC/PKCS7Padding setup.. in both Cold fusion and Visual basic.. (the CF and visual basic applications can share data with no issues.) for the life of me.. I cannot get it to work on the android.. Any ideas as to why..

Right now im currently looking for alternatives to sharing data with the android. but ultimately i would like to be able to share the data in its base encryption with no need to convert. the data transfer HAS to be encrypted and the storage on the device has to be encrypted as well. Any idea's as to why this is the case, I would love to hear them.
 

yazak

Member
Licensed User
You just need to make your CF and VB run a small test to show the outputted bytes of encryption/Decryption
and do same on B4A test, then compare each to see if get same results. If there not that's the problem.

Note use 16 bytes for 128 bit key. (don't think 192 bit key and 256 bit key are supported) IV is always 16 bytes for 128 bits.
 

Robert Grimmett

Member
Licensed User
You just need to make your CF and VB run a small test to show the outputted bytes of encryption/Decryption
and do same on B4A test, then compare each to see if get same results. If there not that's the problem.

Note use 16 bytes for 128 bit key. (don't think 192 bit key and 256 bit key are supported) IV is always 16 bytes for 128 bits.
I've done the verification .. and they don't generate the same data.. however vb and cf do.. so unless there is something missing.. or different as to how the android creates its data.. I'm at a loss and will have to find an alternative way. If this were less important data then i would use just a simple base64 encoding through an ssl connection and re-encode the data on the android. however has to fly on the HIPAA radar and needing it to span an install-able APP, a web app and an android app..(down the road ios as well which im not looking forward to with the security issues there). and outside of the web app the other 2 have to be able to carry some persistent data for short periods of time in case there is no data connection. once the connection is established it updates with no issue and removes the data from the previously unconnected system.

In all this i have even tried using the b4a to generate the core key and IV to make sure it was compatible, however even that produced dissimilar results.
 

yazak

Member
Licensed User
Here the code I use ... works fine communicating with PureBasic I use. maybe this will help you. One thing I have noticed even with 16/32/48/64 characters AES Deciphered Andriod needs padding still, when really it shouldn't maybe a bug.

B4X:
Sub AES_Encrypt(Inputdata() As Byte) As Byte()
    Dim data(0) As Byte, iv(0) As Byte

    iv = Array As Byte(0x24,0xD7,0x55,0x68,0xBA,0x2F,0x34,0x17,0x39,0x07,0xDA,0x66,0x61,0x19,0x38,0x7B) 'iv (initialisation vector)

    Dim kg As KeyGenerator ,c As Cipher
    c.Initialize("AES/CBC/PKCS5Padding") ' using 01 0202 030303 04040404  <-- padding method    public-key cryptography standards five -  PKCS5/PKCS7

    ' CBC needs an initialisation vector
    c.InitialisationVector = iv
    kg.Initialize("AES")
    kg.KeyFromBytes(Array As Byte(0x11,0x56,0x7E,0x42,0xC1,0x9A,0x23,0x9A,0x46,0xA7,0xCE,0x93,0xDE,0xD5,0x55,0x92) ) 'Key

    data = c.Encrypt(Inputdata, kg.key, True)
    Return data

End Sub


Sub AES_Decrypt(Inputdata() As Byte) As Byte()
    Dim data(0) As Byte, iv(0) As Byte
  
    iv = Array As Byte(0x24,0xD7,0x55,0x68,0xBA,0x2F,0x34,0x17,0x39,0x07,0xDA,0x66,0x61,0x19,0x38,0x7B) 'iv (initialisation vector)

    Dim kg As KeyGenerator ,c As Cipher
    c.Initialize("AES/CBC/PKCS5Padding") ' using 01 0202 030303 04040404  <-- padding method    public-key cryptography standards five -  PKCS5/PKCS7

    ' CBC needs an initialisation vector
    c.InitialisationVector = iv
    kg.Initialize("AES")
    kg.KeyFromBytes(Array As Byte(0x11,0x56,0x7E,0x42,0xC1,0x9A,0x23,0x9A,0x46,0xA7,0xCE,0x93,0xDE,0xD5,0x55,0x92) ) 'Key
  
    data = c.Decrypt(Inputdata, kg.key, True)
    Return data
End Sub
 
Last edited:

Robert Grimmett

Member
Licensed User
Works great on the android.. but when i try to implement a key/iv pair on another one of the systems it fails saying that the either the key is to small or the IV is not 16 bits.. go figure.. I have seen so many people do this with no issue (allegedly) i have yet to see it done through VB,CF, and B4a(android) I have to be missing something so simple since each one works on its own.. and VB and CF can talk. Thanks for the help though..at least i am able to focus on an easier to store structure than what I was doing.
 

Javier Alonso

Member
Licensed User
Hi, agraham, I need some help.

I need to use RSA asymmetric encryption in my project, and I have tried to wrap the CipherInputStream and CipherOutputStream classes in a library to complement yours, but my knoledge of java is limited (that's why I'm using B4A) and I haven't succeeded. A friend wrapped up the java code (attached) and I have compiled it in b4A (using simple library compiler) but something is missing because I don't manage to open any cipheroutputstream from within B4A. Could you please help me?
 

Attachments

  • RSA.zip
    47.8 KB · Views: 116

iconia

Member
Licensed User
Any chance the Library could be extended to include key agreement?

I was also wondering how I could enable bouncycastle in b4j to use with this library?
 

atulindore

Member
Licensed User
Dear All
I want to encrypt one string in B4a with DES or AES with my own key and need to decrypt same in Oracle.
Any guidance how this can be done ..
 

xeriko

Member
Licensed User
Hi,
thank you for library agraham. it is works like a charm.
are you planning porting to B4i ?
 

xeriko

Member
Licensed User
Hi Erel,

when i use this function in B4i

B4X:
Sub Encrypt(dataToEncrypt As String) As String
    Dim su As StringUtils
    Dim kg As KeyGenerator
    Dim C As Cipher
    Dim md As MessageDigest
    Dim encrypted() As Byte
   
    Dim cHashKey As String = "deneme"
  
   If cHashKey.Trim = "" Then Return False

    kg.Initialize("AES")
    kg.KeyFromBytes(md.GetMessageDigest(cHashKey.GetBytes("UTF8"), "MD5"))
      
    C.Initialize("AES/ECB/PKCS5Padding")
    encrypted = C.Encrypt(dataToEncrypt.GetBytes("UTF8"), kg.Key, False)

    Return su.EncodeBase64(encrypted)
End Sub

i am getting this error

B4X:
Are you missing a library reference?
Occurred on line: 62 (Main)
    Dim kg As KeyGenerator
 
Top