2014-02-27 118 views
1

我 上午尝试使用Android算法中的AES算法对我的数据进行加密和解密。 这是我的代码Android中的AES加密解密算法

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 = "fedcba"; 
    private String SecretKey = "123456789"; 

    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 
+0

什么是'MainActivity.java'行28 – Raghunandan

回答

0

所有这些必须在onCreatesetContentView

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的视图。因此,您需要在将布局设置为“活动”后初始化视图。

而且从EDITTEXT获得文本中的onResume

在按钮点击

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

我做到了这一点,它的工作。感谢很多!但现在加密和解密存在问题,尤其是解密数据。 – user3360750

+0

@ user3360750很乐意提供帮助。因为你是新来的stackoverflow检查这个http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work – Raghunandan

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。

+0

我做到了这一点,它的工作。感谢很多!但是加密和解密存在问题,特别是在解密数据方面。 – user3360750

+0

@ user3360750 http://stackoverflow.com/questions/17079579/aes-algo-decryption-issue – Raghunandan