#Region Project Attributes
#ApplicationLabel: B4A Encrypt
#VersionCode: 1
#VersionName:
'SupportedOrientations possible values: unspecified, landscape or portrait.
#SupportedOrientations: unspecified
#CanInstallToExternalStorage: False
#End Region
#Region Activity Attributes
#FullScreen: False
#IncludeTitle: True
#End Region
#AdditionalJar: bcprov-jdk15on-150
Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.
'Private fx As JFX
'Private MainForm As Activity
End Sub
Sub Globals
'These global variables will be redeclared each time the activity is created.
'These variables can only be accessed from this module.
Dim RootName As String
Dim InputFile As String
Dim OutputFile As String
Private ProgressBar1 As ProgressBar
Private Label1 As Label
Private Label2 As Label
Private Label3 As Label
Private Label4 As Label
Private Label8 As Label
Private Label10 As Label
End Sub
Sub Activity_Create(FirstTime As Boolean)
'Do not forget to load the layout file created with the visual designer. For example:
Activity.LoadLayout("1")
Activity.LoadLayout("1") 'Load the layout file.
'MainForm.Show
Dim rp As RuntimePermissions
RootName = rp.GetSafeDirDefaultExternal("")
InputFile = RootName & "/MCCB_D-840999-000-12_VST_BL_ADM_XXX_17_APR_2020_09-06-02_EN_PT_01_OF_$$_XXXXXXXXX_V4800_.mp4"
OutputFile = RootName & "/MCCB_D-840999-000-12_VST_BL_ADM_XXX_17_APR_2020_09-06-02_EN_PT_01_OF_$$_XXXXXXXXX_V4800_.ENC"
EncryptFile(InputFile,OutputFile,"Password")
Sleep(200)
DecryptFile(OutputFile,"Password")
End Sub
Sub Activity_Resume
End Sub
Sub Activity_Pause (UserClosed As Boolean)
End Sub
Sub EncryptFile (Input_Path As String, Output_Path As String, Password As String)
'full paths with filenames
Dim size As Long = File.Size(Input_Path, "")
Dim rafInput, rafOutput As RandomAccessFile
rafInput.Initialize(Input_Path, "", True)
rafOutput.Initialize(Output_Path, "", False)
Dim enc As B4XCipher
Dim buffer(2024 * 1024) As Byte
'Buff size = 2072576 &H1FA000
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)
If Label2.Text.Length < 50 Then
For x = 0 To b.Length -1 'represent text file content
'Label2.Text = Label2.Text & b(x) & ","
Next
End If
End If
Dim bb() As Byte = enc.Encrypt(b, Password)
If Label3.Text.Length < 50 Then
For x = 0 To bb.Length -1 'represent text file content
'Label3.Text = Label3.Text & bb(x) & ","
Next
End If
Label4.Text ="ENC byte count: " & bb.Length
rafOutput.WriteInt(bb.Length, rafOutput.CurrentPosition)
rafOutput.WriteBytes(bb, 0, bb.Length, rafOutput.CurrentPosition)
ProgressBar1.Progress = (rafInput.CurrentPosition / size) * 100
Label1.Text = rafInput.CurrentPosition & "/" & size
Sleep(30)
Loop
rafInput.Close
rafOutput.Close
ToastMessageShow("Encryption Done", False)
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)
If Label8.Text.Length < 50 Then
For x = 0 To b.Length -1 'represent text file content
Label8.Text = Label8.Text & b(x) & ","
Next
End If
Dim bb() As Byte = enc.Decrypt(b, Password)
If Label10.Text.Length < 50 Then
For x = 0 To bb.Length -1 'represent text file content
Label10.Text = Label10.Text & bb(x) & ","
Next
End If
'rafOutput.WriteBytes(bb, 0, bb.Length, rafOutput.CurrentPosition)
ProgressBar1.Progress = rafInput.CurrentPosition / size * 100
Sleep(30)
Loop
rafInput.Close
rafOutput.Close
ToastMessageShow("DoneDecrypting", False)
'fx.Msgbox(MainForm, "Done!", "")
End Sub