android - Securing media files in the mobile -
android - Securing media files in the mobile -
i thinking developing birds catalog android. contain many pictures , sound files. files come 3rd party company copyrights.
my application should assure (as much possible) media files not accessible, copied or manipulated.
which strategies follow? crypt files in file scheme , decrypt in memory before showing or playing them? maintain them sql lite clobs? sql lite accessible other apps or hidden rest of apps? other ideas? haven't found much info "issue" on web.
thanks in advance,
chemi.
i suggest saving these files sd card, not private file of activity, images/audio files quite big (i have seen in discussion planning handle 400 mb, same app?). crypting should fine, , more straightforward sqlite.
the class below allows encrypting bytes binary files:
import java.io.file; import java.io.fileinputstream; import java.io.fileoutputstream; import java.io.ioexception; import java.security.securerandom; import javax.crypto.cipher; import javax.crypto.keygenerator; import javax.crypto.secretkey; import javax.crypto.spec.secretkeyspec; public class aesencrypter { public static void encrypttobinaryfile(string password, byte[] bytes, file file) throws encrypterexception { seek { final byte[] rawkey = getrawkey(password.getbytes()); final fileoutputstream ostream = new fileoutputstream(file, false); ostream.write(encrypt(rawkey, bytes)); ostream.flush(); ostream.close(); } grab (ioexception e) { throw new encrypterexception(e); } } public static byte[] decryptfrombinaryfile(string password, file file) throws encrypterexception { seek { final byte[] rawkey = getrawkey(password.getbytes()); final fileinputstream istream = new fileinputstream(file); final byte[] buffer = new byte[(int)file.length()]; istream.read(buffer); homecoming decrypt(rawkey, buffer); } grab (ioexception e) { throw new encrypterexception(e); } } private static byte[] getrawkey(byte[] seed) throws encrypterexception { seek { final keygenerator kgen = keygenerator.getinstance("aes"); final securerandom sr = securerandom.getinstance("sha1prng"); sr.setseed(seed); kgen.init(128, sr); // 192 , 256 bits may not available final secretkey skey = kgen.generatekey(); homecoming skey.getencoded(); } grab (exception e) { throw new encrypterexception(e); } } private static byte[] encrypt(byte[] raw, byte[] clear) throws encrypterexception { seek { final secretkeyspec skeyspec = new secretkeyspec(raw, "aes"); final cipher cipher = cipher.getinstance("aes"); cipher.init(cipher.encrypt_mode, skeyspec); homecoming cipher.dofinal(clear); } grab (exception e) { throw new encrypterexception(e); } } private static byte[] decrypt(byte[] raw, byte[] encrypted) throws encrypterexception { secretkeyspec skeyspec = new secretkeyspec(raw, "aes"); seek { final cipher cipher = cipher.getinstance("aes"); cipher.init(cipher.decrypt_mode, skeyspec); homecoming cipher.dofinal(encrypted); } grab (exception e) { throw new encrypterexception(e); } }
}
you need exception class:
public class encrypterexception extends exception { public encrypterexception ( ) { super( ); } public encrypterexception (string str ) { super(str); } public encrypterexception (throwable e) { super(e); } }
then, have utilize follows generate encrypted files:
encrypttobinaryfile("password", bytestosaveencrypted, encryptedfiletosaveto);
and in app, can read them next way:
byte [] cleardata = decryptfrombinaryfiles("password", encryptedfiletoreadfrom);
to utilize hardcoded password can hacked digging obfuscated code , looking strings. don't know whether sufficient security level in case?
if not, can store password in activity's private preferences or using tricks such this.class.getdeclaredmethods()[n].getname() password. more hard find.
about performances, have know crypting / decrypting can take quite long time. requires testing.
[edit: 04-25-2014] there big error in answer. implementation seeding securerandom
, bad ('evil', say).
there easy way circumvent issue. explained in details here in android developers blog. sorry that.
android
Comments
Post a Comment