#AdditionalJar: bcprov-jdk15on-150
Sub Process_Globals
Private fx As JFX
Private MainForm As Form
Private ProgressBar1 As ProgressBar
End Sub
Sub AppStart (Form1 As Form, Args() As String)
MainForm = Form1
MainForm.RootPane.LoadLayout("1") 'Load the layout file.
MainForm.Show
EncryptFile("C:\Users\H\Downloads\1.7z", "123456")
' DecryptFile("C:\Users\H\Downloads\1.7z.enc", "123456")
End Sub
Sub EncryptFile (Path As String, Password As String)
Dim size As Long = File.Size(Path, "")
Dim rafInput, rafOutput As RandomAccessFile
rafInput.Initialize(Path, "", True)
rafOutput.Initialize(Path & ".enc", "", False)
Dim enc As B4XCipher
Dim buffer(2024 * 1024) As Byte
Do While rafInput.CurrentPosition < size
Dim count As Int = rafInput.ReadBytes(buffer, 0, Min(buffer.Length, size - rafInput.CurrentPosition), rafInput.CurrentPosition)
Dim b() As Byte
If count = buffer.Length Then
b = buffer
Else
Dim b(count) As Byte
Bit.ArrayCopy(buffer, 0, b, 0, count)
End If
Dim bb() As Byte = enc.Encrypt(b, Password)
rafOutput.WriteInt(bb.Length, rafOutput.CurrentPosition)
rafOutput.WriteBytes(bb, 0, bb.Length, rafOutput.CurrentPosition)
ProgressBar1.Progress = rafInput.CurrentPosition / size
Sleep(30)
Loop
rafInput.Close
rafOutput.Close
fx.Msgbox(MainForm, "Done!", "")
End Sub
Sub DecryptFile (Path As String, Password As String)
Dim size As Long = File.Size(Path, "")
Dim rafInput, rafOutput As RandomAccessFile
rafInput.Initialize(Path, "", True)
rafOutput.Initialize(Path & ".decrypted", "", False)
Dim b() As Byte
Dim enc As B4XCipher
Do While rafInput.CurrentPosition < size
Dim blockLength As Int = rafInput.ReadInt(rafInput.CurrentPosition)
If b.Length <> blockLength Then
Dim b(blockLength) As Byte
End If
rafInput.ReadBytes(b, 0, b.Length, rafInput.CurrentPosition)
Dim bb() As Byte = enc.Decrypt(b, Password)
rafOutput.WriteBytes(bb, 0, bb.Length, rafOutput.CurrentPosition)
ProgressBar1.Progress = rafInput.CurrentPosition / size
Sleep(30)
Loop
rafInput.Close
rafOutput.Close
fx.Msgbox(MainForm, "Done!", "")
End Sub