B4J Question Encryp a File

Nokia

Active Member
Licensed User
Longtime User
I am trying to find a way to encrypt a file with the jB4xEncryption. I would been to open the file as a byte stream and then encrypt it impute stream and then write the file back .



Does anybody have any ideas on a good way to doing this?



Can I use the file.openinpute() or am I barking up the wrong tree?



It would seem that I would have to open the file and read a few bytes at a time and encrypt them along the way and then write the bytes to a new location.
 

Nokia

Active Member
Licensed User
Longtime User
Encrypt the file and save the data as a new file:
B4X:
  Dim c As B4XCipher
   Dim in As InputStream = File.OpenInput(...)
   Dim encryptedData() As Byte = c.Encrypt(Bit.InputStreamToBytes(in), "password")
  
   Dim out As OutputStream = File.OpenOutput(...)
   out.WriteBytes(encryptedData, 0, encryptedData.Length)
   out.Close


thanks for the reply that helped out alot.. any idea how big a file can be encrypted?
 
Upvote 0

Nokia

Active Member
Licensed User
Longtime User

Thanks for creating it.. it easy to use.. just have a few questions..

Are there any planed upgrades in the future maybe like a 256 bit encryption?

last questions.. is there a way to encrypt as a string and return as string and not as bytes?
would be helpful to write a string encryption to a config file and read it back..

Thanks..
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Are there any planed upgrades in the future maybe like a 256 bit encryption?
Not in the near future. AES128 is considered a strong encryption algorithm.

is there a way to encrypt as a string and return as string and not as bytes?
You can always convert bytes to string with StringUtils (base64 encoding).
 
Upvote 0

Nokia

Active Member
Licensed User
Longtime User
You will need to test it.

when I try to encrypt something that is over 100 mb I get a out of memory error:

and how do I catch this error?
it does not point to the code area that it's running on.. it just shows error..

Program started.
java.lang.RuntimeException: java.lang.OutOfMemoryError: Java heap space
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:114)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:90)
at anywheresoftware.b4a.objects.Timer$TickTack$1.run(Timer.java:118)
at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.OutOfMemoryError: Java heap space
at java.util.Arrays.copyOf(Arrays.java:3236)
at java.io.ByteArrayOutputStream.grow(ByteArrayOutputStream.java:118)
at java.io.ByteArrayOutputStream.ensureCapacity(ByteArrayOutputStream.java:93)
at java.io.ByteArrayOutputStream.write(ByteArrayOutputStream.java:153)
at anywheresoftware.b4a.objects.streams.File.Copy2(File.java:357)
at anywheresoftware.b4a.keywords.Bit.InputStreamToBytes(Bit.java:94)
at b4j.example.filecrypt._enctimer_tick(filecrypt.java:383)
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:497)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:607)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:228)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:158)
at sun.reflect.GeneratedMethodAccessor5.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:93)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:90)
at anywheresoftware.b4a.objects.Timer$TickTack$1.run(Timer.java:118)
at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
at com.sun.javafx.application.PlatformImpl$$Lambda$51/2350749.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
at com.sun.javafx.application.PlatformImpl$$Lambda$50/9599617.run(Unknown Source)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
at com.sun.glass.ui.win.WinApplication$$Lambda$39/20085625.run(Unknown Source)
... 1 more
 
Upvote 0

EnriqueGonzalez

Expert
Licensed User
Longtime User
May be by adding this line:

B4X:
#VirtualMachineArgs: -Xms1024m -Xmx1024m

And if you read carefully the error was here (this part gives you in which sub was the error but not the specific line):
at b4j.example.filecrypt._enctimer_tick(filecrypt.java:383)
 
Upvote 0

Nokia

Active Member
Licensed User
Longtime User
May be by adding this line:

B4X:
#VirtualMachineArgs: -Xms1024m -Xmx1024m

And if you read carefully the error was here (this part gives you in which sub was the error but not the specific line):
at b4j.example.filecrypt._enctimer_tick(filecrypt.java:383)

thanks.. for the info.. was able to encrypt 155 mb file and up to 425 mb of a few files.. thanks for the tip on where to find the error..
 
Upvote 0
Top