0

I am trying to encrypt and decrypt my data using AES Algorithm in Android. This is my code

  package com.example.aesandroidsecurity;

    import java.security.NoSuchAlgorithmException;
    //import java.security.spec.AlgorithmParameterSpec;

    import javax.crypto.Cipher;
    import javax.crypto.NoSuchPaddingException;
    import javax.crypto.spec.IvParameterSpec;
    import javax.crypto.spec.SecretKeySpec;

    import android.os.Bundle;
    import android.app.Activity;
    import android.view.Menu;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.Toast;

    public class MainActivity extends Activity {

    private Cipher cipher;
    private SecretKeySpec keyspec;
    private IvParameterSpec ivspec;
    private String iv = "fedcba9876543210";
    private String SecretKey = "1234567890123456";  

    Button encrypt = (Button)findViewById(R.id.button1);
    Button decrypt = (Button)findViewById(R.id.button2);    
    EditText data = (EditText)findViewById(R.id.button2);
    String plainText = data.getText().toString();

    byte[] encrypted = null;
    byte[] decrypted = null;    

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //byte[] keyBytes = new byte[32];

        ivspec = new IvParameterSpec(iv.getBytes());
        keyspec = new SecretKeySpec(SecretKey.getBytes(), "AES");

        try {
            cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");                    
        }
        catch (NoSuchAlgorithmException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        catch (NoSuchPaddingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


        try{
            cipher.init(Cipher.ENCRYPT_MODE, keyspec, ivspec);
            encrypted = cipher.doFinal(plainText.getBytes());           
        }       
        catch(Exception e)
        {
            e.printStackTrace();
        }

        encrypt.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                Toast.makeText(getApplicationContext(), ""+encrypted, 
Toast.LENGTH_LONG).show();
            }
        });     

        decrypt.setOnClickListener(new View.OnClickListener() {         
            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                try{
                    cipher.init(Cipher.DECRYPT_MODE, keyspec, ivspec);
                    decrypted = cipher.doFinal(encrypted);
                }       
                catch(Exception e)
                {
                    e.printStackTrace();
                }

                Toast.makeText(getApplicationContext(), ""+decrypted, 
Toast.LENGTH_LONG).show();
            }
        });     
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is 
present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    }

** When I try to run my application, my application stops and I am getting following errors in logcat. Plz help me...**

    02-27 13:59:42.806: E/Trace(1903): error opening trace file: No such
 file or directory (2)
    02-27 13:59:43.296: D/AndroidRuntime(1903): Shutting down VM
    02-27 13:59:43.345: W/dalvikvm(1903): threadid=1: thread exiting 
with uncaught exception (group=0x40a71930)
    02-27 13:59:43.386: E/AndroidRuntime(1903): FATAL EXCEPTION: main
    02-27 13:59:43.386: E/AndroidRuntime(1903): 
java.lang.RuntimeException: Unable to instantiate activity 
ComponentInfo{com.example.aesandroidsecurity/com.example.aesandroidsecurity.MainActivity}:
 java.lang.NullPointerException
    02-27 13:59:43.386: E/AndroidRuntime(1903):     at 
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2106)

    02-27 13:59:43.386: E/AndroidRuntime(1903):     at 
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)

    02-27 13:59:43.386: E/AndroidRuntime(1903):     at 
android.app.ActivityThread.access$600(ActivityThread.java:141)
    02-27 13:59:43.386: E/AndroidRuntime(1903):     at 
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
    02-27 13:59:43.386: E/AndroidRuntime(1903):     at 
android.os.Handler.dispatchMessage(Handler.java:99)
    02-27 13:59:43.386: E/AndroidRuntime(1903):     at 
android.os.Looper.loop(Looper.java:137)
    02-27 13:59:43.386: E/AndroidRuntime(1903):     at 
android.app.ActivityThread.main(ActivityThread.java:5041)
    02-27 13:59:43.386: E/AndroidRuntime(1903):     at 
java.lang.reflect.Method.invokeNative(Native Method)
    02-27 13:59:43.386: E/AndroidRuntime(1903):     at 
java.lang.reflect.Method.invoke(Method.java:511)
    02-27 13:59:43.386: E/AndroidRuntime(1903):     at 
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)

    02-27 13:59:43.386: E/AndroidRuntime(1903):     at 
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
    02-27 13:59:43.386: E/AndroidRuntime(1903):     at 
dalvik.system.NativeStart.main(Native Method)
    02-27 13:59:43.386: E/AndroidRuntime(1903): Caused by: 
java.lang.NullPointerException
    02-27 13:59:43.386: E/AndroidRuntime(1903):     at 
android.app.Activity.findViewById(Activity.java:1839)
    02-27 13:59:43.386: E/AndroidRuntime(1903):     at 
com.example.aesandroidsecurity.MainActivity.<init>(MainActivity.java:28)

    02-27 13:59:43.386: E/AndroidRuntime(1903):     at 
java.lang.Class.newInstanceImpl(Native Method)
    02-27 13:59:43.386: E/AndroidRuntime(1903):     at 
java.lang.Class.newInstance(Class.java:1319)
    02-27 13:59:43.386: E/AndroidRuntime(1903):     at 
android.app.Instrumentation.newActivity(Instrumentation.java:1054)
    02-27 13:59:43.386: E/AndroidRuntime(1903):     at 
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097)

    02-27 13:59:43.386: E/AndroidRuntime(1903):     ... 11 more
    02-27 13:59:48.845: I/Process(1903): Sending signal. PID: 1903 SIG: 9
1
  • 1
    What is MainActivity.java line 28 Commented Feb 27, 2014 at 14:49

2 Answers 2

1

The problem lies here:

public class MainActivity extends Activity { 

    Button encrypt = (Button)findViewById(R.id.button1);
    Button decrypt = (Button)findViewById(R.id.button2);    
    EditText data = (EditText)findViewById(R.id.button2);
    String plainText = data.getText().toString();

}

You are calling findViewById() outside of onCreate(), so none of those Views have been inflated and they do not exist yet. Thus findViewById() returns null for each of those views, and data.getText() will throw the NullPointerException you see in your logs.

Sign up to request clarification or add additional context in comments.

2 Comments

I did that and its working.Thanks a lot! But there is problem with encryption and decryption, specially in decrypting data.
0

All these must be in onCreate after setContentView

Button encrypt; 
Button decrypt;     
EditText data ;
String plainText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button encrypt = (Button)findViewById(R.id.button1);
Button decrypt = (Button)findViewById(R.id.button2);    
EditText data = (EditText)findViewById(R.id.button2);

findViewById looks for a view with the id in the current infalted layout. So you need to initialize views after setting the layout to the Activity.

Also get Text from editText in onResume

or

On Button click

plainText = data.getText().toString();

1 Comment

I did that and its working.Thanks a lot! But now there is problem with encryption and decryption, specially in decrypting data.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.