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:

NeoTechni

Well-Known Member
Licensed User
That is correct. You will need to create a new array with the correct size and copy the first array to the second one.

You can use ByteConverter.ArrayCopy to copy the data.

Thank you.

EDIT: Yeah, that did the trick.
 
Last edited:

NeoTechni

Well-Known Member
Licensed User
I've been trying to find which countries you can't export encryption to, I've so far found:
Cuba, Iran, Iraq, Libya, North Korea, Sudan, Syria

Which don't have access to the Google Play store anyway
 

realblue

Member
Licensed User
Hi agraham,

As I am trying to convert an encryption function from VB.Net to B4A in this thread I need a little update on your lib.

I need to initialize Chiper as following

B4X:
Dim c As Cipher
c.Initialize("AES/CBC/PKCS7Padding")

According to the documentation the lib doesn't support PKCS7Padding. Could you update your lib to support that.

Thanks in advance.
 

realblue

Member
Licensed User
Hi again;

I ment this documentation. As it reads:

Padding may be one of
NoPadding
ISO10126Padding
PKCS1Padding The padding scheme described in PKCS1, used with the RSA algorithm.
PKCS5Padding The padding scheme described in RSA Laboratories, "PKCS5: version 1.5, November 1993.
SSL3Padding The padding scheme defined in the SSL Protocol Version 3.0, November 18, 1996,
section 5.2.3.2 (CBC block cipher):

If I use c.Initialize("AES/CBC/PKCS7Padding") doesn't give me any error but dosn't return correct keybytes either.

Do you think you can help?
 

realblue

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?
 

agraham

Expert
Licensed User
I object to the tone of your last post. As I state in reply #1 in this thread.
Version 1.1 completes the simplified exposure of all the algorithms implemented by the standard Java Cryptography Architecture (JCA).
The library is a shallow wrapper over the Android crypto classes and if Android javax.crypto.Cipher does not support that transformation there is nothing more to do. Cipher.Initialize looks like
B4X:
    public void Initialize(String transformation) throws Exception
    {
        cipher = Cipher.getInstance(transformation);
    }
 

realblue

Member
Licensed User
Hi agraham sorry to bother you, but I didn't mean what you felt. I didn't mean anything but help at all.

I am desperately seeking for the solution.
 

Torby

Member
Licensed User
Beware: I'm trying to do it now. The rest of you can make it work in minutes, but I'll be weeks before I get the compiler to accept it :confused:

Wow. Your readme tells me what to do with it? +1000 points.
 
Last edited:

Torby

Member
Licensed User
And less than 40 minutes later, I have encrypted and decrypted a string!

+1E15 points!
 

slowtime

Active Member
Licensed User
Hi agraham,

are you planning a porting of your usefull library to B4J ?

I'd prefer use your library to share cryped data on PC.

Best regards

Ciao
slowtime
 

slowtime

Active Member
Licensed User
Thank you.
Then I must copy your library jar and xlm in additional folder of B4J and then use it like i do in b4a.

Or I must do other.

Ciao
 

Locutus

Member
Licensed User
First of all I want to wish everybody a Happy New Year!

Secondly I have come across a situation I find very confusing I am able to encrypt some data just fine. I save it to a file. In the same function I load that data from a file and decrypt it. All is fine.
I then load that data from the file in a different function and decrypt it. There are no errors, the same key and iv are used yet, at the beginning of the decrypted data one or two bytes are changed. The rest of the data decrypts fine. It seems to be in the first 16 bytes. For example

Raw data: (in hex)
3962326262666138353466386536371E

After encryption, saving to a file, loading from a file and decryption in the same function
3962326262666138353466386536371E

After loading from a file and decryption in a different function.
396232626266613835346638653F371E

You will notice the 14th byte is different. This position changes but always seems to be in the first 16 bytes.

The same code is used for decrypting in both cases as shown below
B4X:
    rafSig.Initialize(strFileDir,strSigName,True)
    strHash = rafSig.ReadObject(0)

    bArray = Bconv.HexToBytes(strHash)
    bCArray = c.Decrypt(bArray,kg.key,True)
    Log("Decrypted " &  Bconv.HexFromBytes(bCArray))

After much testing every value is correct up until the c.Decrypt call. Any ideas why the byte changes?

PS: I forgot to mention that this does not happen all the time. Sometimes it decrypts correctly.
 
Last edited:
Top