android - How to download an encrypted png file without loosing data? -
i'm new android. i'm doing application can download encrypted png image file sd card displaying after decryption. notice i'm getting "javax.crypto.illegalblocksizeexception: last block incomplete in decryption image decryption
" when decrypting downloaded image. found downloaded image size 0kb (original - 150kb). downloaded encrypted image browser , checked. i'm getting original image size. i'm sure there wrong in image downloading class. cant figure out. please me. in advance.
image downloading asynctask class
public class downloadimagestask extends asynctask<string, void, bitmap> { private string filename; @override protected bitmap doinbackground(string... urls) { //thread.currentthread().setpriority(thread.max_priority); return download_image(urls[0]); } @override protected void onpostexecute(bitmap result) { storeimage(result); } private bitmap download_image(string url) { bitmap bm = null; file file = new file(url); filename = file.getname(); try { url aurl = new url(url); urlconnection conn = aurl.openconnection(); conn.connect(); inputstream = conn.getinputstream(); bufferedinputstream bis = new bufferedinputstream(is); bm = bitmapfactory.decodestream(bis); bis.close(); is.close(); } catch (outofmemoryerror e) { log.e("hub", "error getting image server : " + e.getmessage().tostring()); } catch (ioexception e) { log.e("hub", "error getting image server : " + e.getmessage().tostring()); } return bm; } public void storeimage(bitmap bm) { bitmapfactory.options bmoptions; bmoptions = new bitmapfactory.options(); bmoptions.insamplesize = 1; string extstoragedirectory = commonutils.getdatafrompreferences("metapath", ""); log.d("extstoragedirectory", extstoragedirectory); outputstream outstream = null; file wallpaperdirectory = new file(extstoragedirectory); if (!wallpaperdirectory.exists()) { wallpaperdirectory.mkdirs(); } file outputfile = new file(wallpaperdirectory, filename); if (!outputfile.exists() || outputfile.length() == 0) { try { outstream = new fileoutputstream(outputfile); } catch (filenotfoundexception e1) { e1.printstacktrace(); } try { bm.compress(bitmap.compressformat.png, 100, outstream); outstream.flush(); outstream.close(); log.d("scratchactivtiy", "image saved"); } catch (filenotfoundexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } catch (exception e) { e.printstacktrace(); } } } }
(all images encrypted me. , hosted them in server. there no issue in ancryption or decryption. tested them. working fine.)
cryptclass
public class cryptclass { public byte[] encrypt(string seed, byte[] cleartext) throws exception { byte[] rawkey = getrawkey(seed.getbytes()); byte[] result = encrypt(rawkey, cleartext); // return tohex(result); return result; } public byte[] decrypt(string seed, byte[] encrypted) throws exception { byte[] rawkey = getrawkey(seed.getbytes()); byte[] enc = encrypted; byte[] result = decrypt(rawkey, enc); return result; } //done private byte[] getrawkey(byte[] seed) throws exception { keygenerator kgen = keygenerator.getinstance("aes"); securerandom sr = securerandom.getinstance("sha1prng"); sr.setseed(seed); kgen.init(128, sr); secretkey skey = kgen.generatekey(); byte[] raw = skey.getencoded(); return raw; } private byte[] encrypt(byte[] raw, byte[] clear) throws exception { secretkeyspec skeyspec = new secretkeyspec(raw, "aes"); cipher cipher = cipher.getinstance("aes"); cipher.init(cipher.encrypt_mode, skeyspec); byte[] encrypted = cipher.dofinal(clear); return encrypted; } private byte[] decrypt(byte[] raw, byte[] encrypted) throws exception { secretkeyspec skeyspec = new secretkeyspec(raw, "aes"); cipher cipher = cipher.getinstance("aes"); cipher.init(cipher.decrypt_mode, skeyspec); byte[] decrypted = cipher.dofinal(encrypted); return decrypted; } }
if understand correctly downloading image first
bufferedinputstream bis = new bufferedinputstream(is); bm = bitmapfactory.decodestream(bis);
then saving file png compression:
bm.compress(bitmap.compressformat.png, 100, outstream);
and after doing decryption on file, right?
i think might want decrypt bytes prior saving them png, or maybe prior using decodestream. otherwise decrypting encrypted bytes decode stream , png compression messed with.
try skipping of bitmapfactory stuff , save initial file as-is, , run decryption. in asynctask:
string savefilepath = <path temporary encrypted file>; fileoutputstream outputstream = new fileoutputstream(savefilepath); int bytesread = -1; byte[] buffer = new byte[4096]; while ((bytesread = is.read(buffer)) != -1) { outputstream.write(buffer, 0, bytesread); } outputstream.close(); inputstream.close();
then run decryption stuff on saved file
Comments
Post a Comment