Android Question Mp3 File encryption

DaniDPX

Member
Licensed User
Hello team B4X, I was looking for your kind support on recommending best option to encrypt .mp3 file and decrypt it before playing on a b4a app. I wanted to secure the file and decrypt it quickly as possible before playing it considering users will keep pressing next or prev button.

What I used to fetch the mp3 from http as such...
B4X:
Dim o As OutputStream
o = File.OpenOutput(File.DirInternal & "/xxxx/", CurrentxxxxxID, False)
File.Copy2(Job.GetInputStream, o)
o.Close

This is how the player I use load the .mp3 file
B4X:
MediaPlayer1.Load(Dir, Filename)

Thanks!
Daniel G.
 

Brandsum

Well-Known Member
Licensed User
You can use RandomAccessFile for encryption purpose and AudioStreamer for playing decrypted bytes from mp3 file.

Here is the code:
B4X:
Dim data() As Byte
Dim raf As RandomAccessFile
If Not(File.Exists(File.DirDefaultExternal,"filename")) Then
    raf.Initialize(File.DirDefaultExternal,"filename",False)
    data = Bit.InputStreamToBytes(job.GetInputStream)
    raf.writeEncryptedObject(data,"encryption_key",0)
    raf.Close
Else
    raf.Initialize(File.DirDefaultExternal,"filename",False)
    data = raf.ReadEncryptedObject("encryption_key",0)
    raf.Close
End If

Dim m As AudioStreamer
m.Initialize("mediaPlayer", 44100, False, 16, m.VOLUME_MUSIC)
m.StartPlaying
m.Write(data)
 
Upvote 0

DaniDPX

Member
Licensed User
Thanks @Brandsum, I've followed your code snippet and made sample project. I attached it here for you to check for error I encounter. Will this code be efficient to decrypt ~ 10mb file quickly?

B4X:
Sub Process_Globals

End Sub

Sub Globals
    Dim data() As Byte
    Dim raf As RandomAccessFile
    Private Label1 As Label
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Home")   
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub dd_Progress(Progress As Long, Total As Long)
    Try
'        ProgressBar1.Progress = Progress / Total * 100
        Label1.Text = NumberFormat(Progress / 1048576, 1, 2) & "MB / " & NumberFormat(Total / 1048576, 1, 2) & "MB"
        Log(Label1.Text)
    Catch
'        Log(LastException)
    End Try
End Sub

Sub dd_Complete(Job As HttpJob)
    If Not(File.Exists(File.DirDefaultExternal,"filename")) Then
'        File.WriteString(File.DirDefaultExternal,"filename","")
'        File.clo
    Log ("encrypting downloaded song")
        raf.Initialize(File.DirDefaultExternal,"filename",False)
        data = Bit.InputStreamToBytes(Job.GetInputStream)
        raf.writeEncryptedObject(data,"encryption_key",0)
        raf.Close
    End If
   
    Log("Job completed: " & Job.Success)
    Job.Release
End Sub

Sub Download_Click
    Dim dd As DownloadData
    dd.url = "http://www.hochmuth.com/mp3/Vivaldi_Sonata_eminor_.mp3"
    dd.EventName = "dd"
    dd.Target = Me
    CallSubDelayed2(DownloadService, "StartDownload", dd)
End Sub

Sub Play_Click
    If File.Exists(File.DirDefaultExternal,"filename") Then
        Log ("De-crypting downloaded song")
       
        raf.Initialize(File.DirDefaultExternal,"filename",False)
        data = raf.ReadEncryptedObject("encryption_key",0)
        raf.Close
       
        Dim m As AudioStreamer
        m.Initialize("mediaPlayer", 44100, False, 16, m.VOLUME_MUSIC)
        m.StartPlaying
        m.Write(data)
    End If
End Sub
 

Attachments

  • Mp3Crypt.zip
    24.5 KB · Views: 205
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Upvote 0
Top