B4J Library Alice - AES Encryption


Licensed User
This is a wrap for this Github Project.

Java AES encryption library

Alice is a Java AES/DES encryption library for working with byte arrays, files, and streams. Various key lengths, block modes, padding schemes, key deriviation functions, and Message Authentication Codes (MAC) are available. See the javadoc for more information.

<link>Aliceproject on Github|https://github.com/rockaport/alice</link>
Author: DonManfred
Version: 0.07
  • Alice
    • Events:
      • Decrypt (success As Boolean, path As String, Filename As String)
      • Encrypt (success As Boolean, path As String, Filename As String)
    • Functions:
      • decryptBytes (encrypted As Byte(), password As String) As Byte()
      • decryptFile (sourcePath As String, sourceFilename As String, destPath As String, destFilename As String, password As String)
      • decryptFile2 (sourcePath As String, sourceFilename As String, destPath As String, destFilename As String, password As String)
      • decryptStream (input As java.io.InputStream, output As java.io_OutputStream, password As Char())
      • encryptBytes (input As Byte(), password As Char()) As Byte()
      • encryptFile (sourcePath As String, sourceFilename As String, destPath As String, destFilename As String, password As String)
      • encryptFile2 (sourcePath As String, sourceFilename As String, destPath As String, destFilename As String, password As String)
      • encryptStream (input As java.io.InputStream, output As java.io_OutputStream, password As Char())
      • generateKey (algorithm As String, keyLength As String) As Byte()
        algorithm can be either AES, DES or DESede
        keyLength can be either BITS_64, BITS_128, BITS_192 pr BITS_256
      • generateKey2 (algorithm As String, keyLength As String) As Byte()
      • Initialize (EventName As String, context As com.rockaport.alice.AliceContext)
  • AliceConstants
    • Fields:
      • AES As com.rockaport.alice.AliceContext.Algorithm
      • CBC As com.rockaport.alice.AliceContext.Mode
      • CTR As com.rockaport.alice.AliceContext.Mode
      • DES As com.rockaport.alice.AliceContext.Algorithm
      • DESede As com.rockaport.alice.AliceContext.Algorithm
      • GCM As com.rockaport.alice.AliceContext.Mode
      • GcmTagLengthBITS_104 As com.rockaport.alice.AliceContext.GcmTagLength
      • GcmTagLengthBITS_112 As com.rockaport.alice.AliceContext.GcmTagLength
      • GcmTagLengthBITS_120 As com.rockaport.alice.AliceContext.GcmTagLength
      • GcmTagLengthBITS_128 As com.rockaport.alice.AliceContext.GcmTagLength
      • GcmTagLengthBITS_96 As com.rockaport.alice.AliceContext.GcmTagLength
      • HMAC_SHA_1 As com.rockaport.alice.AliceContext.MacAlgorithm
      • HMAC_SHA_256 As com.rockaport.alice.AliceContext.MacAlgorithm
      • HMAC_SHA_384 As com.rockaport.alice.AliceContext.MacAlgorithm
      • HMAC_SHA_512 As com.rockaport.alice.AliceContext.MacAlgorithm
      • KeyLengthBITS_128 As com.rockaport.alice.AliceContext.KeyLength
      • KeyLengthBITS_192 As com.rockaport.alice.AliceContext.KeyLength
      • KeyLengthBITS_256 As com.rockaport.alice.AliceContext.KeyLength
      • KeyLengthBITS_64 As com.rockaport.alice.AliceContext.KeyLength
      • MacAlgorithmNONE As com.rockaport.alice.AliceContext.MacAlgorithm
      • PaddingNO_PADDING As com.rockaport.alice.AliceContext.Padding
      • PaddingPKCS5_PADDING As com.rockaport.alice.AliceContext.Padding
      • PBKDF_2_WITH_HMAC_SHA_1 As com.rockaport.alice.AliceContext.Pbkdf
      • PBKDF_2_WITH_HMAC_SHA_256 As com.rockaport.alice.AliceContext.Pbkdf
      • PBKDF_2_WITH_HMAC_SHA_384 As com.rockaport.alice.AliceContext.Pbkdf
      • PBKDF_2_WITH_HMAC_SHA_512 As com.rockaport.alice.AliceContext.Pbkdf
      • PbkdfNONE As com.rockaport.alice.AliceContext.Pbkdf
      • SHA_1 As com.rockaport.alice.AliceContext.Pbkdf
      • SHA_224 As com.rockaport.alice.AliceContext.Pbkdf
      • SHA_256 As com.rockaport.alice.AliceContext.Pbkdf
      • SHA_384 As com.rockaport.alice.AliceContext.Pbkdf
      • SHA_512 As com.rockaport.alice.AliceContext.Pbkdf
  • AliceContextBuilder
    • Functions:
      • build As com.rockaport.alice.AliceContext
      • Initialize (EventName As String)
      • IsInitialized As Boolean
    • Properties:
      • Algorithm As String [write only]
      • GcmTagLength As String [write only]
      • Iterations As Int [write only]
      • IvLength As Int [write only]
      • KeyLength As String [write only]
      • MacAlgorithm As String [write only]
      • Mode As String [write only]
      • Padding As String [write only]
      • Pbkdf As String [write only]

A B4A Version is also available.

I tested it with a file of 2.4GB Size



Licensed User
I just did a wrap for the Alice Github Project. I did not developed anything here. Just made a Githubproject useable in B4A and B4J.

If you know a Githubproject which is doing what you want: Post the url: i´ll see what i can do. Note that it should be compatible with b4a and b4j too.
Last edited:

Peter Meares

Licensed User
I thought this looked really interesting for a small password storage system I have planned for ages.
Simply to encypt a text file with the password information, and decrypt as required.

Example below returns an error on compiling. I have tried different keys, including the one in the original post.
I see a couple of purple underline messages in the B4J editor. e.g. bld.Algorithm = ac.AES indicates a forced string conversion.
I have put the Alice.jar and Alice.xml into my "Additional Libraries/B4J" folder.

What have I missed?

Example of the code.
Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Private alice As Alice
    Private ac As AliceConstants
End Sub
Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    'MainForm.RootPane.LoadLayout("Layout1") 'Load the layout file.
    Dim Foldername As String = "My file location"
    Dim bld As AliceContextBuilder
    bld.Algorithm = ac.AES
    bld.Mode = ac.CTR
' Encrypt file
    alice.encryptFile2(Foldername,"Alice.txt", Foldername, "AliceOut.txt", "FGERHZXCG434TRWR")
End Sub
Error message:
Error occurred on line: 23 (Main)
java.security.InvalidKeyException: Illegal key size
at javax.crypto.Cipher.checkCryptoPerm(Cipher.java:1039)
at javax.crypto.Cipher.implInit(Cipher.java:805)
at javax.crypto.Cipher.chooseProvider(Cipher.java:864)
at javax.crypto.Cipher.init(Cipher.java:1396)
at javax.crypto.Cipher.init(Cipher.java:1327)
at com.rockaport.alice.Alice.encrypt(Alice.java:229)
at de.donmanfred.Alicewrapper.encryptFile2(Alicewrapper.java:105)
at b4j.example.main._appstart(main.java:93)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:632)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:237)

Peter Meares

Licensed User
Sorry about the wrong post location.
Still not working. I am still on Java 8 maybe I should move to 11. I will do some reading around the error.