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: 8,727
  • Encryption_java_source.zip
    27.2 KB · Views: 1,980
Last edited by a moderator:

Fabrice La

Active Member
Licensed User
Longtime User
Hello
I juste used your "code module for cripting passwords"

but the text decrypted is not the same.

What I am doing wrong ....
 

Fabrice La

Active Member
Licensed User
Longtime User
I am using your code exactly like it is.

If I use "password" as text this is the crypted part:
79E2F0264C042BDBE25FE42A54129B89

and if I decrypt it :
y??&L+??_?*T??

Not realy the same :BangHead:
 

peacemaker

Expert
Licensed User
Longtime User
Strange. I've rechecked - it works.
Passwords are encoded before saving to a file, and decoded just after reading from this file.
I tried before with password like "111", "222"...but now have checked "pass" - it's decoded well also.
 

Rusty

Well-Known Member
Licensed User
Longtime User
My encryption/decryption works great within the Android environment.
Does anyone have sample VB.NET code to encrypt/decrypt data transferred (encrypted) from the Android to the PC?
If so, can you please post it?
Thanks in advance.
 

Rusty

Well-Known Member
Licensed User
Longtime User
I am using your encryption library and all works well, both encrypt and decrypt.
However, if I encrypt a 5 byte text, the returned encrypted data is 24 bytes long. Likewise, if I encrypt a 10 or 12... byte it comes back 24 bytes long.
Is there a parameter or setting I'm using incorrectly that sets this length?
Thanks,
 

Rusty

Well-Known Member
Licensed User
Longtime User
Sorry, I made too many assumptions.
B4X:
Sub fnEncrypt(dataToEncrypt As String ) As String 
    Dim kg As KeyGenerator
    Dim c As Cipher
    Dim B64 As Base64
    Dim Bconv As ByteConverter

    Dim data(0) As Byte
    Dim iv(0) As Byte
    iv = Array As Byte(211, 5, 233, 24, 55, 166, 7, 88) ' 16 bytes for AES
        
    c.Initialize("DESEDE/CBC/PKCS5Padding")     '   "DESEDE/CBC/NoPadding")            '
    c.InitialisationVector = iv
    kg.Initialize("DESEDE") 
    
    kg.KeyFromBytes(Bconv.StringToBytes("1234567890123456","ASCII"))
    data = Bconv.StringToBytes(dataToEncrypt, "ASCII")        
    data = c.Encrypt(data, kg.Key, True)                

    Return B64.EncodeBtoS(data, 0, data.Length)
    
End Sub
I pass in "Mr Smith" (as an example) and the data returned is 24 bytes long, it decrypts just fine back to "Mr Smith", but I'm trying to understand why the 8 bytes converts to 24 bytes. I'm trying to then write the data into a database with a fixed length column. My column is 20 bytes long and Mr Smith would fit just fine un-encrypted.
I'm sure I've got a parameter or a padding set wrong.
Thanks for your quick response.
I just tried the DESEDE/CBC/NoPadding and the resultant string was 12 bytes long instead of 24. Is there an overhead imposed upon encrypted strings that require longer results than the original string?
 
Last edited:

agraham

Expert
Licensed User
Longtime User
DESEDE (triple-DES) is a block cipher. Data is padded (that's the PKCS5Padding bit) to a length that is a multiple of 8 bytes then each 8 byte block is encrypted in turn (that's the CBC bit) and the encrypted data will be a multiple of 8 bytes. In the case of "Mr Smith" you get 16 bytes. Your Base64 encoding then stretches each 8 byte block to 12 characters (not bytes) hence 24 characters in this case. As strings are coded as UTF16 characters in Java the string is actually 48 bytes long. However when saved to the database if probably gets re-encoded to UTF8 characters (and encoded back to UTF16 when read) which in the case of the Base64 character set will come back down to a 24 byte string in the actual database file.
 

Rusty

Well-Known Member
Licensed User
Longtime User
Is there an algorithm for calculating a database column width to store an encrypted string?
i.e. A 10 character string after encryption will be 24 characters
A 24 character string after encryption will be 48 characters...etc.
Thanks for the help.:)
 

Rusty

Well-Known Member
Licensed User
Longtime User
Thanks Agraham,
My mistake in expression of bytes/characters, I know better..
 

mddmx

Member
Licensed User
Longtime User
Sorry for a stupid question and I am sure that I a missing a major point

Am I supposed to provide the Encrypt and Decrypt String functions some kind of unique seed value ?

Doesn't seem secure if we are all using the same example code ?????

Thank you.


Never mind I found the answer. The following needs to be changed to a unique 16 character string
kg.KeyFromBytes(bconv.StringToBytes("1234567890123456","ASCII"))

Makes me wonder how many times this block of code was used "as is" ...... ?
 
Last edited:

PFlores81

Active Member
Licensed User
Longtime User
Would it be possible to use this to do real time encryption of certain parts of the ext_sdcard ? I ask because the project I am working on requires some sort of encryption for it to be completed.

I still dont have the initial pw prompt setup for when you load the app. but encryption is more important
 

NeoTechni

Well-Known Member
Licensed User
Longtime User
I'm getting:
javax.crypto.IllegalBlockSizeException: data not block size aligned

On large blocks of HTML I'm encrypting for an email inbox
The text is a multiple of 16 digits in length, but once it goes through Bconv.StringToBytes it's not anymore.
 
Top