android - Cipher Output and Input Streams -
i'm attempting store encrypted data in android filesystem. i'm getting errors don't understand , empty files. please help.
code:
private cipher cipher; private arraylist<connectionprofile> connectionprofiles; public void createcipher() throws exception{ cipher = cipher.getinstance("aes/cbc/pkcs5padding"); } public void saveprofiles() { try { if (cipher == null) {createcipher();} fileoutputstream fos = openfileoutput("connprofiles.bin", context.mode_private); bufferedoutputstream bos = new bufferedoutputstream(fos); cipheroutputstream cos = new cipheroutputstream(bos, cipher); objectoutputstream oos = new objectoutputstream(cos); oos.writeobject(connectionprofiles); oos.flush(); oos.close(); } catch (exception e) { e.printstacktrace(); } } public void readprofiles() { try { if (cipher == null) {createcipher();} fileinputstream fis = openfileinput("connprofiles.bin"); bufferedinputstream bis = new bufferedinputstream(fis); cipherinputstream cis = new cipherinputstream(bis, cipher); objectinputstream ois = new objectinputstream(cis); connectionprofiles = (arraylist<connectionprofile>) ois.readobject(); ois.close(); } catch (exception e) { e.printstacktrace(); ; } }
traceback:
05-09 23:24:39.628: w/system.err(837): java.lang.illegalstateexception 05-09 23:24:39.639: w/system.err(837): @ javax.crypto.cipher.update(cipher.java:884) 05-09 23:24:39.639: w/system.err(837): @ javax.crypto.cipheroutputstream.write(cipheroutputstream.java:95) 05-09 23:24:39.639: w/system.err(837): @ java.io.dataoutputstream.writeshort(dataoutputstream.java:192) 05-09 23:24:39.648: w/system.err(837): @ java.io.objectoutputstream.writestreamheader(objectoutputstream.java:1815) 05-09 23:24:39.648: w/system.err(837): @ java.io.objectoutputstream.<init>(objectoutputstream.java:279) 05-09 23:24:39.648: w/system.err(837): @ com.sajnasoft.down2home.mainactivity.saveprofiles(mainactivity.java:39) 05-09 23:24:39.648: w/system.err(837): @ com.sajnasoft.down2home.mainactivity$2.onclick(mainactivity.java:92) 05-09 23:24:39.658: w/system.err(837): @ android.view.view.performclick(view.java:4204) 05-09 23:24:39.658: w/system.err(837): @ android.view.view$performclick.run(view.java:17355) 05-09 23:24:39.658: w/system.err(837): @ android.os.handler.handlecallback(handler.java:725) 05-09 23:24:39.658: w/system.err(837): @ android.os.handler.dispatchmessage(handler.java:92) 05-09 23:24:39.658: w/system.err(837): @ android.os.looper.loop(looper.java:137) 05-09 23:24:39.668: w/system.err(837): @ android.app.activitythread.main(activitythread.java:5041) 05-09 23:24:39.668: w/system.err(837): @ java.lang.reflect.method.invokenative(native method) 05-09 23:24:39.668: w/system.err(837): @ java.lang.reflect.method.invoke(method.java:511) 05-09 23:24:39.678: w/system.err(837): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:793) 05-09 23:24:39.678: w/system.err(837): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:560) 05-09 23:24:39.678: w/system.err(837): @ dalvik.system.nativestart.main(native method) 05-09 23:26:33.878: w/iinputconnectionwrapper(837): showstatusicon on inactive inputconnection
update:
so have
private spinner spinner; private spinadapter adapter; private cipher cipher; private arraylist<connectionprofile> connectionprofiles; private keygenerator keygen; private secretkey key; public void createcipher() throws exception{ cipher = cipher.getinstance("aes/cbc/pkcs5padding"); keygen = keygenerator.getinstance("aes"); key = keygen.generatekey(); } public void saveprofiles() { try { if (cipher == null) {createcipher();} cipher.init(cipher.encrypt_mode, key); fileoutputstream fos = openfileoutput("connprofiles.bin", context.mode_private); bufferedoutputstream bos = new bufferedoutputstream(fos); cipheroutputstream cos = new cipheroutputstream(bos, cipher); objectoutputstream oos = new objectoutputstream(cos); oos.writeobject(connectionprofiles); oos.flush(); oos.close(); } catch (exception e) { e.printstacktrace(); } } public void readprofiles() { try { if (cipher == null) {createcipher();} cipher.init(cipher.encrypt_mode, key); fileinputstream fis = openfileinput("connprofiles.bin"); bufferedinputstream bis = new bufferedinputstream(fis); cipherinputstream cis = new cipherinputstream(bis, cipher); objectinputstream ois = new objectinputstream(cis); connectionprofiles = (arraylist<connectionprofile>) ois.readobject(); ois.close(); } catch (exception e) { e.printstacktrace(); ; } }
and:
05-11 22:20:40.658: w/system.err(1019): java.io.streamcorruptedexception 05-11 22:20:40.658: w/system.err(1019): @ java.io.objectinputstream.readstreamheader(objectinputstream.java:2109) 05-11 22:20:40.658: w/system.err(1019): @ java.io.objectinputstream.<init>(objectinputstream.java:372) 05-11 22:20:40.658: w/system.err(1019): @ com.sajnasoft.down2home.mainactivity.readprofiles(mainactivity.java:59) 05-11 22:20:40.658: w/system.err(1019): @ com.sajnasoft.down2home.mainactivity.oncreate(mainactivity.java:83) 05-11 22:20:40.658: w/system.err(1019): @ android.app.activity.performcreate(activity.java:5104) 05-11 22:20:40.658: w/system.err(1019): @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1080) 05-11 22:20:40.668: w/system.err(1019): @ android.app.activitythread.performlaunchactivity(activitythread.java:2144) 05-11 22:20:40.668: w/system.err(1019): @ android.app.activitythread.handlelaunchactivity(activitythread.java:2230) 05-11 22:20:40.668: w/system.err(1019): @ android.app.activitythread.access$600(activitythread.java:141) 05-11 22:20:40.668: w/system.err(1019): @ android.app.activitythread$h.handlemessage(activitythread.java:1234) 05-11 22:20:40.668: w/system.err(1019): @ android.os.handler.dispatchmessage(handler.java:99) 05-11 22:20:40.668: w/system.err(1019): @ android.os.looper.loop(looper.java:137) 05-11 22:20:40.668: w/system.err(1019): @ android.app.activitythread.main(activitythread.java:5041) 05-11 22:20:40.678: w/system.err(1019): @ java.lang.reflect.method.invokenative(native method) 05-11 22:20:40.678: w/system.err(1019): @ java.lang.reflect.method.invoke(method.java:511) 05-11 22:20:40.678: w/system.err(1019): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:793) 05-11 22:20:40.678: w/system.err(1019): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:560) 05-11 22:20:40.678: w/system.err(1019): @ dalvik.system.nativestart.main(native method)
alright i'm initializing cipher , salt in oncreate, , methods have gotten more complicated follows. end result corrupted stream when attempting read.
private spinner spinner; private spinadapter adapter; private cipher cipher; private arraylist<connectionprofile> connectionprofiles; private keygenerator keygen; private secretkey key; private string salt; private secretkey saltedkey; private static final string random_algorithm = "sha1prng"; private ivparameterspec ivspec; public void createkey() throws exception { keygen = keygenerator.getinstance("aes"); key = keygen.generatekey(); byte[] saltedkeybytes = new byte[key.getencoded().length+salt.getbytes().length]; system.arraycopy(key.getencoded(), 0, saltedkeybytes, 0, key.getencoded().length); system.arraycopy(salt.getbytes(), 0, saltedkeybytes, key.getencoded().length, salt.getbytes().length); saltedkey = new secretkeyspec(saltedkeybytes, 0, saltedkeybytes.length, "aes"); } private byte[] generateiv() throws nosuchalgorithmexception { securerandom random = securerandom.getinstance(random_algorithm); byte[] iv = new byte[16]; random.nextbytes(iv); return iv; } public void saveprofiles() { try { if (key == null) {createkey();} cipher.init(cipher.encrypt_mode, saltedkey, ivspec); fileoutputstream fos = openfileoutput("connprofiles.bin", context.mode_private); bufferedoutputstream bos = new bufferedoutputstream(fos); cipheroutputstream cos = new cipheroutputstream(bos, cipher); objectoutputstream oos = new objectoutputstream(cos); oos.writeobject(connectionprofiles); oos.flush(); oos.close(); fileoutputstream keyoutputstream = openfileoutput("key.bin", context.mode_private); keyoutputstream.write(key.getencoded()); keyoutputstream.flush(); keyoutputstream.close(); byte[] iv = generateiv(); ivparameterspec ivspec = new ivparameterspec(iv); fileoutputstream ivoutputstream = openfileoutput("iv.bin", context.mode_private); ivoutputstream.write(iv); ivoutputstream.flush(); ivoutputstream.close(); } catch (exception e) { e.printstacktrace(); } } public void readprofiles() { try { file file = new file(this.getfilesdir(), "key.bin"); byte[] keybytes = new byte[(int) file.length()]; fileinputstream keyinputstream = new fileinputstream(file); keyinputstream.read(keybytes); keyinputstream.close(); file file2 = new file(this.getfilesdir(), "iv.bin"); byte[] iv = new byte[(int) file2.length()]; fileinputstream ivinputstream = new fileinputstream(file2); ivinputstream.read(iv); ivinputstream.close(); ivparameterspec ivspec = new ivparameterspec(iv); byte[] saltedkeybytes = new byte[keybytes.length+salt.getbytes().length]; system.arraycopy(keybytes, 0, saltedkeybytes, 0, keybytes.length); system.arraycopy(salt.getbytes(), 0, saltedkeybytes, keybytes.length, salt.getbytes().length); saltedkey = new secretkeyspec(saltedkeybytes, 0, saltedkeybytes.length, "aes"); cipher.init(cipher.decrypt_mode, saltedkey, ivspec); fileinputstream fis = openfileinput("connprofiles.bin"); bufferedinputstream bis = new bufferedinputstream(fis); cipherinputstream cis = new cipherinputstream(bis, cipher); objectinputstream ois = new objectinputstream(cis); connectionprofiles = (arraylist<connectionprofile>) ois.readobject(); ois.close(); } catch (exception e) { e.printstacktrace(); ; } }
traceback:
05-19 01:08:17.325: w/system.err(843): java.io.streamcorruptedexception 05-19 01:08:17.325: w/system.err(843): @ java.io.objectinputstream.readstreamheader(objectinputstream.java:2109) 05-19 01:08:17.325: w/system.err(843): @ java.io.objectinputstream.<init>(objectinputstream.java:372) 05-19 01:08:17.335: w/system.err(843): @ com.sajnasoft.down2home.mainactivity.readprofiles(mainactivity.java:102) 05-19 01:08:17.335: w/system.err(843): @ com.sajnasoft.down2home.mainactivity.oncreate(mainactivity.java:132) 05-19 01:08:17.335: w/system.err(843): @ android.app.activity.performcreate(activity.java:5104) 05-19 01:08:17.335: w/system.err(843): @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1080) 05-19 01:08:17.335: w/system.err(843): @ android.app.activitythread.performlaunchactivity(activitythread.java:2144) 05-19 01:08:17.335: w/system.err(843): @ android.app.activitythread.handlelaunchactivity(activitythread.java:2230) 05-19 01:08:17.335: w/system.err(843): @ android.app.activitythread.access$600(activitythread.java:141) 05-19 01:08:17.335: w/system.err(843): @ android.app.activitythread$h.handlemessage(activitythread.java:1234) 05-19 01:08:17.345: w/system.err(843): @ android.os.handler.dispatchmessage(handler.java:99) 05-19 01:08:17.345: w/system.err(843): @ android.os.looper.loop(looper.java:137) 05-19 01:08:17.345: w/system.err(843): @ android.app.activitythread.main(activitythread.java:5041) 05-19 01:08:17.345: w/system.err(843): @ java.lang.reflect.method.invokenative(native method) 05-19 01:08:17.345: w/system.err(843): @ java.lang.reflect.method.invoke(method.java:511) 05-19 01:08:17.345: w/system.err(843): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:793) 05-19 01:08:17.345: w/system.err(843): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:560) 05-19 01:08:17.355: w/system.err(843): @ dalvik.system.nativestart.main(native method)
alright, got working. not using right ivspec cipher initialization. try (iv field):
public void saveprofiles() { try { if (key == null) { createkey(); iv = generateiv(); ivspec = new ivparameterspec(iv); } cipher.init(cipher.encrypt_mode, saltedkey, ivspec); fileoutputstream fos = openfileoutput("connprofiles.bin", context.mode_private); bufferedoutputstream bos = new bufferedoutputstream(fos); cipheroutputstream cos = new cipheroutputstream(bos, cipher); objectoutputstream oos = new objectoutputstream(cos); oos.writeobject(connectionprofiles); oos.flush(); oos.close(); fileoutputstream keyoutputstream = openfileoutput("key.bin", context.mode_private); keyoutputstream.write(key.getencoded()); keyoutputstream.flush(); keyoutputstream.close(); fileoutputstream ivoutputstream = openfileoutput("iv.bin", context.mode_private); ivoutputstream.write(iv); ivoutputstream.flush(); ivoutputstream.close(); } catch (exception e) { e.printstacktrace(); } }
in test, didn't use salt. cause problems if key gets long, too.
Comments
Post a Comment