the given code uses aes encryption 128 bit key value given user. used code in android project following code encrypts @ time of decryption shows exception (error in decryption) , results in unfortunately stopping app. here following code,
import android.util.base64; import android.util.log; import java.io.unsupportedencodingexception; import java.security.messagedigest; import java.security.nosuchalgorithmexception; import java.util.arrays; import javax.crypto.cipher; import javax.crypto.spec.secretkeyspec; public class aes { static final string tag = "symmetricalgorithmaes"; private secretkeyspec secretkey; private byte[] key; private string decryptedstring; private string encryptedstring; //sets value of key public void setkey(string mykey) { messagedigest sha = null; try { key = mykey.getbytes("utf-8"); sha = messagedigest.getinstance("sha-1"); key = sha.digest(key); key = arrays.copyof(key, 16); secretkey = new secretkeyspec(key, "aes"); } catch (unsupportedencodingexception e) { // todo auto-generated catch block log.e(tag, "error in setting key"); } catch (nosuchalgorithmexception e) { e.printstacktrace(); } } public secretkeyspec getkey(){ return secretkey; } public string getdecryptedstring() { return decryptedstring; } public void setdecryptedstring(string decryptedstring) { this.decryptedstring = decryptedstring; } public string getencryptedstring() { return encryptedstring; } public void setencryptedstring(string encryptedstring) { this.encryptedstring = encryptedstring; } //method encryption public void encrypt(string strtoencrypt) { try { cipher cipher = cipher.getinstance("aes/ecb/pkcs5padding"); cipher.init(cipher.encrypt_mode, secretkey); byte[] encd = cipher.dofinal(strtoencrypt.getbytes("utf-8")); byte[] cc =base64.encode(encd,base64.default); setencryptedstring(new string(cc)); } catch (exception e) { log.e(tag, "error in encryption"); } } //method decryption public void decrypt(string strtodecrypt) { byte[] decd = null; try { cipher cipher = cipher.getinstance("aes/ecb/pkcs5padding"); cipher.init(cipher.decrypt_mode, secretkey); decd = cipher.dofinal(strtodecrypt.getbytes("utf-8")); } catch (exception e) { log.e(tag, "error in decryption" + decd); } setdecryptedstring(decd.tostring()); } }
here logcat
08-10 12:27:23.187 30907-30907/com.test.encryptiontest e/symmetricalgorithmaes: error in decryptionnull 08-10 12:27:23.189 30907-30907/com.test.encryptiontest e/androidruntime: fatal exception: main process: com.test.encryptiontest, pid: 30907 java.lang.illegalstateexception: not execute method android:onclick @ android.support.v7.app.appcompatviewinflater$declaredonclicklistener.onclick(appcompatviewinflater.java:293) @ android.view.view.performclick(view.java:5198) @ android.view.view$performclick.run(view.java:21147) @ android.os.handler.handlecallback(handler.java:739) @ android.os.handler.dispatchmessage(handler.java:95) @ android.os.looper.loop(looper.java:148) @ android.app.activitythread.main(activitythread.java:5417) @ java.lang.reflect.method.invoke(native method) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:726) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:616) caused by: java.lang.reflect.invocationtargetexception @ java.lang.reflect.method.invoke(native method) @ android.support.v7.app.appcompatviewinflater$declaredonclicklistener.onclick(appcompatviewinflater.java:288) @ android.view.view.performclick(view.java:5198) @ android.view.view$performclick.run(view.java:21147) @ android.os.handler.handlecallback(handler.java:739) @ android.os.handler.dispatchmessage(handler.java:95) @ android.os.looper.loop(looper.java:148) @ android.app.activitythread.main(activitythread.java:5417) @ java.lang.reflect.method.invoke(native method) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:726) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:616) caused by: java.lang.nullpointerexception: attempt invoke virtual method 'java.lang.string java.lang.object.tostring()' on null object reference @ com.test.encryptiontest.aes.decrypt(aes.java:86) @ com.test.encryptiontest.decode.decode(decode.java:23) @ java.lang.reflect.method.invoke(native method) @ android.support.v7.app.appcompatviewinflater$declaredonclicklistener.onclick(appcompatviewinflater.java:288) @ android.view.view.performclick(view.java:5198) @ android.view.view$performclick.run(view.java:21147) @ android.os.handler.handlecallback(handler.java:739) @ android.os.handler.dispatchmessage(handler.java:95) @ android.os.looper.loop(looper.java:148) @ android.app.activitythread.main(activitythread.java:5417) @ java.lang.reflect.method.invoke(native method) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:726) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:616) --------- beginning of system
try note code
cipher cipher = cipher.getinstance("aes/ecb/pkcs5padding"); cipher.init(cipher.decrypt_mode, secretkey); decd = cipher.dofinal(strtodecrypt.getbytes("utf-8"));
one 1 , see line generates exception.
Comments
Post a Comment