2016-04-30 9 views
1

Ich bin neu bei Android. Ich bin ein Projekt auf AES-Verschlüsselung zu tun und ich möchte eine Android-App machen, ich habe AES-Verschlüsselungs-Code in Java, die perfekt funktioniert:Verwenden von Java-Code für Android-Anwendung (für AES-Verschlüsselung)

//AESAlgorithm Class 

package com.example.pr1; 

import java.security.Key; 
import javax.crypto.Cipher; 
import javax.crypto.spec.SecretKeySpec; 

public class AESalgorithm { 
public static String encryptedText; 
private static String algorithm = "AES"; 
private static byte[] keyValue = 
new byte[] { 'A', 'S', 'e', 'c', 'u', 'r', 'e','S', 'e', 'c', 'r', 'e', 't', 'K', 'e', 'y' }; 

// Performs Encryption 
public static String encrypt(String plainText) throws Exception { 
Key key = generateKey(); 
Cipher chiper = Cipher.getInstance(algorithm); 
chiper.init(Cipher.ENCRYPT_MODE, key); 
byte[] encVal = chiper.doFinal(plainText.getBytes()); 
String encryptedValue = encode(encVal); 
return encryptedValue; 
} 

// Performs decryption 
public static String decrypt(String encryptedText) throws Exception { 
// generate key 
Key key = generateKey(); 
Cipher chiper = Cipher.getInstance(algorithm); 
chiper.init(Cipher.DECRYPT_MODE, key); 
byte[] decordedValue = decode(encryptedText); 
byte[] decValue = chiper.doFinal(decordedValue); 
String decryptedValue = new String(decValue); 
return decryptedValue; 
} 

// generateKey() is used to generate a secret key for AES algorithm 
private static Key generateKey() throws Exception { 
Key key = new SecretKeySpec(keyValue, algorithm); 
return key; 
} 

// performs encryption & decryption 
public static void main(String[] args) throws Exception { 

String plainText = "This "; 

//This is the variable i am passing to MainActivity 
encryptedText = AESalgorithm.encrypt(plainText); 
String decryptedText = AESalgorithm.decrypt(encryptedText); 


} 

private static String encode(byte[] byteArray) { 
StringBuilder buf = new StringBuilder(); 
    int intVal = 0; 
String frag = ""; 

for (byte b : byteArray) { 
    intVal = (int) (0xff & b); 
frag = Integer.toHexString(intVal); 
if (1 == frag.length()) { 
frag = "0" + frag; 
    } 
buf.append(frag); 
} 
return buf.toString(); 
} 

private static byte[] decode(String textString) { 
byte[] byteArray = new byte[(textString.length()/2)]; 
int intVal = 0; 
String frag = ""; 
int c1 = 0; 
for (int i = 0; i < byteArray.length; i++) { 
c1 = (i * 2); 
frag = textString.substring(c1, (c1 + 2)); 
intVal = Integer.parseInt(frag, 16); 
byteArray[i] = (byte) (0xff & intVal); 
} 
return byteArray; 
} 
} 

Nun, ich möchte einen Hauptaktivitätscode schreiben, die eine haben sollte Editview und eine Schaltfläche. Die EditView erhält Text, den ich als reinen Text an den AESAlgorithm.java übergeben möchte, der wiederum verschlüsselten Text an die Hauptaktivität zurückgibt, die ich an eine zweite Aktivität (show.java) senden werde, die eine Textansicht haben wird der verschlüsselte Text.

Ich habe bereits eine (Beispiel) Haupttätigkeit als:

MainActivity (Sample): 

package com.example.pr1; 
import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 





public class PRMain extends Activity { 

    protected static final int TAKE_PHOTO_CODE = 0; 
    Button mButton; 
    EditText mEdit; 
    int count; 
    public static String encryptedtext,plaintext; 


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


     mButton = (Button)findViewById(R.id.button); 
     mEdit = (EditText)findViewById(R.id.editText1); 




     mButton.setOnClickListener(
      new View.OnClickListener() 
      { 
       public void onClick(View view) 
       { 
        AESalgorithm AES = new AESalgorithm(); 
        plaintext = mEdit.getText().toString(); 


       Intent intent = new Intent(PRMain.this, Show.class); 
       encryptedtext = AES.encryptedText; 
       Bundle b = new Bundle(); 
       b.putString("name", encryptedtext); 
       intent.putExtras(b); 
       startActivity(intent); 
       finish(); 

       } 

      }); 



    } 


} 

hier „Show.java“ mit nur einem Textview in dem Layout eine einfache Tätigkeit ist, den Text von Hauptaktivität gesendet wird, angezeigt. Ich möchte die Plaintext-Variable an AESAlgorithm.java für die Verschlüsselung übergeben, und ich möchte den verschlüsselten Text in "verschlüsselten Text" -Variable von Java-Datei erhalten. Wie kann ich den Java-Code ändern, um in dieser Android-App zu arbeiten?

Hier ist meine logcat Daten:

04-30 21:03:49.968: D/AndroidRuntime(17765): Shutting down VM 
04-30 21:03:49.968: W/dalvikvm(17765): threadid=1: thread exiting with uncaught exception (group=0x419d7d40) 
04-30 21:03:49.968: W/dalvikvm(17765): threadid=1: uncaught exception occurred 
04-30 21:03:49.969: W/System.err(17765): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.pr1/com.example.pr1.Show}: java.lang.NullPointerException 
04-30 21:03:49.970: W/System.err(17765): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2417) 
04-30 21:03:49.970: W/System.err(17765): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2469) 
04-30 21:03:49.970: W/System.err(17765): at android.app.ActivityThread.access$1100(ActivityThread.java:151) 
04-30 21:03:49.970: W/System.err(17765): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1362) 
04-30 21:03:49.971: W/System.err(17765): at android.os.Handler.dispatchMessage(Handler.java:110) 
04-30 21:03:49.971: W/System.err(17765): at android.os.Looper.loop(Looper.java:193) 
04-30 21:03:49.971: W/System.err(17765): at android.app.ActivityThread.main(ActivityThread.java:5551) 
04-30 21:03:49.971: W/System.err(17765): at java.lang.reflect.Method.invokeNative(Native Method) 
04-30 21:03:49.971: W/System.err(17765): at java.lang.reflect.Method.invoke(Method.java:515) 
04-30 21:03:49.972: W/System.err(17765): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:914) 
04-30 21:03:49.972: W/System.err(17765): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:730) 
04-30 21:03:49.972: W/System.err(17765): at dalvik.system.NativeStart.main(Native Method) 
04-30 21:03:49.972: W/System.err(17765): Caused by: java.lang.NullPointerException 
04-30 21:03:49.973: W/System.err(17765): at com.example.pr1.Show.onCreate(Show.java:34) 
04-30 21:03:49.973: W/System.err(17765): at android.app.Activity.performCreate(Activity.java:5310) 
04-30 21:03:49.974: W/System.err(17765): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1088) 
04-30 21:03:49.974: W/System.err(17765): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2381) 
04-30 21:03:49.974: W/System.err(17765): ... 11 more 
04-30 21:03:49.974: W/dalvikvm(17765): threadid=1: calling UncaughtExceptionHandler 
04-30 21:03:49.977: E/AndroidRuntime(17765): FATAL EXCEPTION: main 
04-30 21:03:49.977: E/AndroidRuntime(17765): Process: com.example.pr1, PID: 17765 
04-30 21:03:49.977: E/AndroidRuntime(17765): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.pr1/com.example.pr1.Show}: java.lang.NullPointerException 
04-30 21:03:49.977: E/AndroidRuntime(17765): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2417) 
04-30 21:03:49.977: E/AndroidRuntime(17765): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2469) 
04-30 21:03:49.977: E/AndroidRuntime(17765): at android.app.ActivityThread.access$1100(ActivityThread.java:151) 
04-30 21:03:49.977: E/AndroidRuntime(17765): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1362) 
04-30 21:03:49.977: E/AndroidRuntime(17765): at android.os.Handler.dispatchMessage(Handler.java:110) 
04-30 21:03:49.977: E/AndroidRuntime(17765): at android.os.Looper.loop(Looper.java:193) 
04-30 21:03:49.977: E/AndroidRuntime(17765): at android.app.ActivityThread.main(ActivityThread.java:5551) 
04-30 21:03:49.977: E/AndroidRuntime(17765): at java.lang.reflect.Method.invokeNative(Native Method) 
04-30 21:03:49.977: E/AndroidRuntime(17765): at java.lang.reflect.Method.invoke(Method.java:515) 
04-30 21:03:49.977: E/AndroidRuntime(17765): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:914) 
04-30 21:03:49.977: E/AndroidRuntime(17765): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:730) 
04-30 21:03:49.977: E/AndroidRuntime(17765): at dalvik.system.NativeStart.main(Native Method) 
04-30 21:03:49.977: E/AndroidRuntime(17765): Caused by: java.lang.NullPointerException 
04-30 21:03:49.977: E/AndroidRuntime(17765): at com.example.pr1.Show.onCreate(Show.java:34) 
04-30 21:03:49.977: E/AndroidRuntime(17765): at android.app.Activity.performCreate(Activity.java:5310) 
04-30 21:03:49.977: E/AndroidRuntime(17765): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1088) 
04-30 21:03:49.977: E/AndroidRuntime(17765): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2381) 

Antwort

0

Sie können die psvm Methode für Android entfernen.

Aber ich verstehe nicht, was das Problem ist?

Wenn Sie die AES-Klasse zu Ihrem Paket in Ihrem Android-Projekt hinzufügen, können Sie es verwenden, indem Sie die statischen Methoden verschlüsseln und entschlüsseln?

+0

Ich habe versucht, das zu tun, ich habe jetzt die Codes aktualisiert. Ich übergebe verschlüsselte Textvariablen an MainActivity, aber die App stürzt weiter ab. –

+0

Könnten Sie den Fehlercode posten? –

+0

Ich habe die Daten, die ich in meinem Logcat bekommen habe, hochgeladen. –

Verwandte Themen