ak93
ak93

Reputation: 1319

Android encrypted audio player

How would I go about playing a set of encrypted audio files in a audio player android app?

I have an app project where I would have a set of encrypted audio content stored on a users device. A single audio content (example: podcast) would in this case consist of multiple encrypted files. The app acts as a audio player, through which users listen to this audio content. For good user experience the whole content (podcast) should play seamlessly from one file to another. This works perfectly for normal audio files using android's MediaPlayer class.

For security measures, no decrypted file should at any point be present on the users device storage in a way that a user can access this file any other way than through my app (even on rooted devices).

The type of encryption to be used is not fixed yet, so I am open to any algorithm that would work best for this case.

I thought about decrypting each file before playing it. Files are a maximum of 25MB in size, and a AES encryption/decryption for such file takes some 300 milliseconds, so this does not disturb the user experience too much.

There are two problems I see here with two different approaches to decryption:

  1. If I decrypt a file and store it on device as a normal audio file, the user will be able to get to it (rooted devices) and use it outside my app.
  2. If I decrypt a file and keep it in system memory as lets say a byte array, how do I play this back to the user? I have found no way to do this using MediaPlayer, or have found any other library that would allow this out of the box. Maybe some audio streaming library could be modified to play out of a decrypted file stream?

Upvotes: 4

Views: 4284

Answers (3)

Haris Qurashi
Haris Qurashi

Reputation: 2124

There are many ways to do achieve your goal some of them are given below:

  1. Stream your file from a given URL using MediaPlayer.setDataSource(Context, Uri)
  2. Use HttpURLConnection.getInputStream() to get local InputStream create FileInputStream and use MediaPlayer.setDataSource(FileInputStream.getFD()).
  3. Download your audio file using HTTP request but before saving apply some encryption. When you want to play, apply your decryption algorithm, retrieve stream from saved file convert into FileInputStream and use MediaPlayer.setDataSource(FileInputStream.getFD()).

Update: There is also an alternative to MediaPlayer that might be useful to be used for your needs*: https://github.com/google/ExoPlayer

Upvotes: 2

Budius
Budius

Reputation: 39846

The simples way, IMO, is to use ExoPlayer, which is an open-source media player developed by Google. They dealt on this project with lots of edge cases and it's a nice and reliable player.

On top of that your needs are easy to achieve pretty much out-of-the-box.

You'll just load your player using a AesCipherDataSource wrapping a FileDataSource.

So your file is encryped locally using AesCihper and decrypted on the fly, chunk by chunk by the player itself.

Here is the link to their dev-guide: https://google.github.io/ExoPlayer/guide.html and to the AesCipher http://google.github.io/ExoPlayer/doc/reference/com/google/android/exoplayer2/upstream/crypto/AesCipherDataSource.html

Upvotes: 5

Rock_Artist
Rock_Artist

Reputation: 735

  1. Since you're dealing with encrypted data if you don't want to expose anything best way would be implement the MediaPlayer yourself... So basically you'd be the one decoding it. might be worth looking at some open source media players for Android

  2. Instead of using memory a different approach would be streaming as localhost. setting up a small HTTP server. that way you'll keep the smallest amount of decrypted data on the device at a time.

Upvotes: 0

Related Questions