Android中的AES加密解密算法

时间:2014-02-27 14:48:10

标签: android algorithm encryption aes

我  我试图使用AES算法加密和解密我的数据 Android系统。 这是我的代码

  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;
    }
    }

** 当我尝试运行我的应用程序时,我的应用程序停止并且 我在logcat中遇到以下错误。 Plz帮助我...... **

    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

2 个答案:

答案 0 :(得分:1)

问题在于:

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();

}

您在findViewById()之外呼叫onCreate(),因此这些视图都没有被夸大,而且它们还不存在。因此findViewById()为每个视图返回null,data.getText()将抛出您在日志中看到的NullPointerException。

答案 1 :(得分:0)

所有这些必须在onCreate

之后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在当前的infalted布局中查找id为id的视图。因此,您需要在将布局设置为活动后初始化视图。

还从onResume

中的editText获取文本

点击按钮

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